I am attempting to use jQuery to determine if an image has properly loaded.

The following works just fine (and returns true or false as of the state of the image) but only seems to work in IE, in FireFox, it seems to always return true - even if the state is actually incomplete:

    var image = $("img#myImage");
    alert(image[0].complete);

What is the Firefox equivalent for image.complete in JavaScript or jQuery?

share|improve this question
feedback

6 Answers

You could also try checking the one of the dimensions of the img element in addition to complete:

function isImageLoaded() {
    var theImage = $('#myImage'); 

    if (!theImage.get(0).complete) {
        return false;
    }
    else if (theImage.height() === 0) {
        return false;
    }

    return true;
}

An unloaded img or an img with an invalid src attribute should have .height() and .width() equal to 0 in Firefox. In Chrome and Opera neither method appears to work properly. In those browsers theImage.height() always returned a positive value in my testing.

share|improve this answer
I would think so too; but one of the examples I need to account for is as follows: One of the possible images this applies to resides on Amazon's cloud front, and if the security settings are such that the file exists, but access is denied; then it actually returns some XML content to that call, which the browser is returning a height of 15 and a width of 60. (or similar arbitrary values) – FerrousOxide Aug 12 '09 at 21:31
Interesting...I'll post again if I come across a solution. – Marve Aug 13 '09 at 19:00
feedback

Yes, I was just working on an AJAX page that dynamically loaded images. I also wanted to detect if images were loaded so I could transition them in with a jQuery effect.

img = $('myImageID')
img[0].src = 'http://new.url.for.image/';
alert(img[0].complete);                    // seems to always return true
                                           // in Firefox, perhaps because
                                           // the original src had been loaded

So instead I had to do this...

img = $('myImageID')
newImg = $('<img>')                // create a completely new IMG element
          .attr('src', 'http://new.url.for.image/')
          .attr('id', img[0].id);
img.replaceWith(newImg);
img = newImg;
alert(img[0].complete);

(Note: I haven't tested this exact code, it's a simplified version of what I was trying to do, but hopefully communicates the idea.)

Simon.

share|improve this answer
feedback

Jquery makes it really simple.

You can just use the .load() function to bind an event for the time when image is completely loaded.

    $("img").load(function() {
        // Yeay, it was loaded, and works in all browsers!
    });
share|improve this answer
if the image was cached, IE8 will not fire the load event. See this blogpost by David Walsh: davidwalsh.name/image-load-event – Mansiemans Jun 2 at 16:58
feedback

You can use load() but beware of Internet Explorer: It won't fire the event, if the image is cached

if($img[0].readyState === 4){ // image is cached in IE
    alert("Image loaded!");
}else{
    $img.load(function(){ // for other browsers and IE, if not cached
        alert("Image loaded!");
    });
}
share|improve this answer
feedback
myimage=new Image();
myimage.src="whatever";
if(myimage.height>0 && myimage.complete){alert("my image has loaded");}

// might be overly simple but works for me in all browsers i think.
share|improve this answer
feedback

I dont know if firefox has another prop, method that does it but, you can use this workaround:

$(document).ready(function(){

$("img").each(
//for each image declared in html
function(index)
{
	document.images[index].complete= false;
	document.images[index].onload = function(){ this.complete= true};
});

});

share|improve this answer
3  
You can't set the complete property of an image. It will throw an error because the property is read only. – Alex Oct 28 '09 at 2:41
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.