user4069483
user4069483

Reputation:

Online Offline EventListener not working

I am using online offline EventListener but its not working event. I have place cordova.js before code or after code its not working at all

    document.addEventListener("offline", getSettinglocally, false);
    document.addEventListener("online", getSettingremotly, false);

deviceReady EventListener is working fine

Upvotes: 1

Views: 1916

Answers (2)

Dawson Loudon
Dawson Loudon

Reputation: 6029

These events do not fire on load of the application. They are for catching these events after the application has completely loaded and is in use. If you want to capture the network state on load of the application use something like below:

initialConnection: function() {
    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;
    global.connectionStatus = states[networkState];
    app.connectionStatus(global.connectionStatus);
    if(global.connectionStatus) {
       //do something when connected
    }
    else{
        //do something else if not
    }
}

And as noted in another answer for this you need the network-information plugin installed.

Upvotes: 1

Adi
Adi

Reputation: 6354

offline and online events are part of a this (org.apache.cordova.network-information) plugin.
For these events to work you need to have the plugin installed.

If you need some guidance on how to install a plugin into a Cordova Project please take a look at this link.

Upvotes: 1

Related Questions