Auto-Replace Broken Images

/ Published in: jQuery
Save to your folder(s)

There are 2 jQuery snippets, both replace broken images with one of your choosing, the difference is that one is "safe" and the other is "persistent". The safe one will quick working, so if your doing something that dynamically changed images on a page it'll only work for the first broken image. The persistent one will keep working, but if your missing image image is missing it'll cause a overflow error.

Copy this code and paste it in your HTML
  1. // Safe Snippet
  2. $("img").error(function () {
  3. $(this).unbind("error").attr("src", "missing_image.gif");
  4. });
  6. // Persistent Snipper
  7. $("img").error(function () {
  8. $(this).attr("src", "missing_image.gif");
  9. });


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.