navigator.onLine is not working in my mobile.How to check internet is online.offline??? In Phonegap

StackOverflow https://stackoverflow.com/questions/22448591

  •  15-06-2023
  •  | 
  •  

Question

I am using phonegap for my app. My app is basicaly for RSS_FEED from one website. but my requirement is when internet is not there application should alert-offline.

when application run on online all data stored into database. and when internet is not there that time data fetched from DB so my problem is only that application shows me online every time while using of navigator.onLine.

if(navigator.onLine) {
    alert("check online");
    loadScript("http://www.google.com/jsapi",msgOnFn)
    loadScript("js/googleapi.js", msgOnFn);
} else {
    loadScript("js/db.js",msgOffFn);
}

I tried THIS ALSO In this it shows none or wifi everytime. I am not getting why this happening.

in web browser,It is working fine. :)

Was it helpful?

Solution

please follow the phonegap documentation.

You could also change the given function to return true or false:

function checkConnection() {
    var networkState = navigator.connection.type;

    var states = {};
    states[Connection.UNKNOWN]  = false;
    states[Connection.ETHERNET] = true;
    states[Connection.WIFI]     = true;
    states[Connection.CELL_2G]  = true;
    states[Connection.CELL_3G]  = true;
    states[Connection.CELL_4G]  = true;
    states[Connection.CELL]     = true;
    states[Connection.NONE]     = false;
    if (states[networkState]) {
        return true;
    } else {
        return false;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top