最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论
    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?

    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?

Share Improve this question asked Oct 14, 2012 at 5:39 dmandman 11.1k25 gold badges116 silver badges217 bronze badges 1
  • you want to test internet connection or connection to webserver ? you should make any request to webserver if you want to check if connection is here, – zb' Commented Oct 14, 2012 at 5:49
Add a ment  | 

1 Answer 1

Reset to default 6

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");
    }
}
发布评论

评论列表(0)

  1. 暂无评论