/    Sign up×
Community /Pin to ProfileBookmark

How to tell if an image is loaded…

Does anyone know how to tell if an image is loaded? Thanks ?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliJun 18.2003 — Once an image is loaded you can view it on the web page..?
Copy linkTweet thisAlerts:
@Skiracer901authorJun 18.2003 — 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.
Copy linkTweet thisAlerts:
@pyroJun 18.2003 — How about something like this:

var img = new Image();
img.src = "http://www.yourdomain.com/images/img.png";
img.onerror = function () {
//swap the image
}
Copy linkTweet thisAlerts:
@James_TrippNov 19.2007 — 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

}
Copy linkTweet thisAlerts:
@ZeroKilledNov 20.2007 — the property complete is propietary of msie only and doesn't work on firefox. using the attribute complete isn't recommended.
Copy linkTweet thisAlerts:
@James_TrippNov 20.2007 — 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
×

Success!

Help @Skiracer901 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...