onerror event using background: url()

In case myimage.gif isn’t transparent, you could use multiple backgrounds: background: url(‘myimage.gif’), url(‘fallback.gif’); This way fallback.gif will only be visible if myimage.gif isn’t available. Note that fallback.gif may be downloaded even if myimage.gif is available. Alternatively, even though not widely supported, CSS Images 3 introduces the image() notation: background: image(‘myimage.gif’, ‘fallback.gif’); Multiple <image-decl>s can be …

Read more

How does one use the onerror attribute of an img element

This works: <img src=”https://stackoverflow.com/questions/8124866/invalid_link” onerror=”this.onerror=null;this.src=”https://placeimg.com/200/300/animals”;” > Live demo: http://jsfiddle.net/oLqfxjoz/ As Nikola pointed out in the comment below, in case the backup URL is invalid as well, some browsers will trigger the “error” event again which will result in an infinite loop. We can guard against this by simply nullifying the “error” handler via this.onerror=null;.

How to tell if a tag failed to load

UPDATE 2021: All browsers today support onerror=”” on script tags, examples: Building script tag in js on MDN Html example by @Rudey in comments: <script src=”nonexistent.js” onerror=”alert(‘error!’)”></script> Original comment from 2010: If you only care about html5 browsers you can use error event. From the spec: If the src attribute’s value is the empty string …

Read more