方法一:
function validateImage(url) {
var xmlHttp ;
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
xmlHttp.open("Get",url,false);
xmlHttp.send();
if(xmlHttp.status == 404)
console.log("图片不存在");
else
console.log("图片存在");
}
方法二:
function CheckImgExists(imgurl) {
var ImgObj = new Image(); //判断图片是否存在
ImgObj.src = imgurl;
//没有图片,则返回-1
if (ImgObj.fileSize > 0 || (ImgObj.width > 0 && ImgObj.height > 0)) {
console.log("图片存在");
} else {
console.log("图片不存在");
}
}
本文介绍了两种检查图片是否存在的实用方法。方法一使用 XMLHttpRequest 对象发起 HTTP 请求来判断图片 URL 是否可达;方法二通过创建 Image 对象加载图片,并检查其尺寸来判断图片是否存在。
2070

被折叠的 条评论
为什么被折叠?



