Tuesday, September 16, 2014

Show a default image if one doesn’t exist!!!

Do you want to show default image if the image url doesn’t exist? Using jQuery it can be done. Please try the following code..

var img = $('img');
var default_url = "http://example.org/images/img.jpg";
var img_url = "http://example.org/images/" + some_variable + ".jpg";
img.error(function() {
  $(this).attr('src', default_url) 
});
img.attr('src', img_url);

No comments:

Post a Comment