I'm working on rewriting a userscript that I wrote a week or so ago, but I stumbled upon a problem. I am by no means an expert on JavaScript, and I'm not sure what I'm doing wrong with my code.
Here is my code:
function checkImageValidity(url) {
return imageExists(url, function(exists) {
return exists;
});
}
function imageExists(url, callback) {
var img = new Image();
img.onload = function() { callback(true); };
img.onerror = function() { callback(false); };
img.src = url;
}
Inside my call to imageExists
, I've tried adding console.log(url + " -- " + exists)
, which is giving me the results I expected from my testing URLs.
The problem is, this is not returning properly to checkImageValidity, it's always being returned as "undefined".
What am I doing wrong?