Skip to content

Instantly share code, notes, and snippets.

@DanCouper
Created January 29, 2016 10:04
Show Gist options
  • Save DanCouper/4b84696317ce49aab272 to your computer and use it in GitHub Desktop.
Save DanCouper/4b84696317ce49aab272 to your computer and use it in GitHub Desktop.
Basic onerror handling for images
// Sadly, this doesn't work: img onerror events
// don't bubble, despite what the spec says:
// $(document).on('error', 'img', (e) => {
// So just do the following
// NOTE: depending on latency/loading time etc,
// there will be a FoUC, with the alt text flashing up:
$(document).on('ready page:load', () => {
// FIXME: naïve version, hits every image:
$('img').on('error', (e) => {
const fallBack = `data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7`
$(e.target).attr('src', fallBack)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment