Snippets

Preloading Images with jQuery and JavaScript

Function to preload images.

$function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }

jQuery.preLoadImages("image1.gif", "/path/to/image2.png");

Source: http://engineeredweb.com/blog/09/12/preloading-images-jquery...

Share this snippet

 
  • http://ChiefAlchemist.com Mark Simchock

    I like the Snippets Library idea. But as someone who’s learning (always) it would help if they were fully commented. Yes, to the fully experienced most things are obvious. But what about when you’re lower on the learning? Or just don’t have the wherewithal to dig in a sort something out.

  • http://blog.realhe.ro Fedek6

    Not complete code in your post. It should be wrapped in: (function($) { … })(jQuery)