Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check that url is a valid image source using Jquery?

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
like image 929
Mohit Jain Avatar asked Feb 22 '12 12:02

Mohit Jain


1 Answers

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");
like image 175
Pal Singh Avatar answered Sep 23 '22 15:09

Pal Singh