Event Redirection Offline Offline HTML5

1

How do I identify if the HTML5 app identifies and redirects if it's offline or online?

Ex: When you open the app if you are without internet it loads a warning html and if online it redirects to a website.

I found this code but I do not know if it is possible to modify it this way.

     function updateOnlineStatus(msg) {
       var status = document.getElementById("status");
       var condition = navigator.onLine ? "ONLINE" : "OFFLINE";
       status.setAttribute("class", condition);
       var state = document.getElementById("state");
       state.innerHTML = condition;
       var log = document.getElementById("log");
       log.appendChild(document.createTextNode("Event: " + msg + "; status=" + condition + "\n"));
     }
     function loaded() {
       updateOnlineStatus("load");
       document.body.addEventListener("offline", function () {
         updateOnlineStatus("offline")
       }, false);
       document.body.addEventListener("online", function () {
         updateOnlineStatus("online")
       }, false);
     }
<!doctype html>
 <html>
 <head>
   <style>...</style>
 </head>
 <body onload="loaded()">
   <div id="status"><p id="state"></p></div>
   <div id="log"></div>
 </body>
 </html>

Reference: link

    
asked by anonymous 26.09.2017 / 15:38

1 answer

1

I solved it this way:

   function updateOnlineStatus(evento) {
       var status = document.getElementById("status");
       var condition = navigator.onLine ? location.href='online.html' : location.href='offline.html' ;          
     }
     function loaded() {
       updateOnlineStatus("load");
       document.body.addEventListener("offline", function () {
         updateOnlineStatus("offline")
       }, false);
       document.body.addEventListener(function () {
         updateOnlineStatus("online")
       }, false);
     }
<!doctype html>
 <html>
 <head>
 
 </head>
 <body onload="loaded()">

 </body>
 </html>

If there is a simpler method, I appreciate the collaboration.

    
26.09.2017 / 16:41