Navigator: onLine property

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

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.

Value

A boolean.

Examples

Basic usage

To check if you are online, query window.navigator.onLine, as in the following example:

js
if (navigator.onLine) {
  console.log("online");
} else {
  console.log("offline");
}

If the browser doesn't support navigator.onLine the above example will always come out as false/undefined.

Listening for changes in network status

To see changes in the network state, use addEventListener to listen for the events on window.online and window.offline, as in the following example:

js
window.addEventListener("offline", (e) => {
  console.log("offline");
});

window.addEventListener("online", (e) => {
  console.log("online");
});

Specifications

Specification
HTML
# dom-navigator-online-dev

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Node.js
onLine
Chrome – Full support
Chrome 2 (Release date: 2009-05-21)
footnote
footnote 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.
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 1.5 (Release date: 2005-11-29)
footnote
footnote 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).
Opera – Full support
Opera 3 (Release date: 1997-12-01)
footnote
footnote From Opera 11.1 until Opera 12.1, the browser returns true when 'Work Offline' mode is disabled and false when it is enabled, regardless of actual connectivity.
Safari – Full support
Safari 4 (Release date: 2009-06-08)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 4 (Release date: 2011-03-29)
footnote Full support
Opera Android – Full support
Opera Android 10.1 (Release date: 2010-11-09)
footnote
footnote From Opera 11.1 until Opera 12.1, the browser returns true when 'Work Offline' mode is disabled and false when it is enabled, regardless of actual connectivity.
Safari on iOS – Full support
Safari on iOS 3.2 (Release date: 2010-04-03)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Partial support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Partial support
footnote Can incorrectly return true, see bug 41369933.
WebView on iOS – Full support
WebView on iOS 3.2 (Release date: 2010-04-03)
footnote Full support
Node.js – No support
Node.js
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
No support
No support
See implementation notes.