Click to See Complete Forum and Search --> : How to tell if an image is loaded...


Skiracer901
06-18-2003, 05:43 PM
Does anyone know how to tell if an image is loaded? Thanks :)

Khalid Ali
06-18-2003, 05:46 PM
Once an image is loaded you can view it on the web page..:D

Skiracer901
06-18-2003, 05:50 PM
I mean how to tell if the image is loaded in javascript! I want to make a script that changes the image if it didn't load properly.

pyro
06-18-2003, 06:24 PM
How about something like this:

var img = new Image();
img.src = "http://www.yourdomain.com/images/img.png";
img.onerror = function () {
//swap the image
}

James Tripp
11-19-2007, 02:42 PM
There is an attribute 'complete' on Image() objects.


var img = new Image();
img.src = "http://www.yourdomain.com/images/img.png";

....

if ( img.complete ) {
//image has been downloaded now
} else {
//image has not been downloaded
}

ZeroKilled
11-19-2007, 06:39 PM
the property complete is propietary of msie only and doesn't work on firefox. using the attribute complete isn't recommended.

James Tripp
11-19-2007, 07:22 PM
Worked fine for me in FireFox, But not W3C Internet Standard

Looks like w3schools has done some testing on that as well

http://www.w3schools.com/htmldom/dom_obj_image.asp