How to check if a user is online or offline in JavaScript?

Published August 15, 2020

To check whether a user is online or offline in JavaScript, You have to listen for the events online and offline on the window in JavaScript.

Check if User is online

Add an online event listener to the window like this,

// to check if user is online
// add an online event listener
// to window
window.addEventListener("online", (event) => {
  console.log("User is Online ✅");
});

Check if User is offline

Add an offline event listener to the window like this,

// to check if user is offline
// add an offline event listener
// to window
window.addEventListener("offline", (event) => {
  console.log("User is Offline 🚫");
});

Feel free to share if you found this useful 😃.


Share on: Facebook Twitter