HTML 5 Online and offline events
HTML
<header>
<h2 id="status"></h2>
</header>
CSS
body {
font-family: Segoe UI;
font-size: 9pt;
}
h1 {
font-family: Segoe UI;
font-weight: lighter;
/*background-color: rgb(27, 161, 226);*/
color: #fff;
padding: 20px;
}
h2 {
color: #fff;
font-family: Segoe UI;
font-weight: lighter;
padding: 10px;
}
.offline {
background-color: red;
}
.online {
background-color: green;
}
JavaScript
function isOnline() {
return navigator.onLine;
}
function checkStatus() {
var status = document.getElementById("status");
if (isOnline()) {
status.className = "online";
status.innerText = "Online";
} else {
status.className = "offline";
status.innerText = "Offline";
}
}
(function () {
window.addEventListener("online", checkStatus);
window.addEventListener("offline", checkStatus);
checkStatus();
})();