John
John

Reputation: 387

Load image event with jQuery fails in Internet Explorer 8

I'm struggling with loading an image for my popup in Internet Explorer 8, and I don't know why it returns "Image failed to load." all the time.

I first thought it was a caching problem, but it wasn't it seems, so now I don't know what it is...

    var img = $("<img />").attr('src', imageSource)
    .load(function() {
        if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
            $("#imagePopup .content").html('Image failed to load.');
        }
        else {
            $("#imagePopup .content").html(img);
        }
    });

Upvotes: 0

Views: 2043

Answers (2)

Alex
Alex

Reputation: 2126

naturlwidth is not supported in IE8. You would have to use the height and width property.

Upvotes: -1

Javaguru
Javaguru

Reputation: 910

The following worked for me:

var img = $('<img/>').load(function() {
   // Loading done ..
   img.appendTo("#imagePopup .content");
}).error(function(){
   // Unable to load image ..
   $("#imagePopup .content").html('Image failed to load.');
}).attr('src', imageSource);

Upvotes: 4

Related Questions