Alert box when no internet connection - Phonegap

后端 未结 9 2802
南方客
南方客 2021-02-20 06:55

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 sh

相关标签:
9条回答
  • 2021-02-20 07:32
    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 讨论(0)
  • 2021-02-20 07:34
    <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>
    
    0 讨论(0)
  • 2021-02-20 07:38

    Add else to do nothing...

    if (navigator.network.connection.type == Connection.NONE) {
        alert('Geen internet :(');
    }
    else {
        // nothing
    };
    
    0 讨论(0)
  • 2021-02-20 07:39

    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

    0 讨论(0)
  • 2021-02-20 07:40

    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 :(');
    };
    
    0 讨论(0)
  • 2021-02-20 07:40

    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.

    0 讨论(0)
提交回复
热议问题