Why Preload?
The main reason you want to preload images is when they are being used for mouse-over or roll-over effects. If they're not displayed by the page when it loads then the page will wait until the image is needed to load it. However, this can result in a delay to the mouse-over/rollover effect while the image loads. In steps the preload.
Before we dig into how this works, I'll provide the finished code for those who just want to copy and paste. As always, click the "PLAIN TEXT" above the code to view it in a copy & paste friendly format. For those who want to know how it works, read on.
-
//This function will cycle through the directory it is sent
-
//$x is used to count the number of images, we set it to 0 by default
-
function preloadImages($directory, $x = 0) {
-
//first, lets make sure the user sent a directory. If they didn't, return "false"
-
// if the path has a slash at the end we remove it here
-
}
-
// if the path is not valid or is not a directory ...
-
// ... we return false and exit the function
-
return FALSE;
-
// ... if the path is not readable
-
// ... we return false and exit the function
-
return FALSE;
-
// ... else if the path is readable
-
} else {
-
// we open the directory
-
// and scan through the items inside
-
// if the filepointer is not the current directory
-
// or the parent directory
-
if($item != '.' && $item != '..') {
-
// we build the new path to access
-
$path = $directory.'/'.$item;
-
// if the new path is a directory
-
// we call this function with the new path, and the current counter
-
preloadImages($path, $x);
-
// if the new path is a file
-
} else {
-
// we get the file info
-
//and output the javascript code
-
$x++;
-
}
-
}
-
}
-
// close the directory
-
-
// return success
-
return TRUE;
-
}
-
}
To implement it, place it in the head section of your document like so:
-
<script language="JavaScript">
-
<!--
-
if (document.images)
-
{
-
<?php preloadImages("images") or die ("Unable to preload images"); ?>
-
-
}
-
//-->
-
</script>
images is the location of the images directory (in this case, its at the same level as the file that the javascript code is in). You may have to change this to match yours.
Note: This assumes that all files in the directory and sub-directories are images
Now lets go through the code and analyze whats happening.
Pages: 1 2


















Hi
As a fresh http://www.ryanprice.ca user i only want to say hi to everyone else who uses this forum
Hey Ryan. This is EXCELLENT! Thanks so much.
This runs perfectly in Firefox and Safari… However (there’s always a however), it doesn’t work at all in IE7/IE6.
I tried a similar script to yours that didn’t look at the directory, but you would list all of the images in a row.
I have over 400 images, so your script MUST work for me!
From what I can tell IE doesn’t like the Image() object.
Is there a way around this? Thanks!
Hi Creative,
Do you have a work-in-progress online I could look at? Hard to tell what the problem is without seeing it first.