Detect the Internet connection is offline?

后端 未结 19 1800
甜味超标
甜味超标 2020-11-22 00:53

How to detect the Internet connection is offline in JavaScript?

19条回答
  •  Happy的楠姐
    2020-11-22 01:39

    IE 8 will support the window.navigator.onLine property.

    But of course that doesn't help with other browsers or operating systems. I predict other browser vendors will decide to provide that property as well given the importance of knowing online/offline status in Ajax applications.

    Until that happens, either XHR or an Image() or request can provide something close to the functionality you want.

    Update (2014/11/16)

    Major browsers now support this property, but your results will vary.

    Quote from Mozilla Documentation:

    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.

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

提交回复
热议问题