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:
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:
window.addEventListener("offline", (e) => {
console.log("offline");
});
window.addEventListener("online", (e) => {
console.log("online");
});
Specifications
Specification |
---|
HTML # dom-navigator-online-dev |
Browser compatibility
BCD tables only load in the browser