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';" />


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:

Leave a Reply

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