Javascript 获取浏览器加载后引发404错误的链接列表

Javascript 获取浏览器加载后引发404错误的链接列表,javascript,jquery,html,http,http-status-code-404,Javascript,Jquery,Html,Http,Http Status Code 404,我有一个页面,加载了一堆图像 有些链接指向不存在的图像,因此浏览器在尝试检索这些图像时抛出404 以下是问题的一个示例: <div>01<img id="01" src="http://i.imgur.com/ydiBX0Bb.jpg" /></div><hr /> <div>02<img id="02" src="http://i.imgur.com/aObMmrWb.jpg" /></div><hr /

我有一个页面,加载了一堆图像

有些链接指向不存在的图像,因此浏览器在尝试检索这些图像时抛出404

以下是问题的一个示例:

<div>01<img id="01" src="http://i.imgur.com/ydiBX0Bb.jpg" /></div><hr />
<div>02<img id="02" src="http://i.imgur.com/aObMmrWb.jpg" /></div><hr />
<div>03<img id="03" src="http://www.google.com/does_not_exist.jpg" /></div><hr />
<div>04<img id="04" src="http://www.google.com/also_does_not_exist.jpg" /></div><hr />
<div>05<img id="05" src="http://i.imgur.com/kJezbugb.jpg" /></div><hr />
我可以使用JavaScript/jQuery获取抛出404错误的图像链接列表吗


谢谢

您所寻找的似乎已经在stackoverflow上

您需要做的基本上是将任意一段代码放入for循环中。 如果要这样做,您将需要一组图像源

链接中的普通JS函数:

function resourceExists(url) {
    var http = new XMLHttpRequest();
    http.open('HEAD', url, false);
    http.send();
    return http.status!=404;
}
var aImageSrc = ['path/to/img/1', 'path/to/non/existant/img/2', 'path/to/other/img'];
var aErrorSrc = []; //Will be filled with non-existant images.
var imgStr = '';

for (var i = 0; i < (aImageSrc.length-1); i++) {
    if (!resourceExists(aImageSrc[i])) {
        aErrorSrc.push(aImageSrc[i]);
    } else {
        //Append image to string if it exists.
        imgStr += '<image src="'+aImageSrc[i]+'" alt="Don't forget the people with lesser visibility" />';
    }
}

//Append the string to any element you need to create
//If you want to append the image anyways even tho it does not exist,
//You could remove the else in the for-loop and just put the append line below it.

//To top it off, do a console log to display all the errors.
console.log(aErrorSrc);
使用链接中提到的函数的示例:

function resourceExists(url) {
    var http = new XMLHttpRequest();
    http.open('HEAD', url, false);
    http.send();
    return http.status!=404;
}
var aImageSrc = ['path/to/img/1', 'path/to/non/existant/img/2', 'path/to/other/img'];
var aErrorSrc = []; //Will be filled with non-existant images.
var imgStr = '';

for (var i = 0; i < (aImageSrc.length-1); i++) {
    if (!resourceExists(aImageSrc[i])) {
        aErrorSrc.push(aImageSrc[i]);
    } else {
        //Append image to string if it exists.
        imgStr += '<image src="'+aImageSrc[i]+'" alt="Don't forget the people with lesser visibility" />';
    }
}

//Append the string to any element you need to create
//If you want to append the image anyways even tho it does not exist,
//You could remove the else in the for-loop and just put the append line below it.

//To top it off, do a console log to display all the errors.
console.log(aErrorSrc);
var-aImageSrc=['path/to/img/1','path/to/non/existant/img/2','path/to/other/img'];
var AerorSrc=[]//将填充不存在的图像。
var imgStr='';
对于(变量i=0;i<(aImageSrc.length-1);i++){
如果(!resourceExists(aImageSrc[i])){
AerorSrc.push(aImageSrc[i]);
}否则{
//将图像附加到字符串(如果存在)。
imgStr+='';
}
}
//将字符串附加到需要创建的任何元素
//如果您想以任何方式附加图像,即使它不存在,
//您可以删除for循环中的else,只需将append行放在它下面。
//最重要的是,执行控制台日志以显示所有错误。
console.log(aErrorSrc);
注意

不要忘记删除
console.log()
希望这有帮助


&短跑;希德

没有。但是,您可以在所有对象上循环并测试它们的宽度/高度。未加载的将具有0的宽度/高度。您可以添加onerror事件处理程序,以查看它们是否失败。。。反之亦然。。。添加一个onload事件处理程序,用于注册成功的事件处理程序。使用在图像上循环并获取宽度/高度的方法。工作起来很有魅力!谢谢你的帮助。