Javascript preload images script

103

This post teaches you the proper efficient way to preload images with javascript.

if (document.images)
{
preload_image = new Image();
img_path = new Array();

img_path[0] = “http://kilroylives.com/images/buttons/avatar.gif”;
img_path[1] = “http://kilroylives.com/images/buttons/avatar-over.gif”;
img_path[2] = “http://kilroylives.com/images/buttons/enlarge.gif”;
img_path[3] = “http://kilroylives.com/images/buttons/enlarge-over.gif”;
img_path[4] = “http://kilroylives.com/images/buttons/favorite.gif”;

for(var i = 0; i<=img_path.length; i++)
preload_image.src = img_path[i];
}

All you need to do is add more img_path[x] = “”; line for each image to preload. This is very usual when using CSS text links with background images.

Article written by developer and designer


3 comments ↓

If you found this post useful click the share this button. Contribute below by adding a comment, no registration is required.

Leave a Comment