方法一:
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("图片不存在");
}
}