Javascript 使用JS确定图像404/文件大小
我在JS中有一个预加载图像的函数: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_
// 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(){…}
?为什么不简单地使图像可用?或者,如果它们不可用,请从脚本中删除它们?