Preloading Images: A trick to overcome delays in image-rich webpages loading

One of the things that can really slow down the display of Web pages is an abundance of images, each one of which can contain the equivalent of 17,000 to 20,000 characters.

There is a trick of Image Preloading to help overcome the delays experienced while image-rich documents load. Through the use of JavaScript, image files are loaded into image objects. The net result is that the graphics are not displayed but are loaded into the browser's cache for later use. When it is time for the browser to actually display the image(s), they are taken from the local cache instead of having to make the trip across the Internet.

The script embedded in the following document is an example of a preload script:

    "http://www.w3.org/TR/html4/strict.dtd">


   

    Preloading Images
   


   

Document body goes here.





The script builds an array of image paths and then iterates through them, creating an image object and assigning an src property to cause the image to actually be preloaded. The script can be run via two different means: by a function call in the , which causes the script to run before the document loads, or by an onLoad handler in the tag, which causes the script to run after the document loads.

Note:  Image preloaders aren't useful for individual documents; they are most useful for sites of multiple documents that reuse the same images over and over (buttons, rules, backgrounds, and so on). When seeding the loader, don't forget to include images from all documents your audience may see.

The former, before the document loads, is handy when the document itself contains many images — running the
preloader first can speed the display of the initial document. The latter, after the document loads, is better chosen when subsequent documents are the documents with the majority of images. This allows the initial document to load more quickly because it doesn't have to wait for the script to run — the document is displayed for the user to peruse while the script runs in the background.