Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Connectivity Notifier</title> <style> body { font-family: Arial, sans-serif; text-align: center; margin-top: 50px; } #status { padding: 10px; border-radius: 5px; color: #fff; font-size: 24px; } .online { background-color: #4caf50; animation: blinker 1s linear infinite; } .offline { background-color: #f44336; animation: blinker 1s linear infinite; } @keyframes blinker { 50% { opacity: 0.5; } } </style> </head> <body> <h1>Connectivity Notifier</h1> <p id="status" class="offline">Checking connectivity...</p> <script> if ("serviceWorker" in navigator) { navigator.serviceWorker.register("https://www.w3docs.com/sw.js").then(function () { console.log("Service Worker Registered"); }); navigator.serviceWorker.onmessage = function (event) { const statusElement = document.getElementById("status"); statusElement.textContent = event.data; statusElement.className = event.data === "Online" ? "online" : "offline"; }; } </script> </body> </html>