I am trying to check HTTP status for a document. But getting trouble during function run. I have function
function sitePresent(url) {
var status = 1000;
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
status = 200;
} else {
status = 500;
}
};
req.open("GET", url, true);
req.send();
return status;
}
alert(sitePresent(url));
But I get 500 (it is just test code). When I add alert popup into both of the conditions instead status = XXX
I get two popups.
I think all in that block is executed. I am missing something.
Please how can I make a function that returns true or false depending on HTTP status?