0

我正在尝试检查浏览器是否与 javascript 有 Internet 连接,但我在 IE 5.5 上遇到了一些问题

<script>
function checkConnection(){
           if(navigator.onLine === false){
               //document.execCommand("Stop");
               alert("No internet connection.");
               document.execCommand("Stop");
}
</script>

和:

<input type="submit" value="GO" name="whereTo" onclick="checkConnection();"  />

似乎 IE 5.5 没有 navigator.onLine 属性,如何检查 IE 5.5 的连接?

4

1 回答 1

2

为什么不尝试发送 AJAX 请求?它不会检查您是否严格在线,但会告诉您是否可以访问某些内容...尝试几个 URL 可能就足够了...

function ajaxRequest(url) {
  var xmlhttp;
  if (window.XMLHttpRequest) {
    // code for IE 7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  }  else {
    // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState !== 4 && xmlhttp.status !== 200) {
      alert("No internet connection.");
      document.execCommand("Stop");
    }
  }

  xmlhttp.open("GET",url, true);
  xmlhttp.send();
  return false;
}
于 2013-11-08T09:53:19.817 回答