Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用JS确定图像404/文件大小_Javascript - Fatal编程技术网

Javascript 使用JS确定图像404/文件大小

Javascript 使用JS确定图像404/文件大小,javascript,Javascript,我在JS中有一个预加载图像的函数: // http://stackoverflow.com/questions/3646036/javascript-preloading-images var preloaded_images = []; function preload() { for (i = 0; i < arguments.length; i++) { preloaded_images[i] = new Image(); preloaded_

我在JS中有一个预加载图像的函数:

// http://stackoverflow.com/questions/3646036/javascript-preloading-images
var preloaded_images = [];
function preload() {
    for (i = 0; i < arguments.length; i++) {
        preloaded_images[i] = new Image();
        preloaded_images[i].src = preload.arguments[i];
    }
}
//http://stackoverflow.com/questions/3646036/javascript-preloading-images
var预加载_图像=[];
函数预加载(){
对于(i=0;i

但是,我尝试预加载的一些图像会抛出404错误。如何在此函数中检测404,而不尝试将图像添加到DOM?

您可以附加
onerror
eventHandler:

...
preloaded_images[i].onerror = function() {
    // the image couldn't be loaded..
}
...
如果您想知道它已成功加载,请执行以下操作:

...
preloaded_images[i].onload = function() {
    // the image was loaded successful..
}
... 

预加载的_图像[i].onerror=function(){…}
?为什么不简单地使图像可用?或者,如果它们不可用,请从脚本中删除它们?