用于检测Internet连接的Jquery插件

Piy*_*dar 10 jquery google-chrome

我目前正在使用Tom Riley的Jquery插件在我的应用程序中检测Internet连接,它在Internet Explorer中工作正常,但在Google Chrome中实现它时没有响应.

任何人都可以建议一个更好的检测互联网连接的插件,它在谷歌浏览器中完美运行(所有浏览器)

Sar*_*raz 10

你不需要一个插件,只需:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
  if (! window.jQuery) {
    alert('No internet Connection !!');
  }
  else {
     // internet connected
  }
</script>
Run Code Online (Sandbox Code Playgroud)

以上工作的原因是因为jQuery lib是从需要互联网连接的谷歌CDN读取的,如果页面无法读取,则表示不存在互联网连接.

更新

你可以这样做:

function checkConnection() {
  var connected = true;
  var img = document.createElement('img');
  img.src = "path to remoate image on your server";
  img.onerror = function() { connected = false; }
  return connected;
}
Run Code Online (Sandbox Code Playgroud)

您可以随时使用它:

if (checkConnection()) {
  // connected
}
Run Code Online (Sandbox Code Playgroud)

更新2

您可以定期/自动检查它是这样的:

setInterval(function(){
  var isConnected = checkConnection(); // checkConnection() comes from above code
  if (isConnected) {
    alert('Connected');
  }
  else {
    alert('Not Connected');
  }
}, 10000); // 10000 = 10 seconds, check for connection every 10 seconds
Run Code Online (Sandbox Code Playgroud)

其他有用的链接: