【问题标题】:Javascript: Verify External Url Of An ImageJavascript:验证图像的外部 URL
【发布时间】:2012-04-03 00:01:37
【问题描述】:

假设我有外部 URL(不是来自我的网站)并且我想验证它以确保该 URL 来自以下文件:- jpg、jpeg、gif、png 并且也是正确的图像文件(不是任何脚本文件或 php)。如果可能的话:- 检查图像的 url 是否有效。

@jfriend00 好的,这就是我想要做的。我已经制作了 html 表单,当人们提交它时,它会调用一个 javascript 函数,该函数将验证 url 是否是图像。所以这是我的代码。但它不起作用。请告诉我我应该从这里做什么?

<script type="text/javascript"> 

function vrfyImgURL() {
var x = document.forms["submitIMGurl"].elements["img_url"].value;

if(x.match('^http://.*/(.*?).(jpe?g|gif|png)$')){

var imgsrc = x;
var img = new Image();

img.onerror = function(){
alert("Can't Be Loaded");
return false;
}
img.onload = function(){
alert("Loaded Successfully");
return true;
}

img.src = imgsrc;

}else{
    alert("It looks like the url that you had provided is not valid! Please only submit correct image file. We only support these extensions:- jpeg, jpg, gif, png.");
return false;   
}
} 
</script>

<form action="http://www.example.com/current_page" enctype='multipart/form-data' method="post" onsubmit="return vrfyImgURL();" name="submitIMGurl">
<input type="text" value="http://" name="img_url" />
<input type="submit" value="Submit" />
</form>

【问题讨论】:

  • 你为什么要问和这里一样的问题:stackoverflow.com/questions/9714525/javascript-image-url-verify/… 我之前回答过一次?
  • @jfriend00 因为该解决方案不适用于 chrome 中的外部图像 url
  • 该解决方案不关心图像 URL 是否是本地的。在我提供的答案中,图像不受域的任何限制。如果它不适用于外部图像,那么您做错了其他事情。我建议您在该讨论中发布更多信息,我们将在这里解决它,因为这不是一个新问题。在我的jsFiddle 在那个答案中,它使用的是外部图像。
  • @jfriend00 嗨,我已经在问题中包含了我的代码,所以请看看那里。
  • 好的,所以问题真的是“如何将上一个答案中提供的代码连接到我的实际表单提交?”

标签: javascript image url verify


【解决方案1】:

您在这篇文章中的代码不是我在the previous post 中给您的函数的正确实现,并且由于多种原因无法正常工作。您不能从 onload、onerror 处理程序返回 true/false。这些是异步事件,您的 vrfyImgURL 函数已经返回。

你真的必须使用我在that previous post 中输入的代码。有用。就用它吧。不要修改它。您传入一个回调,该回调将被调用并带有验证检查结果。您必须使用异步编程来使用它,其中回调被调用结果。您不能像尝试那样使用直接顺序编程。是您对我的代码的修改使它停止工作。

您可以在此处查看我之前提供给您的代码:http://jsfiddle.net/jfriend00/qKtra/,在示例中,它处理来自各种域的图像。对图片的域不敏感,&lt;img&gt;标签不受域限制。

要将其连接到您的表单提交,您可以这样做:

<form action="http://www.example.com/current_page" enctype='multipart/form-data' method="post" onsubmit="return formSubmit();" name="submitIMGurl">
<input type="text" value="http://" name="img_url" />
<input type="submit" value="Submit" />
</form>

<script type="text/javascript">

function formSubmit() {
    var url = document.forms["submitIMGurl"].elements["img_url"].value;
    if (!checkURL(url)) {
        alert("It looks like the url that you had provided is not valid! Please only submit correct image file. We only support these extensions:- jpeg, jpg, gif, png.");
        return(false);
    }
    testImage(url, function(testURL, result) {
        if (result == "success") {
            // you can submit the form now
            document.forms["submitIMGurl"].submit();
        } else if (result == "error") {
            alert("The image URL does not point to an image or the correct type of image.");
        } else {
            alert("The image URL was not reachable.  Check that the URL is correct.");
        }

    });
    return(false);    // can't submit the form yet, it will get sumbitted in the callback
}

function checkURL(url) {
    return(url.match(/\.(jpeg|jpg|gif|png)$/) != null);
}


function testImage(url, callback, timeout) {
    timeout = timeout || 5000;
    var timedOut = false, timer;
    var img = new Image();
    img.onerror = img.onabort = function() {
        if (!timedOut) {
            clearTimeout(timer);
            callback(url, "error");
        }
    };
    img.onload = function() {
        if (!timedOut) {
            clearTimeout(timer);
            callback(url, "success");
        }
    };
    img.src = url;
    timer = setTimeout(function() {
        timedOut = true;
        callback(url, "timeout");
    }, timeout); 
}

</script>

如果这是我的界面,我会禁用该表单并记下从调用 testImage 开始检查图像 URL 并在调用回调时结束。

【讨论】:

  • 但是我应该如何使用您的代码来验证用户使用表单提交的 url。我的意思是如何在 onsubmit="" 上实现你的代码
  • 我为您的表单添加了一个实现。
  • 太棒了。它可以工作,但只需要在函数之前的 onsubmit="" 中添加“return”。非常感谢。
猜你喜欢
  • 2012-03-31
  • 2020-08-04
  • 1970-01-01
  • 2011-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-17
  • 1970-01-01
相关资源
最近更新 更多