I've been experimenting with an XMLHttpRequest and managed to get the basic code functioning properly. However, I'm struggling to create a function that will return a boolean variable indicating whether it has worked or not:
var xhr = new XMLHttpRequest();
var success = false;
xhr.open('HEAD', sourceurl, true);
xhr.send();
xhr.onreadystatechange=function(){
if (xhr.readyState === 4){ //if complete
if(xhr.status === 200){ //check if "OK" (200)
//success
console.log('success');
return true;
} else {
// fail
console.log('fail' );
return false;
}
}
};
Although the console logs display the correct messages, when attempting to wrap this code within a function, no feedback is provided on its success or failure.
I would greatly appreciate any assistance with this!
EDIT: This is not a duplicate issue, as I am unable to utilize jQuery in my case and the existing solutions do not apply to my situation.