View Full Version : Easy preload image

12-27-2006, 10:34 PM
Ok, I'm sure this will only take half a second. I remember seeing this somewhere. What I want is to load "pictures/navbarbg2.jpg" in javascript. I have a image swap on a picture, and when I hover over one picture, there is a blink where the image is loading. If I have javascript put the hover image on cache, I think that will remove the blink.

12-28-2006, 12:05 AM
there're about a million scripts for this, but here's mine:

function preLoad(args){
if (document.images){
var img = new Image();
var failed = [];
img.onerror = function(){
failed[failed.length] = this.src;
if(args.length > 0){ this.src = args.shift(); }
img.onload = function(){
if(args.length > 0){ this.src = args.shift(); }
img.src = args.shift();
else { var failed = args; }
return failed;

use it like this:

preLoad(["preloading.jpg", "multiple.png", "files.gif"]);

12-28-2006, 06:40 AM
Well, it seemed to work (always hard to tell). Thanks a lot.