Value
online is a boolean true or false.
Examples
Basic usage
To check if you are online in a worker, query 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 online and offline, as in the following example:
js
addEventListener("offline", (e) => {
console.log("offline");
});
addEventListener("online", (e) => {
console.log("online");
});
Specifications
| Specification |
|---|
| HTML # dom-navigator-online-dev |