The most common way to cache, or pre load images, is to write a script similar to the following:
img1= new Image();//-->
The script at the left caches six images into the computer's
memory. None of these images are displayed yet, they're just loaded from
the network for later use.
To cache an image you need to make a new Image() object.
img1= new Image();and then assign an image file to the newly created object.
img1.src="compa.gif";Notice that we only load the images if the bAnimate variable is TRUE. This way we avoid errors on older browsers.
The weird thing about caching images this way is that the new Image objects are not useable for other tasks--all they do is pre load (or cache) the images.
Another way to pre load the images is to literally put them some place on your document. If you don't want them to show you can always do this:
<IMG SRC="myimage.gif" WIDTH=0 HEIGHT=0>
This technique is not as good as the earlier method because it leaves a "nearly invisible" image on your web page and is subject to termination and errors, resulting in a very visible "image not found" error.
[Previous Lesson] [Tutorial Home Page] [Next Lesson]