使用jQuery / JS检查图像HTTP状态代码? [英] Check image HTTP status codes using jQuery/JS?
本文介绍了使用jQuery / JS检查图像HTTP状态代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用jQuery / JS检查图像的状态代码?
is it possible to check the status codes of images using jQuery/JS?
例如,
img1 = "http://upload.wikimedia.org/wikipedia/commons/thumb/2/26/YellowLabradorLooking_new.jpg/260px-YellowLabradorLooking_new.jpg";
if(statusCheck(img1) == 404){
/do something
}elseif(statusCheck(img1) == 403){
//do something else
}elseif(statusCheck(img1) == 304){
//do something else
}
谢谢
推荐答案
您可以构建图像
object,不受跨源限制的影响:
You can construct an Image
object, which isn't affected by cross-origin restrictions:
var image = new Image();
image.src = "http://upload.wikimedia.org/wikipedia/commons/thumb/2/26/YellowLabradorLooking_new.jpg/260px-YellowLabradorLooking_new.jpg"
image.onload = function() {
alert('Image has loaded');
};
image.onerror = function() {
alert('Image did not load');
};
图像加载是异步的,因此创建一个返回错误代码的函数不是个好主意。
Image loading is asynchronous, so making a function that returns the error code won't be a good idea.
演示: http:// jsfiddle .net / Blender / apyL7 /
Demo: http://jsfiddle.net/Blender/apyL7/
此外,似乎没有办法获取响应代码,所以你'仅限于知道图像是否加载。
Also, there doesn't seem to be a way to get the response code, so you're limited to knowing only whether the image loaded or not.
这篇关于使用jQuery / JS检查图像HTTP状态代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文