Online status
The onLine property of the Navigator interface returns whether the device is connected to the network, with true meaning online and false meaning offline. The property's value changes after the browser checks its network connection, usually when the user follows links or when a script requests a remote page. For example, the property should return false when users click links soon after they lose internet connection. When its value changes, an online or offline event is fired on the window.
Browsers and operating systems leverage different heuristics to determine whether the device is online. In general, connection to LAN is considered online, even though the LAN may not have Internet access. For example, the computer may be running a virtualization software that has virtual ethernet adapters that are always "connected". On Windows, the online status is determined by whether it can reach a Microsoft home server, which may be blocked by firewalls or VPNs, even if the computer has Internet access. Therefore, this property is inherently unreliable, and you should not disable features based on the online status, only provide hints when the user may seem offline.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 2 | 12 | 1.5 | 4 | 18 | 3.2 | |
| The offline event of the Window interface is fired when the browser has lost access to the network and the value of Navigator.onLine switches to false. | 3 | 12 | 9 | 4 | 18 | 3 |
| The online event of the Window interface is fired when the browser has gained access to the network and the value of Navigator.onLine switches to true. | 3 | 12 | 9 | 4 | 18 | 3 |
| The offline event of the WorkerGlobalScope fires when the device loses connection to the internet. | | | 29 | 8 | | 8 |
| The online event of the WorkerGlobalScope fires when the device reconnects to the internet. | | | 29 | 8 | | 8 |
| Returns the online status of the browser. The property returns a boolean value, with true meaning online and false meaning offline. The property sends updates whenever the browser's ability to connect to the network changes. The update occurs when the user follows links or when a script requests a remote page. | 4 | 12 | 3.5 | 4 | 18 | 5 |
- Earlier versions of Chrome incorrectly return true when a tab is first opened, but it starts reporting the correct connectivity status after the first network event. Windows: 11, macOS: 14, ChromeOS: 13, Linux: Always returns `true`. For history, see bug 40530968.
- Since Firefox 4 the browser returns `true` when 'Work Offline' mode is disabled and `false` when it is enabled, regardless of actual connectivity. Since Firefox 41, on OS X and Windows, the returned values follow the actual network connectivity, unless 'Work offline' mode is selected (where it will always return `false`).
- Since Firefox 4 the browser returns `true` when 'Work Offline' mode is disabled and `false` when it is enabled, regardless of actual connectivity. Since Firefox 41, on OS X and Windows, the returned values follow the actual network connectivity, unless 'Work offline' mode is selected (where it will always return `false`).
Use cases
-
Using Online status
The onLine property of the Navigator interface returns whether the device is connected to the network, with true meaning online and false meaning offline.
Cautions
- May not be supported in older browsers.
Implementation notes
- Some APIs require a secure context (HTTPS) or user activation. Check the requirements before use.