开发者

I need some help detecting if a browser has access to the internet [duplicate]

开发者 https://www.devze.com 2023-03-18 22:09 出处:网络
This question already has answers here: Closed 10 years ago. Possible Duplicate: Check if Internet Connection Exists with Javascript?
This question already has answers here: Closed 10 years ago.

Possible Duplicate:

Check if Internet Connection Exists with Javascript?

I have been working on a manual to be sent via CD to a customer. The owner of my company requires that the manual be split into many subsections as .doc and .docx files. The table of contents for this manual will be an HTML page, to be run by autorun when the CD is put into the computer.

I can be fairly sure that the computer is a windows computer, but that's abou开发者_Go百科t it. My current thinking is to use this script to take advantage of google to display the word file, but only if the computer is actually connected to the internet. That way I don't have to rely on the customer having a particular version of word (or having a compatibility pack installed).

From the research I've done, relying on a computer to correctly recognize that a link to a word document should be opened with word is problematic, and has to be resolved at the user end.


Have a look at window.navigator.onLine


You could try an ajax request to a reliable site.

// THIS IS OUR PAGE LOADER EVENT!
window.onload = function() { 
    testConnection();
};

function testConnection(){
    var xmlhttp;

    if (window.XMLHttpRequest){
        xmlhttp=new XMLHttpRequest();
    } else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            alert(xmlhttp.responseText);
        } else if (xmlhttp.readyState==4){
            alert("NO INTERNET DETECTED!");
        }
    }

    xmlhttp.open("GET","http://www.w3schools.com/ajax/ajax_info.txt",true);
    xmlhttp.send();
}

Now you will have to play around with it for a way to use the response because it is asynchronous, but this will work to test a connection.


Previously discussed here: Check if Internet Connection Exists with Javascript? and here: How to know if the network is (dis)connected?.


GM_xmlhttpRequest({
  method: "GET",
  url: "http://www.google.com/",
  onload: function(response) {
    if(response.responseText != null) { 
       alert("OK");
    } else { 
      alert("not connected");
    }
  }
});
  • second alternative

    var HaveConnection = navigator.onLine;

0

精彩评论

暂无评论...
验证码 换一张
取 消