/    Sign up×
Community /Pin to ProfileBookmark

Detect image without onerror

Is there a way to use JavaScript to see if an image is available at another URL without using the onerror?

I would use use the onerror however, a version of Safari that I have to support does not have this available.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@TheBearMayMar 06.2008 — Not really, but if the image is greater than 30 pixels high this might work:
<i>
</i> window.onload=function (){
var pic1= new Image();
pic1.src="http://www.yourURL.com/myImage.jpg";
if ((pic1.height == 30 &amp;&amp; navigator.appName == "Microsoft Internet Explorer") || pic1.height == 0)// IE 30 is default, FF 0
alert("Possible missing image");
else alert ("Image height found: "+pic1.height);
}


So why 30, and why the onload? IE defaults the new Image() function to 30 pixels unless you pass it a parameter or load a valid image (FF defaults to zero as I would expect). The onload, haven't figured out why, but unless I execute it after the page loads I was getting an intermittent false positive.
Copy linkTweet thisAlerts:
@sbriouxauthorMar 07.2008 — Anyone know how reliable the image.width or image.height is in all browsers?
×

Success!

Help @sbrioux 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 6.18,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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