当设备上没有互联网连接时,我正试图弹出一个弹出窗口。
我让the following example正常工作,但现在我希望仅在结果为“无网络连接”时显示警报。
我试过了:
if (states[Connection.NONE]){
alert('Geen internet :(');
};
但是,无论是否存在连接,这只会使警报框弹出。 谁能帮我? :)
答案 0 :(得分: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
答案 1 :(得分:7)
如果你这样做
if (states[Connection.NONE]){
alert('Geen internet :(');
};
这会发生。
做到这一点。
networkState = navigator.network.connection.type
alert(states[networkState]);
看看这是否适用于你。
编辑:只需比较:
if (networkState == Connection.NONE){
alert('no internet ');
};
答案 2 :(得分:4)
添加其他内容无所事事......
if (navigator.network.connection.type == Connection.NONE) {
alert('Geen internet :(');
}
else {
// nothing
};
答案 3 :(得分:3)
那是因为你正在测试常数的真实性。那种类型 测试总是会返回true。你想要使用的是:
if (navigator.network.connection.type == Connection.NONE]{
alert('Geen internet :(');
};
答案 4 :(得分:3)
在此处查看cordova 2.0.0的最新文档:http://docs.phonegap.com/en/2.0.0/cordova_connection_connection.md.html#Connection
有一个很好的例子。您需要检查Connection.NONE或Connection.UNKNOWN - 两者都意味着没有互联网。其他任何意味着你都有某种互联网连接。
答案 5 :(得分: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>
答案 6 :(得分: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");
}
答案 7 :(得分: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);
}
}
答案 8 :(得分: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>