Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

How to know when images are finished loading

Hi,

I would like to know exactly when an image is totally loaded. I need this because I have to test the image width with javascript. I tried image.onLoad but it's fired when the tag is loaded, not the entire image.

So for result, if the image is, let's say, 5 of width, it returns 28 with IE and 23 with FF. But if I put a delay, the image width is correct. So, I understand that the onload event is not working as I'd expect.

So, for now, what I do is that I set a timeout of 500 milliseconds and then call my function. It's working, but not very good I think...

Here's my code:

var ctrlImg = document.getElementById('ctrlImg');
ctrlImg.src = "/getNb.aspx";
setTimeout("setValue()",500);

Is there a way to know for sure when an image is loaded?

Thanks,

Stephane
[879 byte] By [steph2004] at [2007-11-11 8:11:33]
# 1 Re: How to know when images are finished loading
Yes -- code an event handler for the "onreadystatechange" event. No need for timeouts.

For details, check this search result:
http://groups.google.com/groups?q=onreadystatechange&hl=en
bschaettle at 2007-11-11 23:34:53 >