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.

7 comments:

  1. I have been tearing my hair out for two nights because of this bug. Thankyou SO much. Check out what I've been working on here: http://puremango.co.uk/mask/
    I've given you credit in the code. thanks again.

    ReplyDelete
  2. Thank you!
    Excellent decision? it very helps me!

    ReplyDelete
  3. How to put the progress bar while loading markers in the google map?
    thx

    ReplyDelete
  4. Almost good enough...
    What if I cant set the handler before setting the src?
    Is there a property like 'isLoaded' or something to figure out if the image has already loaded?

    ReplyDelete
  5. I have been tearing my hair out for two nights because of this bug. Thankyou SO much. Check out what I’ve been bez torba working on here:

    ReplyDelete