Alert box when no internet connection - Phonegap

喜你入骨 提交于 2019-12-05 20:28:01

问题


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


回答1:


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




回答2:


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



回答3:


Add else to do nothing...

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



回答4:


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



回答5:


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.




回答6:


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>



回答7:


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



回答8:


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



回答9:


<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>


来源:https://stackoverflow.com/questions/10009596/alert-box-when-no-internet-connection-phonegap

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