jQuery Preload images – tutorial and example

Written by on February 14, 2010 in General - 16 Comments

Not so long ago i created jQuery Image Resize Plugin, today i want to show you another plugin: jQuery Image Preloader. Basically it is a script that loads images in the background and track number of loaded images. It’s very simple, only 25 lines of code, but maybe useful if you are creating jQuery image gallery or some other script that requires image preloading.

Lets start of with how it works. As i understand it, it’s not actually a plugin but rather jQuery extension. It means that it’s not applied to HTML elements but rather used as function. Here’s how you can use it.

$(function() {
    $.preload([
        "http://farm3.static.flickr.com/2661/3792282714_90584b41d5_b.jpg",
        "http://farm2.static.flickr.com/1266/1402810863_d41f360b2e_o.jpg"
    ], {
        init: function(loaded, total) {
            $("#indicator").html("Loaded: "+loaded+"/"+total);
        },
        loaded: function(img, loaded, total) {
            $("#indicator").html("Loaded: "+loaded+"/"+total);
            $("#full-screen").append(img);
        },
        loaded_all: function(loaded, total) {
            $("#indicator").html("Loaded: "+loaded+"/"+total+". Done!");
        }
    });
});

$.preload() function takes two parameters. First is an array of urls that will be loaded. Second is an object that can have up to three callbacks:

  • init() – executed before the first loading images start
  • loaded() – executed when image is loaded (thus if there are 10 images then this callback is executed 10 times, for each image once)
  • loaded_all() – executed when all images are loaded

You can see how it works here. That’s an extremly simple example but will allow you to easier grasp how to modify it to your needs.

About the Author

Greg Winiarski is a freelance PHP and JavaScript programmer. He specializes in web applications and WordPress development.

16 Comments on "jQuery Preload images – tutorial and example"

  1. imwrong February 17, 2010 at 10:08 pm ·

    Hi there. Would it be possible to embed this to a WordPress single page theme (single.php), using custom fields to get the image url?

    It can’t be that difficult but I’m afraid I have no programming experience to do it myself.

    It would really be very helpful as a wp plugin for those on a slow shared server.

  2. web design March 12, 2010 at 5:54 am ·

    Although I can not understand many of them,i know you do a very good job.

  3. Daniel July 25, 2010 at 8:06 pm ·

    Argh, putting each filename directly in the Javascript? Too tedious and won’t work when you want to use the same script on many pages. Separation of content and presentation applies to JS as well as CSS.

  4. Weblap.ro July 31, 2010 at 9:26 pm ·

    Thanks dude!

  5. Jennifer R August 13, 2010 at 3:37 pm ·

    Could I remove # init()# loaded() # loaded_all() from the code above?

  6. Greg Winiarski August 13, 2010 at 7:57 pm ·

    Well yes, but that’s the only interface to check what has been loaded and what not.

  7. Bart W August 16, 2010 at 7:35 pm ·

    How would you get the script to load the array in order, say if you had a slideshow to preload that needed to display in exact order. Thanks.

  8. Glenn August 19, 2010 at 7:45 am ·

    Hi, it doesn’t seem to work on IE7 when you refresh the page.

    Would you know the reason why?
    Thanks!

  9. Glenn August 19, 2010 at 9:09 am ·

    Founds the solution. I just had to call the load first before setting the attr.

    imgList.push($(“”) .load(function() { loaded++; setting.loaded(this, loaded, total); if(loaded == total) { setting.loaded_all(loaded, total); } }).attr(“src”, imgArr[i]) );

  10. Jason Kirst September 22, 2010 at 3:22 am ·

    Ditto on Glenn’s issue on IE7. His fix worked for me. Thank you Greg and thank you Glenn!

  11. dario December 7, 2010 at 9:59 pm ·

    I also had to change this line:
    for(var i in imgArr) {
    to this:
    for (i = 0; i < imgArr.length; i++) {

    for it work on IE7.

  12. Nakul December 9, 2010 at 8:18 am ·

    How to handle error if the image is not loaded?

  13. Annika February 8, 2011 at 4:00 pm ·

    Hi,
    I don’t get this to work, how I integrate it on my page?

    Just trying this function and would like to preload the pictures and have a progress picture spinning

    thanks

  14. Mihir March 11, 2011 at 9:36 am ·

    Thank you Greg for nice script!
    Thank all for providing IE fixes.

  15. Freddy March 11, 2011 at 1:40 pm ·

    Thanks for this tutorial, very useful for interactive websites! And thank you both Glenn and dario for your ie fix!

  16. Hakim April 10, 2011 at 3:57 am ·

    Thanks for this script mate. In combination with a sequential fadein function this look really great!

    Would it be possible somehow to avoid having to write out every single file name? Current script is great if you’re just using it on one page, but if used on several it requires several copies targeting images on those individual pages.

    Thanks again for the great script and tutorial :)

Leave a Comment