|
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? 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!
Categorized:
HTML and CSS
(Article 8018,
Written by Dave Taylor)
Tagged: css, event handlers, html, javascript, web page design Previous: How can I receive faxes online for free? Next: How do I narrow my search results on LinkedIn? Subscribe!
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? This simple handler save us some lines of codes. THANKS! VERY HELPFUL! Posted by: renjo dave at March 18, 2009 4:59 AMNeat solution... 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';" I have something to say, now that you mention it, but ...
I do have a comment, now that you mention it!
|
Recommended
Recent Entries
Search
I Need Help!
Apple iPad Help
Articles and Reviews Auctions and Online Shopping Blogs and RSS Feeds Building Web Site Traffic Business and Management CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above Facebook Help Google Plus Help HTML and CSS Industry News and Trade Shows iPhone and Cell Phone Help iPod, Sony PSP and MP3 Player Help Mac OS X Help Pay Per Click (PPC) Advertising Search Engine Optimization (SEO) Shell Script Programming Tech Support Video Help The Writing Business Twitter, LinkedIn and Social Network Help Unix and Linux Help Video Game Tips and Help Windows PC Help WordPress Help |