12

I want to check if an image exists on the given URL using jquery. For example how do I check that an image exists on this url.

https://www.google.com/logos/2012/hertz-2011-hp.gif

but not on this url

 http://www.google.com
2
  • see there, there and there it will be useful Commented Feb 22, 2012 at 12:46
  • Here is what you may be looking for. Commented Feb 22, 2012 at 12:46

1 Answer 1

27
function IsValidImageUrl(url) {
$("<img>", {
    src: url,
    error: function() { alert(url + ': ' + false); },
    load: function() { alert(url + ': ' + true); }
});
}

IsValidImageUrl("https://www.google.com/logos/2012/hertz-2011-hp.gif");
IsValidImageUrl("http://google.com");
Sign up to request clarification or add additional context in comments.

2 Comments

Have you tested this? For some reason I am trying to use this code in chrome and every thing is coming back false
oh, what's happening is this load executes in a separate thread. I was trying to return true false from the error load methods which wouldn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.