1

I want to preload images but ensure they are loaded before continuing. How can I do this?

The following does not work as it sends off the load request only, but doesn't wait till the image is loaded. So it is possible that the image isn't loaded when requested soon after.

jQuery.preloadImages = function () {
        for (var i = 0; i < arguments.length; i++) {
            jQuery("<img>").attr("src", arguments[i]);
        }
    }       
    $.preloadImages("img1.jpg","img2.jpg");
Mr. Flibble
  • 26,564
  • 23
  • 69
  • 100

1 Answers1

0
jQuery.preloadImages = function () {
  var image = new Image();

  for (var i = 0; i < arguments.length; i++) {
      image.src = arguments[i+1];
  }
}

I haven't tested.

TK.
  • 27,073
  • 20
  • 64
  • 72
  • that does the same thing - it doesn't load the image immediately. you would use image.onload = function(){...} to use the image when ready, but I need a blocking way of doing it. – Mr. Flibble May 21 '10 at 17:35