I'm trying to get a pop-up to, well, pop up when there is no internet connection on the device.
I got the following example working, but now I want the alert only to show when the result is "No network connection".
I tried this:
if (states[Connection.NONE]){
alert('Geen internet :(');
};
But that just makes the alert-box pop up, no matter if there is a connection or not. Who can help me? :)
Old-ish question, but here's how I'd do it - You can use events to detect if the device is online or offline. This is ideal for this situation as the pop-up will appear as soon as the device goes offline:
document.addEventListener("offline", function(){ alert("You're offline") }, false);
And to do the same, but when the device regains an internet connection?:
document.addEventListener("online", function(){ alert("You're online") }, false);
Check out the events docs here: http://docs.phonegap.com/en/1.8.1/cordova_events_events.md.html#offline
UPDATE :
as of cordova 5 these events have been moved to cordova-plugin-network-information
if you do
if (states[Connection.NONE]){
alert('Geen internet :(');
};
this would happen.
do this.
networkState = navigator.network.connection.type
alert(states[networkState]);
see if this works for u or not.
EDIT: just compare:
if (networkState == Connection.NONE){
alert('no internet ');
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With