I am trying to check internet connection in js:
function doesConnectionExist() {
var xhr = new XMLHttpRequest();
var file = "https://www.google.com";
var randomNum = Math.round(Math.random() * 10000);
xhr.open('HEAD', file + "?rand=" + randomNum, true);
xhr.send();
xhr.addEventListener("readystatechange", processRequest, false);
function processRequest(e) {
if (xhr.readyState == 4) {
if (xhr.status >= 200 && xhr.status < 304) {
alert("connection ok");
} else {
alert("connection doesn't exist!");
}
}
}
}
Its not working, showing:
connection doesn't exist!
if I pass "localhost/myApp" Instead of "www.google.com", it works fine, but if I pass my IP instead of "localhost", it's not working that time again.