Library code snippets
Detect missing images with client-side script
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>
Related articles
Related discussion
-
An old chestnut: Scrolling DIVs. Can I use onmouseover/onmouseout?
by Skunk (1 replies)
-
code highlighting using jquery
by pjm (1 replies)
-
Problem when using TemplateField and ImageButton
by mhuff84 (14 replies)
-
dropdownlist for country,state and city
by jack_tom (2 replies)
-
popup window in vb.net... how it's work?..
by yf2009 (6 replies)
Related podcasts
-
Raleigh Code Camp reflections, Chrome wins speed tests, GroovyMag
I attended the Raleigh Code Camp last week and had a blast meeting some awesome people (but of course there's never enough time to meet everyone you'd like to!). Some great sessions I attended covered the MS MVC platform and an introduction to MS DLR, both of which I discuss here in a bit more d...
Events coming up
-
Oct
28
Web Standards Group (Sydney)
North Sydney, Australia
TBA
This method is available in Microsoft Internet Explorer only - for Netscape compatible browsers (anything on the Mozilla code-base such as Netscape, Mozilla Firebird, etc.) wait until the image is loaded and check the images' .naturalWidth and .naturalHeight properties. These reflect the actual dimensions of the image regardless of any resizing done by the browser, and will be 0 if a null (nonexistent) image is holding the place.
But then, these methods are only available in the Mozilla base.
Very nice article which resolved my problem
This thread is for discussions of Detect missing images with client-side script.