javascript - cordova - check internet connectivity first then load body page -


i using cordova-plugin-network-information plugin below code.

<script type="text/javascript" charset="utf-8">      // wait device api libraries load     //     document.addeventlistener("deviceready", ondeviceready, false);      // device apis available     //     function ondeviceready() {         checkconnection();     }      function checkconnection() {         var networkstate = navigator.connection.type;          var states = {};         states[connection.unknown]  = 'unknown connection';         states[connection.ethernet] = 'ethernet connection';         states[connection.wifi]     = 'wifi connection';         states[connection.cell_2g]  = 'cell 2g connection';         states[connection.cell_3g]  = 'cell 3g connection';         states[connection.cell_4g]  = 'cell 4g connection';         states[connection.cell]     = 'cell generic connection';         states[connection.none]     = 'no network connection';          if(networkstate != connection.none){             window.location.href = "index2.html";         } else{             document.getelementbyid('custom-message').style.display = "block";         }      }      </script> 

this plugin working fine redirects 2nd page (index2).

i want in such way first checks connection & load same page index page (no need create index2 page) , if fails (no internet) throw custom error message.

thanks

i'm using this:

function init() {     document.addeventlistener("online", ononline, false);     document.addeventlistener("offline", onoffline, false);     document.addeventlistener('deviceready', startup, false);     document.addeventlistener("backbutton", onbackkeydown, false); } function ononline() {      $('#nointernet').hide(); } function onoffline() {      $('#nointernet').show(); } 

it not checks status on pageload, when changes. if connection goes offline custom error message (#nointernet) shows. when connection goes online, message disappears.

apart code, make code functional change this:

if(networkstate != connection.none){ 

to:

if(states[networkstate] != states[connection.none]){ 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -