Checking the online/offline status of the browser

Published on: February 11, 2021 By T.Andrew Rayan

In this article, let's see how to check whether the browser is online/offline status.

Javascript provides us with the property navigator.onLine in order to check the network status of the browser.

The navigator.onLine property will return true if the browser is in online else it will return false.

Let's see it with an example,

Now if the browser is in online then it will log Network is available. else if the browser is in offline then it will log Network is not available..

Suppose if we want listen to the network change status, then we can use addEventListener to listen to window.online or window.offline events.

Example

In the above example, when the browser is in online status, it will log Browser is in online status message to the console and if the browser is in offline status then, it will prompt an alert dialog with the message Browser is in offline status.


Most Read