Check if image exists on server using 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');