Question

Question: how do I know whether XMLHttpRequest failed because of internet connection being down or because the requested domain does not exist?

Background & context: Chrome extension which can make XHttpRequests to any domain.

Code:

function isOnline(url, callback)
{

    var xhr = new XMLHttpRequest();
    var result = {status: 0, online: true};

    xhr.onerror = function(data) { 
        // note! data object here doesn't contain any useful info about the reason
        result.online = false;
    };

    xhr.onreadystatechange = function(data) {
        if (xhr.readyState == 4) 
        {
            result.status = xhr.status;
            result.message = xhr.statusText;
            // timeout is needed since onerror is processed AFTER onreadystatechange
            setTimeout(function() { callback(result) }, 100);
        }
    };

    xhr.open('HEAD', url + "?rand=" + Math.random(), true);
    try
    {
        xhr.send();
    }
    catch(error)
    {
        console.log("This line is never shown");
    }
}

Now the problem is that if the Internet connection is UP and I call

isOnline("http://www.nonexistentdomain.com", function(result) { console.log(result); });

The xhr.onerror handler fires and status = 0, online: false.

If the network connection is DOWN and I make the same call, the result is the same (status = 0, online: false).

In other words it seems that it is not possible to determine whether the requested URL or domain does not exist or if the network connection is down?

The try-catch block around xhr.send() does not actually catch any exceptions although an exception is thrown (seen in chrome console).

Also code

navigator.isOnline

always seems to return true in Chrome extension eventhough the network connection is not up so that cannot be used.

So I either need a 100% reliable way to determine if the Internet connection is up or not OR a way to figure out in XMLHttpRequest whether the failure was due to unavailable network or the fact that the domain does not exist.

If the XMLHttpRequest class cannot be used to determine whether the domain does not exist or the network connection is down, I might have to check internet connection before every "real request". Ack! Also using e.g. www.google.com to check it won't do since google is blocked in many countries. www.microsoft.com might be ok but somehow I hate the idea.

Any ideas?

Was it helpful?

Solution

If I understood the question correctly... you need to differentiate between internet down, or wrong dns.

Basically... you can't, well... not without making another request to somewhere else. All you can know is that destination is unreachable. That is because you can't actually define "internet is down". How do you define that? Internet is down if I can't go over my gateway? What if I have 2 rooters one after the other? Or 3? You say: Internet is down if I can't go over 3 gateways? Then what if I have 1000?

So... the most you can do is what you said: make a request to something else. As microsoft.com, or example.net, or whatever... you can make more requests and if all are down then you can say that all is down. This allows you to say "if I can't reach any of those, then internet is down". This is indeed not 100% reliable, but may fit your needs.

As about invalid destination... same problem. A destination may be valid but for some reason may be unreachable from you. Reasons may include: dns was not fully propagate, a route is down, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top