-1

How check if file (image) exists in javascript using jquery

img = 'someimage.jpg'
$('<img>').attr('src',img);
isherwood
  • 58,414
  • 16
  • 114
  • 157
Vladimir Salguero
  • 5,609
  • 3
  • 42
  • 47
  • I found 14 similar threads using your search term. http://stackoverflow.com/questions/14651348/checking-if-image-does-exists-using-javascript – Cooper Buckingham Mar 17 '15 at 17:58
  • You can take a look here: https://gist.github.com/vdite/11369781 . An other idea is this: `` I have this from the link Tom Walters told. – Vinc199789 Mar 17 '15 at 17:59

1 Answers1

2

Attach an onerror event listener to that image.

img.onerror = function(){ alert("not found"); }

or use get request

$.get('someimage.jpg', function(data){ 

}).fail(function() {
    alert('not found'); 
});
A.B
  • 20,110
  • 3
  • 37
  • 71