Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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. :)

like image 641
AndyBoy Avatar asked Mar 17 '14 07:03

AndyBoy


1 Answers

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;
    }
}
like image 192
andreasbecker.de Avatar answered Oct 29 '22 15:10

andreasbecker.de