Alert box when no internet connection - Phonegap

梦想的初衷 提交于 2019-12-04 02:24:24
edcs

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

ghostCoder

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 ');
};
Surya

Add else to do nothing...

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

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 :(');
};

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.

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>
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"); 
}

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);
    }
}
user6611978
<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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!