In previous tips, we've shown you how to determine whether a file exists
programmatically by using either the MSWC.Tools object or the
Scripting.FileSystemObject object. Both have their place, but what if you need
to make this determination in DHTML on the client side? A good example of this
situation is when you need to ascertain whether an image exists before
displaying it. Nobody likes seeing broken image links, but how can you trap this
condition programmatically? In DHTML, the <img> tag fires an OnError
event anytime the image cannot be loaded. This can occur either because the src
attribute of the <img> element points to a file that doesn't exist,
because of a timeout, or because of another error. Detecting the problem,
however, allows you to take some action before the element is rendered.
You could substitute another image, change the value of the image's alt
attribute, or (as shown below) simply prevent the element from being rendered at
all.<html>
<head>
<script language="JavaScript">
function ImageLoadFailed() {
window.event.srcElement.style.display
= "None";
}
</script>
</head>
<body>
<img
src="http://localhost/images/nonexistingimage.gif"
OnError="ImageLoadFailed()">
</body>
</html>
Detect missing images with client-side script
By James Crowley, published on 18 Jul 2001
| Filed in
You might also like...
JavaScript forum discussion
-
How to stop & start the scrolling text on mouseover & onmouseout event.
by codeunit (3 replies)
-
Header and Footer in Web page print
by fhajaj (4 replies)
-
Automatically check/uncheck multiple checkboxes
by gus (3 replies)
-
Re: HTML TABLE
by mikefenej (0 replies)
-
Re: HTML TABLE
by mikefenej (0 replies)
JavaScript podcasts
-
Herding Code: Herding Code 162: Whacha doin, Goodbye Google Reader, scriptcs and Lightning Round!
Published 7 years ago, running time 0h35m
This week on Herding Code, the guys talk about what they’ve been up to lately (including Kevin’s new Greater Than Parts site), lament the passing of Google Reader, talk about scriptcs, and even fit in a lightning round! Download / Listen: Herding Code 162: Whacha doin, Goodbye Google Reader, scri.
JavaScript jobs
-
UI Designer
TCS ( Tata Consultancy Services) in London, United Kingdom
£200-250 per year (Contract) -
Front End developer
DIA in London, Hammersmith, United Kingdom
£20,000-35,000 GBP per year -
PHP Developer
Storm Creative Media Limited in Leigh-on-Sea, United Kingdom
Negotiable -
Front end, UX & UI developer
Stratajet Ltd in London, United Kingdom
Negotiable, based on experience
Comments