我想使用jquery检查给定URL上是否存在图像。例如,如何检查图像是否存在于此网址上。如何检查该网址是使用Jquery的有效图像源?
https://www.google.com/logos/2012/hertz-2011-hp.gif
但不是在这个网址
http://www.google.com
我想使用jquery检查给定URL上是否存在图像。例如,如何检查图像是否存在于此网址上。如何检查该网址是使用Jquery的有效图像源?
https://www.google.com/logos/2012/hertz-2011-hp.gif
但不是在这个网址
http://www.google.com
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");
参见[有](http://stackoverflow.com/questions/169625/regex-to-check-if-valid-url-在-jpg-png-gif中),[there](http://stackoverflow.com/questions/3381663/check-if-image-exists-with-given-url-using-jquery)和[有](http://stackoverflow.com/questions/4669111/is-this-a-valid-test-to-check-if-a-url-refers-to-an-image-in-js-jquery)这将是有用的 – komelgman 2012-02-22 12:46:27
[Here](http://stackoverflow.com/questions/3646914/how-do-i-check-if-file-exists-in-jquery-or-javascrip点)是你可能要找的。 – buster 2012-02-22 12:46:46