If you want to preload images before displaying them in the browser, a convenient way to do so is to use image.onload.
For example, the following code shows the onload in action:
img = new Image();
var onloadFunc = function() {
alert("got here... you really should do something now");
};
img.onload = onloadFunc;
img.src = url;
if (img.complete) {
onloadFunc();
}
That works in most situations, but I’ve just discovered IE6 is seriously buggy (so what’s new?) in its onload event handling. The specific problem is preloading an image from the body’s onload event (or any other method that fires after the page has loaded, such as a script at the bottom of the page, etc).
A morning of googling failed to find a solution, so for future-me’s reference, basically the best way to ensure images are preloaded at the end of a pageload is something like the following:
var timer = null;
var images = new Array();
// trigger this from the body's onload
function onPageLoad() {
images[0] = new Image();
images[0].src = 'url to some image';
images[1] = new Image();
images[1].src = 'url to another image';
timer = window.setInterval("onPageCheck()", 250);
}
function onPageCheck() {
for(var i = 0; i < images.length; i++) {
if (!images[i].complete) {
return;
}
}
window.clearInterval(timer);
images = null;
// now use the images
...
}
Basically, create an array of images (where the .src property triggers the preload), then use a timeout to check if the load is completed.
This appears to get around the problem. Note that the onload seems to work fine when triggered outside of the page onload (a button click and so on).

Thank you. That damn so-called-browser was driving me crazy.
Thanks a lot. I hope this works for us. *#@^ IE.
You know what really makes me mad, is that Gates tried to create a monopoly so other people couldn’t use windows. I bet IE7 still wouldn’t be supporting transparent png’s if it wasn’t for competition from Mozilla.
Thanks, nice peace of code.
The relatively new followup to Ms. Frontpage, “ExpressionWeb” very seldomly gives me the onpageload-option…
It has strange moodswings and prefers to offer the less reliable onmousemove option.
I dunno why we keep putting up with Ms/win, but by now I’m so used to it that it hardly annoys me anymore.
It is actually rather amusing to think of all the weird bugs and unexpected behaviors we win users has to learn to cope with….
A steady stream of basically useless knowledge reach us through necessity and relentless googeling, all due to our mutual quest for solving problems we never asked for in the first place!
.. The brotherhood of WIN I call us
–
Oh well, atleast I didn’t have to pay for a Mac