Checking for Internet connectivity

A simple example of checking Internet connectivity, by trying to connect to a specific http web adress. The function will return true all fale, based on the result of the http request.
function isInternet() {
    var xhr = new XMLHttpRequest();
    var file = 'http://myDataPage.com/myPeopleData.json';
    var randNum = Math.round(Math.random() * 10000);
     
    xhr.open('HEAD', file + "?rand=" + randNum, false);
     
    try {

        xhr.send();
         
        if (xhr.status >= 200 && xhr.status < 304) {
            return true;
        } else {
            return false;
        }
    } catch (e) {
        return false;
    }
}

Responses

0 Replies