Image Exist Control
by XcsN
HTML
<div id="image-test"></div>
CSS
#image-test {
width: 300px;
height: 300px;
border: 1px solid #CCC;
}
JavaScript
function checkImage(src)
{
var img = new Image();
img.onload = function() {
// code to set the src on success
$('#image-test').css('background', 'url(' + src + ') no-repeat 50% 50%');
};
img.onerror = function() {
// doesn't exist or error loading
alert('no image');
};
img.src = src; // fires off loading of image
}
checkImage('http://www.google.com/images/logo_sm.gif');
// uncomment to test error function
//checkImage('http://www.google.com/images/logo_no_exist.gif');