Checking if image does exists using javascript [duplicate]
// The "callback" argument is called with either true or false
// depending on whether the image at "url" exists or not.
function imageExists(url, callback) {
var img = new Image();
img.onload = function() { callback(true); };
img.onerror = function() { callback(false); };
img.src = url;
}
// Sample usage
var imageUrl = 'http://www.google.com/images/srpr/nav_logo14.png';
imageExists(imageUrl, function(exists) {
console.log('RESULT: url=' + imageUrl + ', exists=' + exists);
});
You can create a function and check the complete
property.
function ImageExists(selector) {
var imageFound = $(selector);
if (!imageFound.get(0).complete) {
return false;
}
else if (imageFound.height() === 0) {
return false;
}
return true;
}
and call this funciton
var exists = ImageExists('#UrlQueueBox');
Same function with the url instead of a selector as parameter (your case):
function imageExists(url){
var image = new Image();
image.src = url;
if (!image.complete) {
return false;
}
else if (image.height === 0) {
return false;
}
return true;
}