I have found many questions on this topic including stackoverflow. But none of them working for me. I have compiled the following from the existing answers:
function doesFileExist(urlToFile)
{
var xhr = new XMLHttpRequest();
xhr.open('HEAD', urlToFile, false); // => Error message on this line
xhr.send();
if (xhr.status == "404") {
return false;
}else {
return true;
}
}
Then I use this function to call my web server as follows:
var theSource = "myurl";
var result = doesFileExist(theSource);
if (result == true) {
// yes, file exists!
console.log('file exists:', theSource);
} else {
// file does not exist!
console.log('file does not exist:', theSource);
}
The error message is this:
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.