Baseline Widely available
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.
ValueA boolean.
Examples Basic usageTo 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
.
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 Browser compatibility
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.3