I am aiming to check if image has been loaded successfully. It has been working well in modern browsers but IE8 or 7 it is a terrible issue. Here is a sample code:
var img = new Image(),
url = 'http://something.com/images/something.gif';
$(img).attr('src', url).load(function() {
if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
alert('broken image!');
}
else {
alert('successfully loaded');
}
}
Anyone has any idea to work around with this issue? Thanks in advace!
You HAVE to set the
onloadhandler BEFORE you set the.srcvalue.In some versions of IE, if the image is in the browser cache, the load event will be fired immediately when the
.srcvalue is set. If your load handler is not already in place, you will miss that event.Also,
naturalWidthandnaturalHeightare NOT supported in older versions of IE so they will always be undefined. And, you should useonerrorandonabortto catch the error conditions.There’s no need to use jQuery for this. You can just do this: