javascriptandroidhtmlcordovaphonegap-plugins

navigator.onLine not working cordova 5.0.0


I am having problems with checking if a device has no internet connection. I am using cordova 5.0.0 CLI. This is my code:

if(navigator.onLine) {
  alert("online");
} else {
  alert("offline");
  window.open("404.html");
}

The problem is: it's always true. How can i fix it? I know this plugin, but I don't know how I even get it working. I know how to add plugins, but then it ends. Could someone help me getting this plugin working, or an alternative code to check if a user is offline? Thanks in advance


Solution

  • Add this to your index.html :

    document.addEventListener("offline", function(){ navigator.notification.alert("No connection found") }, false);
    

    It will alert you if there isn't a network connection.

    If you don't have the notification plugin :

    document.addEventListener("offline", function(){ alert("No connection found") }, false);