11

我正在尝试在设备上没有互联网连接时弹出一个弹出窗口。

我得到了以下示例,但现在我希望警报仅在结果为“无网络连接”时显示。

我试过这个:

if (states[Connection.NONE]){
       alert('Geen internet :(');
       };

但这只会弹出警报框,无论是否有连接。谁能帮我?:)

4

9 回答 9

25

老问题,但这是我的做法 - 您可以使用事件来检测设备是在线还是离线。这非常适合这种情况,因为一旦设备离线,就会出现弹出窗口:

document.addEventListener("offline", function(){ alert("You're offline") }, false);

并且做同样的事情,但是当设备重新连接互联网时?:

document.addEventListener("online", function(){ alert("You're online") }, false);

在此处查看事件文档:http: //docs.phonegap.com/en/1.8.1/cordova_events_events.md.html#offline

更新 :

从cordova 5开始,这些事件已移至cordova-plugin-network-information

于 2012-10-11T14:25:30.740 回答
7

如果你这样做

if (states[Connection.NONE]){
   alert('Geen internet :(');
   };

这会发生。
做这个。

networkState = navigator.network.connection.type
alert(states[networkState]);

看看这是否适合你。

编辑: 只是比较:

if (networkState == Connection.NONE){
  alert('no internet ');
};
于 2012-04-04T12:56:05.737 回答
4

添加其他什么都不做...

if (navigator.network.connection.type == Connection.NONE) {
    alert('Geen internet :(');
}
else {
    // nothing
};
于 2012-10-07T14:43:56.110 回答
3

那是因为你正在测试一个常数的真实性。这种类型的测试将始终返回 true。您要使用的是:

if (navigator.network.connection.type == Connection.NONE]{
  alert('Geen internet :(');
};
于 2012-04-04T15:03:28.570 回答
3

在此处查看cordova 2.0.0 的最新文档:http: //docs.phonegap.com/en/2.0.0/cordova_connection_connection.md.html#Connection

有一个很好的例子。您需要检查 Connection.NONE 或 Connection.UNKNOWN - 两者都意味着没有互联网。其他任何事情都意味着您拥有某种类型的互联网连接。

于 2012-08-11T13:07:16.763 回答
1

解决方案 :

<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>
于 2012-11-01T07:26:37.967 回答
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"); 
}
于 2014-07-16T06:47:21.807 回答
0

我只是在这里合并了几个答案,但我想要一些能够响应在线/离线事件但一开始就知道是否有互联网连接的东西。我在一个广播状态变化的 Angular 控制器中使用它,但为了简单起见,我删除了这些部分。

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);
    }
}
于 2015-05-14T23:14:08.703 回答
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>
于 2016-07-20T05:56:13.070 回答