最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Once and for all, what does navigator.onLine do? - Stack Overflow

programmeradmin0浏览0评论

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.

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.

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.

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.

Share Improve this question asked Oct 25, 2012 at 20:06 jmeasejmease 2,5355 gold badges49 silver badges89 bronze badges 2
  • 1 developer.mozilla.org/en-US/docs/DOM/window.navigator.onLine – Mahn Commented Oct 25, 2012 at 20:08
  • 1 labs.ft.com/2012/08/navigator-online-here-be-dragons – CodeReaper Commented Jan 20, 2014 at 12:00
Add a comment  | 

2 Answers 2

Reset to default 15

From the 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

According to MDN, it varies on the browser for implementation.

https://developer.mozilla.org/en-US/docs/DOM/window.navigator.onLine

I would assume with WebKit browsers, its not a reliable way to know that a browser has Internet access, just some form of network access.

发布评论

评论列表(0)

  1. 暂无评论