onerror event handler in javascript code example
Example 1: oneerror javascript image
<img src="imagenotfound.gif" alt="Image not found" onerror="this.onerror=null;this.src='imagefound.gif';" />
Example 2: js catch all images errors
document.addEventListener('error', (e) => {
console.log('Document error triggered!')
if (!e.target) {
return console.log('Received event without target!')
}
const errorTarget = e.target
const imageErrorSource = errorTarget instanceof HTMLImageElement
if (!imageErrorSource) return false
return true
}, true)