jQuery/JavaScript to replace broken images

Handle the onError event for the image to reassign its source using JavaScript:

function imgError(image) {
    image.onerror = "";
    image.src = "https://stackoverflow.com/images/noimage.gif";
    return true;
}
<img src="https://stackoverflow.com/questions/92720/image.png" onerror="imgError(this);"/>

Or without a JavaScript function:

<img src="https://stackoverflow.com/questions/92720/image.png" onError="this.onerror=null;this.src="https://stackoverflow.com/images/noimage.gif";" />

The following compatibility table lists the browsers that support the error facility:

http://www.quirksmode.org/dom/events/error.html

Leave a Comment