1

I'm sure you've seen this question many times, but i will asure you that this is different. I want to know how to check if an image exists using the path to the image with a url variable. I would like something like this:

if ($('url').length ) {
    $(".mainContent").html("File exists");
} 
else
    $(".mainContent").html("File does not exists");`

Hope you can help me soon...

putvande
  • 15,068
  • 3
  • 34
  • 50
user2681824
  • 19
  • 1
  • 2

1 Answers1

0

For images that might exist i find most elegant solution is useing $ajax, like:

$.ajax({
    url: 'your_image.jpg',
    type: "POST",
    dataType: "image",
    success: function() {
        /* function if image exists (setting it in div or smthg.)*/
    },
    error: function(){
       /* function if image doesn't exist like hideing div or setting default image*/


      } 

})

But just to be warned some people insist on useing GET instead of POST but I didn't have any problems and it works like a charm

Ivan Pavić
  • 528
  • 4
  • 22