简体   繁体   English

如何通过javascript检查浏览器的在线/离线状态?

[英]How to check online/offline state in browser via javascript?

I want a simply code, that checks the internet connection inside the browser and a handling that handles the various codes, that will come back.我想要一个简单的代码,它检查浏览器内的互联网连接,以及一个处理各种代码的处理,它会回来。

I already tried a http-get-request to handle the returning status code, but at the end, I had some issues with the same-origin-policy.我已经尝试了一个 http-get-request 来处理返回的状态代码,但最后,我在同源策略方面遇到了一些问题。

I also tried the ononline- and onoffline-event, but these are depricated and do not work in my target-browser.我还尝试了 ononline- 和 onoffline-event,但这些都被弃用了,并且在我的目标浏览器中不起作用。

My 3rd option was with navigator.onLine.我的第三个选择是使用 navigator.onLine。 When the Browser got Internet Connection, the output is true.当浏览器获得 Internet 连接时,输出为 true。 But, when my browser got not connection, the answer of navigator.onLine is not false...但是,当我的浏览器没有连接时,navigator.onLine 的答案不是错误的...

My main use case is to check, if the browser got internet connection or not.我的主要用例是检查浏览器是否连接到互联网。 If he got Internet Connection, then he should open www.abc.de But if there is no Internet Connection, he should open a local application with localhost:8080/abc.如果他有 Internet 连接,那么他应该打开 www.abc.de 但如果没有 Internet 连接,他应该打开一个本地应用程序 localhost:8080/abc。

In principle this is a duplicate of Why navigator.onLine is inconsistent?原则上这是为什么 navigator.onLine 不一致的重复 Is there any reliable solutions? 有没有可靠的解决方案?

However I would personally try something like但是我个人会尝试类似的东西

if (location.host.indexOf("localhost")==-1) { // we are not already on localhost
  var img = new Image();
  img.onerror=function() { location.replace("http://localhost:8080/abc"); }
  img.src="http://servertotest.com/favicon.ico?rnd="+new Date().getTime();
}

To detect the offline and online status you need to use the below example.要检测离线和在线状态,您需要使用以下示例。

Code Sample代码示例

 // add "is-offline" class to the html element if user is offline and remove when online again window.addEventListener('load', function() { function updateOnlineStatus(event) { if (navigator.onLine) { document.documentElement.classList.remove('is-offline'); document.querySelector('.connection-status').innerHTML = 'Online'; } else { document.documentElement.classList.add('is-offline'); document.querySelector('.connection-status').innerHTML = 'Offline'; } } window.addEventListener('online', updateOnlineStatus); window.addEventListener('offline', updateOnlineStatus); });
 .connection-status { font-size: 19px; font-weight: 600; color: #58b05c; } .is-offline .connection-status { color: red; }
 <center> <p class="connection-status">Online</p> </center>

try to use this尝试使用这个

window.navigator.onLine;

for more information, please refer欲了解更多信息,请参阅

https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM