Skip to content

Instantly share code, notes, and snippets.

@joelremix
Created September 23, 2016 18:54
Show Gist options
  • Save joelremix/facaf21800983dbdcb9e37f22304b65c to your computer and use it in GitHub Desktop.
Save joelremix/facaf21800983dbdcb9e37f22304b65c to your computer and use it in GitHub Desktop.
jQuery: Check Broken Images
$(document).ready(function(){
// Looping through all image elements
$("img").each( function () {
var element = $(this);
$.ajax({
url:$(this).attr('src'),
type:'get',
async: false,
error:function(response){
var replace_src = "images/noimage.png";
// Again check the default image
$.ajax({
url: replace_src,
type:'get',
async: false,
success: function(){
$(element).attr('src', replace_src);
},
error:function(response){
$(element).hide();
}
});
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment