Skip to content

Instantly share code, notes, and snippets.

@murtaugh
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save murtaugh/9586860 to your computer and use it in GitHub Desktop.
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).
$('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