Javascript Detect Broken Image
Posted by tech on
May 26, 2008
We have no way to know if an image is broken or not especially if we are just hot linking it from some other site. Javascript’s onerror function just does that. To add a no-image-file as replacement instead of the broken image icon (which would make your site look unprofessional), use this function below.
1 2 3 4 5 6 | function onImgError(source) { source.src = "no-image-125px.gif"; // disable onerror to prevent endless loop source.onerror = ""; return true; } |
and call it using this <img src="test.gif" onerror="onImgError(this)"/>
tip from: http://www.tech-evangelist.com/2007/09/15/missing-image-fix/








December 15th, 2008 at 10:54 am
Great tip!
Worked like a charm. I updated the method to take in the desired replacement image URL.
Thanks again.
March 19th, 2009 at 5:26 am
Great tip!
Amazed at the smoothness
March 19th, 2009 at 7:03 am
yer welcome
. whatever problems you have with javascript, you let me know.
July 23rd, 2009 at 2:50 am
Thats excellent, such an elegant solution. Many thanks.
July 23rd, 2009 at 2:54 am
thanks