Javascript Detect Broken Image

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.

?View Code JAVASCRIPT
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/

Found this post useful? Buy me a cup of coffee or help support the sponsors on the right.

Related Posts with Thumbnails