1
I have to take an image that can be in 2 directories and to know which directory I am trying to use the function below.
The problem is that within the onerror or onload event I cannot assign any value to the imageTest object. I want to do this test without using ajax, only using javascript, but lost in what to do.
function is_img(file) {
const imageTest = {
status: false,
};
var img = document.createElement('img');
img.src = file;
img.onload = () => {
imageTest.status = true;
console.log("A imagem " + file + " existe");
}
img.onerror = (msg, url, lineNo, columnNo, error) => {
imageTest.status = false;
console.log("A imagem " + file + " não existe");
}
if (imageTest.status === true) {
return true;
} else {
return false;
}
}
Because you do not backend the source of the file by making an ajax request?
– Ivan Ferrer