Industry guru Dave Taylor offers free tech support on a wide variety of technical and business topics, including HTML, Apple iPhone, online advertising, Cascading Style Sheets, Web design, management, Unix, Linux, search engine optimization, online dating, Mac OS X, shell script programming and Microsoft Windows.

Can I display an "image not found" image in HTML?

I've been writing some scripts to display large directories of images in HTML in an automated fashion and not infrequently the static HTML generated ends up not matching the underlying images on the server. I could generate the pages on-demand, but it's too processor intensive. Instead, I'd like to know if there's a way I can automatically display an "image no longer available" graphic instead of just a broken link?


Dave's Answer:

Until just a few days ago, I would have said "I think you're out of luck" or would have suggested that a more efficient dynamic page generating CGI script would have been the way to go (after all, transforming a directory of images into rudimentary HTML to display those images isn't too difficult to program, in my experience) but now I do have an answer for you!

What's interesting is that the answer is from the nether region of markup where it's not truly JavaScript, but it's also not really HTML, it's an error handler script similar to the "onClick" and "onLoad" event handlers that you might already be familiar with (I talk about them at length in my book Creating Cool Web Sites with HTML, XHTML and CSS, but I never bumped into this particular event handler when I was researching the material so this is new stuff even for people who have that book).

The event handler is onError and apparently you can tie various JavaScript actions to a specific HTML tag, including the "img" tag, by using it in a savvy manner.

In this case, we'll want to tweak an IMG tag thusly:

<img src="badimage.gif" alt="Bad Image" onError="this.src='error.gif';" />

That's how it apparently works, so if you have 'error.gif' set up as an error image, perhaps saying something like "Image no longer available", you should be able to achieve the exact results you seek.

There are other places where it'd be interesting to experiment with onError too, for those of you wanting to hack around a bit. For example, put it in the BODY tag and you can pop up a window warning people that something went wrong loading the page and that they should check their error window and perhaps email the results to you so you can fix the problem. If you do experiment with this nifty attribute, please do post a comment so we can all learn what you can and can't do with it.

Tip of the hat to Rob Gonda for bringing the onError event handler to my attention. Thanks, Rob!


Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon    

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

I was wondering how come wehn I send e-mail to a particular address it tells me the mail box is full,and it's not. everyone can send the mail but me? all my other e-mail address work,and also it it the correct address?
Thanks dave

Posted by: KAREN at May 9, 2008 1:43 PM

This simple handler save us some lines of codes. THANKS! VERY HELPFUL!

Posted by: renjo dave at March 18, 2009 4:59 AM

Neat solution...
You do have to make sure that the image provided in the onerror handler does exist though, otherwise you get a "Stack overflow at line: 0" error (IE7)...

Posted by: marqis at May 1, 2009 2:27 AM

The "Stack overflow at line: 0" is coused by an infinite loop when 'error.gif' does not exist (it keeps on triggering a new onerror event).

To solve this replace the onerror handler with:

onerror="this.onerror=''; this.src='error.gif';"

Posted by: marqis at May 1, 2009 3:13 AM

I have something to say, now that you mention it, but ...
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you for all your efforts on this Web site by buying you a cup of coffee!

I do have a comment, now that you mention it!











Remember personal info?


Please note that I will never send you any unsolicited email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.








Ask Dave Taylor: The iPhone App: Advertisement



Follow me on Twitter @DaveTaylor

Search
Find just the answers you seek from among our 2300+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Book Links
© 2002 - 2010 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.