Last active
August 29, 2015 13:57
-
-
Save murtaugh/9586860 to your computer and use it in GitHub Desktop.
If an image fails, what do we do? In my case, maybe I've already defined a backup (usually in the case of inline SVGs), so we should look for that first. If that fails, then we'll hide the image (or do whatever the context requires).
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
$('img').on('error', function(){ | |
//console.log('img load failure: ' + $(this).attr('src')); | |
var fallback = $(this).attr('data-fallback'); | |
if (typeof fallback !== 'undefined' && fallback !== false) { | |
$(this).attr('src', fallback); | |
//console.log('missing img src replaced with: ' + fallback); | |
} else { | |
$(this).hide(); // a fairly blunt response. | |
//console.log('missing image hidden'); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment