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];

jQuery.preLoadImages("image1.gif", "/path/to/image2.png");
  • 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.

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