Detect the Internet connection is offline?

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

How to detect the Internet connection is offline in JavaScript?

19条回答
  •  梦谈多话
    2020-11-22 01:36

    As olliej said, using the navigator.onLine browser property is preferable than sending network requests and, accordingly with developer.mozilla.org/En/Online_and_offline_events, it is even supported by old versions of Firefox and IE.

    Recently, the WHATWG has specified the addition of the online and offline events, in case you need to react on navigator.onLine changes.

    Please also pay attention to the link posted by Daniel Silveira which points out that relying on those signal/property for syncing with the server is not always a good idea.

提交回复
热议问题