Detect if user is online

Posted on Sat Dec 03 2022
Tags: PostToday I LearnedJavaScript


There is an API that let us check if the user is online:


and provide events triggered when going offline or online:

window.addEventListener('offline', () => console.log('now offline');
window.addEventListener('online', () => console.log('now online');

Possible usage

Some possible usages are:

  • synchronizing data when going online with a PWA
  • displaying an alert to the user if he looses connection when doing actions that requires him to be online

See also