在线API在任何浏览器中均不起作用 [英] The onLine API isn't working in any browser

查看:159
本文介绍了在线API在任何浏览器中均不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

navigator.onLine始终返回true,并且在64位Windows 7的Firefox 66.0.1或Chrome 73中,对于此简单代码段,均不会触发onlineoffline事件.

navigator.onLine is always returning true and the online and offline events are not firing for this simple piece of code either in Firefox 66.0.1 or Chrome 73 on 64-bit Windows 7.

let statusElement = document.getElementById("status");
statusElement.innerText = navigator.onLine ? "online." : "offline.";

function updateStatus(event) {
    statusElement.innerText = `${event.type}.`;
}

window.addEventListener('offline', updateStatus);
window.addEventListener('online', updateStatus);

在HTML很简单的地方,这是这样:

Where the HTML is simple this:

<h1>You are <span id = "status"></span></h1>

推荐答案

我刚刚更换了机器,相同的代码可以完美地在另一台机器上工作.

I just changed my machine and the same code works on a different machine perfectly.

我发现它无法在旧计算机上运行的原因是因为我拥有一个始终处于活动状态的IP地址.这是Oracle的Virtual-Box分配给我的计算机的IP.我是通过在计算机上运行ipconfig来发现这一点的,发现不管做什么,我总是使用以太网卡安装Virtual-Box.

I figured the reason it didn't work on the old machine was because I had an IP Address that was always active. It was an IP that was allotted to my machine by Oracle's Virtual-Box. I found this out by running ipconfig on my machine and saw that no matter what I did, I always had the Virtual-Box using my Ethernet card.

并且由于此功能的文档说如果计算机连接到网络,则表示在线的状态,这意味着任何网络,即具有 any 类别的网络访问权限.

And since the documentation for this feature says that it will show a status of online if the machine is connected to the network, implying to any network, i.e. if it has network access of any sort.

因此,尽管您确实想在大多数情况下使用该方法,但是可以推断出,如果您想真正知道用户是否具有活动的Internet连接,则该方法并不十分可靠.

It is therefore inferred that this method is not terribly reliable if you want to really know if the user has an active Internet connection, though it does work for most of the cases.

案件解决了.

这篇关于在线API在任何浏览器中均不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆