Skip to content

Check whether browser is online using JavaScript

The onLine property under navigator object returns the online status of the browser. We can use this property to check if the browser is online.

Advertisements
if (navigator.onLine) 
  console.log('You are online');
else 
  console.log('You are offline');

//You are online

Attach this to an event listener you can enable notifications or call functions when browser goes online / offline.

window.addEventListener('online', function(e) {
    console.log('You are online');
});

window.addEventListener('offline', function(e) {
    console.log('You are offline');
});

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.