Ответ 1
В прослушивателе событий image.onload
проверьте, являются ли теги image.width
и image.height
равными нулю (предпочтительно image.naturalWidth
и image.naturalHeight
, когда они поддерживаются).
Если ширина и высота равны нулю, изображение считается недействительным.
Демо: http://jsfiddle.net/RbNeG/
// Usage:
loadImage('notexist.png');
function loadImage(src) {
var image = new Image;
image.onload = function() {
if ('naturalHeight' in this) {
if (this.naturalHeight + this.naturalWidth === 0) {
this.onerror();
return;
}
} else if (this.width + this.height == 0) {
this.onerror();
return;
}
// At this point, there no error.
document.body.appendChild(image);
};
image.onerror = function() {
//display error
document.body.appendChild(
document.createTextNode('\nError loading as image: ' + this.src)
);
};
image.src = src;
}