<!doctype html>
<head>
<style>
/* CSS comes here */
body {
padding:10px;
font-family:arial;
font-size:1.2em;
}
.error {
background-color:#ff5252;
color:white;
padding:10px;
border-radius:5px;
margin-top:10px;
}
.success {
background-color:#00e676;
color:white;
padding:10px;
border-radius:5px;
margin-top:10px;
}
</style>
<title>Check if User is Offline/Online</title>
</head>
<body>
<h2>Welcome to Studytonight!</h2>
<p>This is a simple code example to show you how to find when a user goes offline and when the user comes back online to show some messages to the user when this happens.</p>
<div id="status"></div>
<script>
/* JS comes here */
let status = document.getElementById("status");
window.addEventListener('load', function(e) {
if (navigator.onLine) {
status.innerHTML = "User is online";
status.classList.add("success");
} else {
status.innerHTML = "User is offline";
status.classList.remove("success");
status.classList.add("error");
}
}, false);
window.addEventListener('online', function(e) {
status.innerHTML = "User is back online";
status.classList.remove("error");
status.classList.add("success");
}, false);
window.addEventListener('offline', function(e) {
status.innerHTML = "User went offline";
status.classList.remove("success");
status.classList.add("error");
}, false);
</script>
</body>
</html>