HTML OnMissingImage
Scenario: you need to display a bunch of images but you're not 100% certain that they exist.
Solution 1: If the images are hosted in the same server you can loop over their physical path display an alternate image if the image was not found on disk. This solution is very taxing (I/O intensive) and not efficient at all. It also won't work if you're not hosting the images.
Solution 2: What now? I found a few JavaScripts to loop over every image found in the DOM and check of http status, then replace them if the image wasn't loaded properly. This solution is much better because it's decentralized and the load is delegated to the client as opposed of the server, and it also works with images hosted elsewhere.
Solution 3 (winner): Did you know that the image tags have an onError attribute? I can't believe that after 15 years of web development I just found out about this now :) ... This is the optimal solution as it is native to the browser, works on every browser (I tested IE6+, FF2.0+, Safari1.3+, all on Mac and PC).
Check it out, I included two images: img01.gif, which exists on my server, and img02.gif. Both of them have an onerror attribute with a simple script: this.src=error.gif.
img02.gif does not exist on my server, consequently the script will be executed and load my error.gif

So simple and yet so powerful. Enjoy.
Solution 1: If the images are hosted in the same server you can loop over their physical path display an alternate image if the image was not found on disk. This solution is very taxing (I/O intensive) and not efficient at all. It also won't work if you're not hosting the images.
Solution 2: What now? I found a few JavaScripts to loop over every image found in the DOM and check of http status, then replace them if the image wasn't loaded properly. This solution is much better because it's decentralized and the load is delegated to the client as opposed of the server, and it also works with images hosted elsewhere.
Solution 3 (winner): Did you know that the image tags have an onError attribute? I can't believe that after 15 years of web development I just found out about this now :) ... This is the optimal solution as it is native to the browser, works on every browser (I tested IE6+, FF2.0+, Safari1.3+, all on Mac and PC).
Check it out, I included two images: img01.gif, which exists on my server, and img02.gif. Both of them have an onerror attribute with a simple script: this.src=error.gif.
img02.gif does not exist on my server, consequently the script will be executed and load my error.gif
<img src="img01.gif" width="100" height="100" onerror="this.src='error.gif';">
<br clear="all">
<img src="img02.gif" width="100" height="100" onerror="this.src='error.gif';">
<br clear="all">
<img src="img02.gif" width="100" height="100" onerror="this.src='error.gif';">

So simple and yet so powerful. Enjoy.
TrackBacks
There are no trackbacks for this entry.
Trackback URL for this entry:
http://www.robgonda.com/blog/trackback.cfm?6F36F871-B332-AF2B-5E6B823579B1F028
http://www.robgonda.com/blog/trackback.cfm?6F36F871-B332-AF2B-5E6B823579B1F028









@Ray, w3c would be the place, but like Todd mentioned, it seems like it's not explicitly mentioned there. However, I found this reference to it at http://www.w3schools.com/jsref/jsref_onError.asp
Also, if you disable white space suppression on the server it will make your comment emails a bit nicer - or - and this is NOT checked in to blogcfc yet, wrap the comment text with a <cfprocdir> that disables whitespace supression.
It is a small thing, but boy does it makes the comment emails easier to read.
http://cfsilence.com/blog/client/index.cfm/2008/4/...
I spent part of last friday puzzling over a very slow "cfhttp" error-checking process that I developed for a site where we have the (vehicle listing) data in-hand, but no indication how many images each one has in the data (or if it has one at all) and all of the images are hosted remotely. My next step was going to be a nightly massive loop / check / copy process for a gazillion jpgs and thumbnails, and even then I would have to check for the file existing on our own server after they'd all been copied over... this method will be MUCH better!
Like somebody else said in an earlier comment, it makes me wonder what else lurks in the html spec. How come we don't all know about and use this all the time? ( we will now!! )
Thanks Rob!
@Ray, I don't mind it at all ... we usually use your blog for such threads, so it's nice to use mine for once :)
http://www.w3schools.com/jsref/jsref_onError.asp