0

Possible Duplicate:
How do I check if file exists in jQuery or Javascript?

I need to find out image exists at specific path on server using javascript.

If the image exists at specific path on server in that case I need to display a default image without showing an error.

Thank you very much for your help.

Community
  • 1
  • 1
Madu
  • 21
  • 1
  • 5
  • You can use the alt property in the image tag to display a little text if the image cannot be displayed. – Pablo Mescher Jul 26 '12 at 05:21
  • I used img.height to detect the height and find whethere it exists in the server. But when I refresh all the other images were taken as not exists and all the images were replaced with the default image. – Madu Jul 26 '12 at 05:25

1 Answers1

3

You can send an Ajax request for that image. I'll use jQuery for simplicity

var request = $.ajax("http://www.goggle.com/fdsf.png")​​​​​​​​​​​​​;

request.done(function(msg) {
  console.log("Exists");
});

request.fail(function(jqXHR, textStatus) {
  // You check the jqXHR status object for the HTTP status so you can know 
  // why it failed
  console.log("Doesn't");
});​
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217