dman
dman

Reputation: 11064

XMLHTTPRequest - Test for Connection instead of .send(null) failure

    var xml = null;
    xml = new XMLHttpRequest();
    xml.open("get", who, false);
            xml.send(null);
    if ((xml.status >= 200 && xml.status <= 300) || xml.status == 304) {
    var hi = xml.responseText;
    } else {
    alert("No Internet Connection! You will have to enter information by hand");
    };

I want set up this alert where if there is no internet connection, it will say so. However, the browser stops/hangs at xml.send(null) with NS_ERROR_FAILURE:Failure. How can set up the test properly for a connection?

Upvotes: 2

Views: 4418

Answers (1)

Jay
Jay

Reputation: 4686

Try this instead.

function check() {
    var z, xml = null;
    xml = new XMLHttpRequest();
    xml.open("get", who, false);
    try {
        xml.send(null);
    } catch(z) {
        alert("Network failure");
        return;
    }
    if ((xml.status >= 200 && xml.status <= 300) || xml.status == 304) {
        var hi = xml.responseText;
    } else {
        alert("No Internet Connection! You will have to enter information by hand");
    }
}

Upvotes: 6

Related Questions