Make your own free website on Tripod.com

Good Housekeeping: Organize Preloads in Arrays
If you have many images to preload, use this method for less typing and easier editing:

(1) Create an array to contain the src's of all images to be preloaded: (2) Itemize the src's, separated by commas, except for the last one, and close the array:

arImageSrc = new Array (
"imButdown.gif",
"imButup.gif",
"imButthis.jpg", ...
"../images/imButthat.jpg" )

Create a second array, the elements of which will be new Image objects

arImageList = new Array ();

Use for and in to perform the same task on every element in the src array. That is, for every image src that you previously listed, a new Image object will be created and its src property set automatically by the routine.

for (counter in arImageSrc) {
arImageList[counter] = new Image();
arImageList[counter].src = arImageSrc[counter];
}
If, when your page is modified, you want to insert or omit an image, just add or omit an src entry from the first array.

Code
arImageSrc = new Array (
"imButdown.gif",
"imButup.gif",
"imButthis.jpg",
.
.
.
"../images/imButthat.jpg"
)

arImageList = new Array ();

for (counter in arImageSrc) {
arImageList[counter] = new Image();
arImageList[counter].src = arImageSrc[counter];
}
Tips:
If all your images are the same type, if they are all gifs for instance, then the .gif extension can be omitted from the first array and the last line in the for loop could read:

arImageList[counter].src = arImageSrc[counter] + ".gif";


If all your images are found in the same path, omit the path from the first list and make this change:

arImageList[counter].src = pathstring + arImageSrc[counter] + ".gif";