JavaScript: Image onerror Fallback

Published on Author craigzearfossLeave a comment

Without a JavaScript function

<img src="image.png" onError="this.onerror=null;this.src='/images/noimage.gif';" />

or

With a JavaScript function

function imgError(image) {
    image.onerror = "";
    image.src = "/images/noimage.gif";
    return true;
}

<img src="image.png" onerror="imgError(this);"/>

For a compatibility lists of browsers that support the error facility:
http://www.quirksmode.org/dom/events/error.html

Leave a Reply

Your email address will not be published. Required fields are marked *