Hello,
I am using the "new image" functionality in javascript to have the client load an image. I need a way to tell when the image is loaded, does anyone have a solution to this problem?
Thanks!
| SitePoint Sponsor |

Hello,
I am using the "new image" functionality in javascript to have the client load an image. I need a way to tell when the image is loaded, does anyone have a solution to this problem?
Thanks!

Alright, I figure I can just do the following to see if it is loaded yet:
var image = new Image;
image.src = '...';
if (image.width > 0) {
....
But, here is the problem
When I do that it appears that I only get the images width if the image is cached. I have even tried adding a long delay (two seconds... I am on localhost, so that should be plenty) and it seems like it only works when it is cached. Does anyone have an idea of how I can tell when it is loaded so I can get its dimensions (the dimensions is what I need).

Does this help?
Code:var image = new Image; image.src = 'http://www.sitepointforums.com/images/sitepoint-logo.gif'; setTimeout('checkImg()',1000); function checkImg(){ if (image.complete) { alert("image downloaded\nWidth: "+image.width+"\nHeight: "+image.height); }else { alert("Image not yet loaded, waiting 2 seconds.."); setTimeout('checkImg()',2000); } }


Shoundn't
new Image ;
be
new Image();
Bookmarks