Navigator: onLine property - Web APIs | MDN

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

Help improve MDN

Learn how to contribute

This page was last modified on by MDN contributors.