Again, this is more a note to myself, but I find the best function to preload images with jQuery is the following:

jQuery.preloadImages = function() {
     for(var i = 0; i<arguments.length; i++)
    {
          jQuery("<img>").attr("src", arguments[i]);
    }
}

This accepts X amount of arguments and can be called like the following:

$.preloadImages("/static/img/loadinganimation.gif", "/static/img/throbber.gif");

Why would you want to do this? Simple – if you’re using any “loading animations” on your site, with animated GIFs, you may find that on first activation on certain browsers the GIF is missing. If you preload it using a function like this one, your problem is solved.

Credit to this function goes to bloggingdeveloper.com.

Tags: , ,

One Response to “Simple jQuery Image Preloading”

  1. Jonas 30. Jul, 2011 at 3:57 PM #

    Nice preloading script…

    thanks

Leave a Reply

Spam protection by WP Captcha-Free