Created
January 29, 2016 10:04
-
-
Save DanCouper/4b84696317ce49aab272 to your computer and use it in GitHub Desktop.
Basic onerror handling for images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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