一劳永逸,navigator.onLine做什么? [英] Once and for all, what does navigator.onLine do?

查看:146
本文介绍了一劳永逸,navigator.onLine做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过一千个帖子,有人问如何在javascript中检测互联网连接,并且总有一个人说使用navigator.onLine,另一个人说它不可靠,只告诉你连接是否可用,如果你已经连接,那就不是了。

I've seen a thousand posts where someone is asking how to detect internet connectivity in javascript and there is always one person who says to use navigator.onLine and another that says it is unreliable and only tells you if a connection is available, not if you're connected.

底线,navigator.onLine告诉我,即使3G / 4G和WiFi打开,用户也无法联系那里的服务器不是飞机模式或任何东西?只是你的平均死区。

Bottom line, will navigator.onLine tell me if the user is unable to contact there server due to poor coverage even if 3G/4G and WiFi are turned on and they are not in airplane mode or anything? Just your average dead zone.

推荐答案

来自 MDN文章关于 navigator.onLine


浏览器以不同方式实现此属性。

Browsers implement this property differently.

在Chrome和Safari中,如果浏览器无法连接到局域网(LAN)或路由器,它处于脱机状态;所有其他条件都返回true。因此,虽然您可以假设浏览器在返回false值时处于脱机状态,但您不能认为真值必然意味着浏览器可以访问Internet。您可能会得到误报,例如计算机运行的虚拟化软件具有始终连接的虚拟以太网适配器。因此,如果您确实想确定浏览器的在线状态,则应开发其他检查方法。要了解更多信息,请参阅HTML5 Rocks文章,在网格中工作

In Chrome and Safari, if the browser is not able to connect to a local area network (LAN) or a router, it is offline; all other conditions return true. So while you can assume that the browser is offline when it returns a false value, you cannot assume that a true value necessarily means that the browser can access the internet. You could be getting false positives, such as in cases where the computer is running a virtualization software that has virtual ethernet adapters that are always "connected." Therefore, if you really want to determine the online status of the browser, you should develop additional means for checking. To learn more, see the HTML5 Rocks article, Working Off the Grid.

在Firefox和Internet Explorer中,将浏览器切换到离线模式会发送错误值。所有其他条件都返回一个真值

In Firefox and Internet Explorer, switching the browser to offline mode sends a false value. All other conditions return a true value

这篇关于一劳永逸,navigator.onLine做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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