I think it is very nice and short
<img src="imagenotfound.gif" alt="Image not found" onerror="this.src='imagefound.gif';" />
But be careful. The custom browser will be stuck in an infinite loop if the onerror object itself generates an error.
EDIT To avoid an infinite loop, immediately remove onerror .
<img src="imagenotfound.gif" alt="Image not found" onerror="this.onerror=null;this.src='imagefound.gif';" />
By calling this.onerror=null , it removes onerror and then tries to get an alternate image.
NEW I would like to add a jQuery way if this can help someone.
<script> $(document).ready(function() { $(".backup_picture").on("error", function(){ $(this).attr('src', './images/nopicture.png'); }); }); </script> <img class='backup_picture' src='./images/nonexistent_image_file.png' />
You just need to add class = 'backup_picture' to any img tag that you want to load the backup image if it is trying to show a bad image.
Etdashou Mar 27 '12 at 13:57 2012-03-27 13:57
source share