Created
June 18, 2012 19:49
-
-
Save rmw/2950332 to your computer and use it in GitHub Desktop.
Showing a default image if an image doesn't exist in jQuery
This file contains 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
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment