Sunday, September 18, 2005

onload event

You can use the onload event on images to know when the browser has finished the download of an image.

I have used it for the upcoming version of Panoramio, where I only show the images after they have been fully loaded.

There is however a subtle difference between Internet Explorer and Firefox related to this event.

If you set up the handler to this event after setting the src of the image and the image is in the cache, Internet Explorer will not fire the onload event.

Internet Explorer fires this event inmediatelly if the image is in the cache, so you should do:


img.onload = onLoadHandler;
img.src = "foo.png";


instead of


img.src = "foo.png";
img.onload = onLoadHandler;


or your code will not work on Internet Explorer for cached images.