Sometimes it is necessary to detect if browser is online or offline before doing some task. Many developers use AJAX to detect browser connectivity status(online or offline) by sending a request to server. But this is a bad way of finding browser status as it requires bandwidth and can lead to bad user experience.
There is a direct way provided by browsers since a long time to detect browser connectivity status. Browsers provide navigator.onLine property to check if browser is online or offline. Its set to true if browser is online otherwise set to false.
Browsers also support online and offline events to notify the applications whenever the browser is goes offline or comes online.
online and offline events can also be attached to any specific HTML element.