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...

1
  • Check if the element exists in the DOM, or if the image is actually there, on the server ? Commented Aug 14, 2013 at 10:11

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.