gpt4 book ai didi

javascript - 一劳永逸,navigator.onLine 是做什么的?

转载 作者:技术小花猫 更新时间:2023-10-29 12:40:42 26 4
gpt4 key购买 nike

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

最重要的是,即使 3G/4G 和 WiFi 已打开并且用户未处于飞行模式或其他任何状态,navigator.onLine 是否会告诉我用户是否由于覆盖不良而无法联系那里的服务器?只是您的平均盲区。

最佳答案

来自MDN article about navigator.onLine :

Browsers implement this property differently.

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.

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

关于javascript - 一劳永逸,navigator.onLine 是做什么的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13076383/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com