So I am trying to check if an image URL is valid or not, in which it has to return a true
or false
value. The problem is that I can't figure out how to achieve this. I'm using ionic 4.0 and this is what I already have:
imageExists(url, callback) {
const img = new Image();
img.onload = function () { callback(true); };
img.onerror = function () { callback(false); };
if(callback === true ) {
return true;
} else {
return false;
}
}
const imageUrl = 'https://www.google.com/images/srpr/nav_logo14.png';
let bool = this.imageExists(imageUrl, function (exists) {
console.log('RESULT: url=' + imageUrl + ', exists=' + exists);
console.log(exists);
bool = exists;
});
console.log(bool);