[Javascript] image preloader doubts

Steve Clay sclay at ufl.edu
Thu Jul 20 09:36:17 CDT 2006


Thursday, July 20, 2006, 9:38:22 AM, suresh kumar wrote:
> different images for each ad,it leads to 200,so that i am asking whether
> image preloader function is sufficient to handle this task ,whether it

Unless their browser cache settings have been crippled, you needn't worry
about RAM.

Instead of preloading all images at the page load, another strategy is to
only preload the next image as needed. For example, during page load
preload the first 2 images. When the 2nd image is diplayed, begin
preloading the 3rd one, etc. To be a little safer you might stay 2 images
ahead: preload 3 at page load and preload the 4th when the 2nd is shown.

This will also save you a ton of bandwidth if many users load the page but
don't stay for all images to appear.

Here's a bare-bones single image preloader:

function loadingImage(src) {
  var img = new Image;
  img.src = src;
  return img;
}

// just load an image
loadingImage('http://example.com/jane.png');

// you can attach handlers to the load, error and abort events:
var john = loadingImage('http://example.com/john.png');
john.onload = function(){alert('john loaded.');};
john.onerror = john.onabort = function(){alert('john failed.');};

-- 
Steve
http://mrclay.org/




More information about the Javascript mailing list