Skip to main content

Detecting network status change with events and determine if the user has gone offline/online.

//
// Detecting Network Status Change with Events
//
// You can register to online and offline events to check when user switches to
// online or offline mode.

window.addEventListener("online", function() {
    console.log("Current status : online");
});

window.addEventListener("offline", function() {
    console.log("Current status : offline");
});

//
// Checking Current Status with navigator.onLine
//
// navigator.onLine is a property that gives a true or false depending upon the
// current network status (true when online & false when offline).

if (navigator.onLine === true) {
    console.log("You are online");
} else {
    console.log("You are offline");
}