Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.