jQuery: check whether image is loaded

Wait for the image!

This is common task. You’d like to load an image and then show it. With jQuery that is simple enough. Let’s assume this code:

<img src="image.jpg" />
<div>displayed after the image is loaded</div>

This is OK if the div is hidden with jQuery you can show it with adding this code in your javascript:

<script>
    $('img').load(function() {
        $('div').show();
    });
</script>

Related posts:

  1. jQuery check for element visibility
  2. load flash .swf in hidden div
  3. $.ajax in jQuery
This entry was posted in javascript, micro tutorial and tagged , , . Bookmark the permalink.

25 Responses to jQuery: check whether image is loaded

  1. Thanks I am going to check it out!

  2. john says:

    stoimen, thanks for the article, it’s kinda strange this technique

  3. haris dunis says:

    Nice shot, many thanx

  4. fatih says:

    very good

  5. bashar says:

    Nice example, exactly wat i was serching about

  6. compiatech says:

    thx fo sharing ,,, (-:

  7. Ben New says:

    Extending this technique you can make a “loading” image appear while the image is loading, and then switch in the image after it has loaded:

    var $image = $(”).css({ display: ‘none’ }).attr({
    src: ‘path/to/image.png’,
    alt: ‘description of image’
    });
    var $loader = $(”).attr({
    src: ‘path/to/loader.gif’
    });
    $(this).append($loader).append($image.load(function() {
    $loader.remove();
    $image.css({ display: ‘inline-block’ });
    }));

    Or you could use ‘inline’ or ‘block’ for the final display property of the image, whatever matches your requirements.

  8. Vivekanand says:

    Hi Author,

    The code which you have shared is not working for me. I want when one image is loaded I need to showcase another image depending on the previous image loaded.

    Hope this is the correct plugin: http://github.com/peol/jquery.imgloaded/blob/master/ahpi.imgload.js

    Thanks,
    Vivek [Founder of DeveloperSnippets.com]

  9. Stoimen says:

    This seems to be a different use case, isn’t it? You’d like to load an image only after another image is loaded?

  10. Weee says:

    This method does not work in opera 10 or IE.

  11. Vivekanand says:

    @Stoimen:

    Yes! you are right! this is another scenario but could you please help me on the same.

    Thanks,
    Vivek

  12. @Weee: you are almost right. I had an issue with IE that when loading multiple images on the same time not all of them call the load event. Strange but I could not reproduce it with single image.

    Howsoever the method is very useful and after some testing you can see is it working in your case or not.

  13. Cenk SARI says:

    This works fine (IE,Chrome,Firefox) but not Safari. arghh!

  14. Weee says:

    this — is $(image) in this case (cuted from my code):

    if (this.complete || this.readyState === 4) {
        // do on load complete	
    } else {
        $(this).bind('load', function(){	
            // do on load complete	
        });
    }

    Works in safari, firefox, opera. No bugs with cached images.

  15. formadesign.ru says:

    This solution to Safari document ready bug:

    $(document).ready(function() {
        if (jQuery.browser.safari &amp;&amp; document.readyState != "complete") {
             setTimeout(arguments.callee, 100);
             return;
         }
     
        //code on page ready
    });
  16. teo says:

    Nice article, but the title is pretty misleading. You confuse “check whether” with “wait until”.

    Which is not just nit-picking, as in most cases you would need _both_ things.

  17. Sim says:

    It’s not working for me too. I am using latest jquery.

  18. DonutPanic says:

    @Weee: thanks for that. Works like charme!

  19. Alex says:

    I’ve got a plugin to handle this sort of stuff.

    It’s called waitForImages.

  20. Stoimen says:

    @Alex – thanx!

  21. @Alex,
    Great plug-in. With a little tweaking it performed exactly as I needed it to. Kudos!

  22. Worked perfectly. Thanks!

  23. The cross browser drawbacks of using .load() are already listed here: http://api.jquery.com/load-event/

  24. HB Connection says:

    the Code from Wee is very Simple and powerful

    i appreciate.
    thank you

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">