假设我有EXTERNAL URL(不是来自我的网站),我想验证它以确保这个url是这些文件中的一个: - jpg,jpeg,gif,png也是一个正确的图像文件(不是任何脚本)文件或PHP)。如果可能的话: - 检查图像的URL是否正常工作。
@ jfriend00好的所以这就是我想要做的。我有maded 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>
答案 0 :(得分:2)
此帖子中的代码不是我在the previous post中为您提供的功能的正确实现,并且由于多种原因无效。你不能从onload,onerror处理程序返回true / false。这些是异步事件,您的vrfyImgURL
函数已经返回。
你真的必须使用我放在that previous post中的代码。有用。只是使用它。不要修改它。您传入一个回调函数,并使用验证检查结果调用该回调。您必须使用异步编程来使用此结果调用回调。你不能像你想要的那样使用直接顺序编程。正是您对我的代码进行了修改,使其停止工作。
你可以看到我之前在这里工作的代码:http://jsfiddle.net/jfriend00/qKtra/,在这个例子中,它正在处理来自各种域的图像。它对图像的域不敏感,<img>
标记不受域限制。
要将其连接到表单提交,您可以执行以下操作:
<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,并在调用回调时结束。