11

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

9 Answers 9

25

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

4
  • Came by this question by coincidence and noticed I never marked an answer as correct. This seems the easiest and best anser, thanks!
    – Rvervuurt
    Feb 5, 2015 at 11:16
  • 1
    This is great but I'd like to know the state when the app initially starts - is there any way of getting that in the same sort of way?
    – Chris Rae
    May 14, 2015 at 22:27
  • Does anyone face the issue that I face - stackoverflow.com/questions/35425746/… Feb 16, 2016 at 19:49
  • I find these methods to be highly inconsistent when using Cordova. Sometimes the it says connection none even though one exist. Aug 10, 2017 at 17:35
7

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 ');
};
2
  • That does exactly what the example does: returning the kind of connection in an alert-box. What I want is that the pop-up box ONLY displays when the connection is states[Connection.NONE]
    – Rvervuurt
    Apr 4, 2012 at 14:12
  • where to add this code? i mean can i call this in the $(document).ready(function() {...} function? Feb 11, 2013 at 5:32
4

Add else to do nothing...

if (navigator.network.connection.type == Connection.NONE) {
    alert('Geen internet :(');
}
else {
    // nothing
};
3

That's because you are testing the truthiness of a constant. That type of test will always return true. What you want to use is:

if (navigator.network.connection.type == Connection.NONE]{
  alert('Geen internet :(');
};
3

Check the latest documentation for cordova 2.0.0 here: http://docs.phonegap.com/en/2.0.0/cordova_connection_connection.md.html#Connection

There is a good example. You need to check for Connection.NONE or Connection.UNKNOWN - both mean no internet. Anything else means you have an internet connection of some sort.

1

Solution :

<script type="text/javascript" charset="utf-8">

// Wait for PhoneGap to load
// 
document.addEventListener("deviceready", onDeviceReady, false);

// PhoneGap is loaded and it is now safe to make calls PhoneGap methods
//
function onDeviceReady() {
    checkConnection();
}

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

    var states = {};
    states[Connection.UNKNOWN]  = 'Unknown connection';
    states[Connection.ETHERNET] = 'Ethernet connection';
    states[Connection.WIFI]     = 'WiFi connection';
    states[Connection.CELL_2G]  = 'Cell 2G connection';
    states[Connection.CELL_3G]  = 'Cell 3G connection';
    states[Connection.CELL_4G]  = 'Cell 4G connection';
    states[Connection.NONE]     = 'No network connection';

    alert('Connection type: ' + states[networkState]);
}

</script>
0
Try the below code.  

var networkState = navigator.network.connection.type;
var states = {};
states[Connection.UNKNOWN]  = 'Unknown connection';
states[Connection.ETHERNET] = 'Ethernet connection';
states[Connection.WIFI]     = 'WiFi connection';
states[Connection.CELL_2G]  = 'Cell 2G connection';
states[Connection.CELL_3G]  = 'Cell 3G connection';
states[Connection.CELL_4G]  = 'Cell 4G connection';
states[Connection.NONE]     = 'No network connection';

if ((states[networkState]) == states[Connection.NONE])
{
alert("Please check your internet connectivity and try again"); 
}
0

I'm just amalgamating a couple of answers here, but I wanted something which responded to the online/offline events but also knew at the start whether there was an internet connection. I was using this in an Angular controller which broadcast about state changes, but I've removed those parts for simplicity.

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    var _curStatus = false;

    function toggleStatus(newVal) {
        console.log('Setting internet connection state to: ' + newVal);
        _curStatus = newVal;
        // My angular $broadcast went here
    }

    var conType = navigator.network.connection.type;
    toggleStatus((conType != Connection.NONE) && (conType != Connection.UNKNOWN));
    document.addEventListener("online", function() {toggleStatus(true);}, false);
    document.addEventListener("offline", function() {toggleStatus(false);}, false);
    }
}
0
<script type="text/javascript" charset="utf-8">

    // Wait for device API libraries to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // device APIs are available
    //
    function onDeviceReady() {
        checkConnection();
    }

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

            if(navigator.connection.type == Connection.NONE) {        
        alert("No network connection.Please turn it on");
    }
        }

    </script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.