如果没有互联网连接,我正在尝试将我的应用程序发送到错误页面。我已经安装了相关的插件并添加了以下代码。
我可以检测到连接类型以及设备是否处于脱机状态时发出消息。
我很难将应用程序发送到错误页面,如果离线并在设备重新联机时返回主页。
// device APIs are available
//
function onDeviceReady() {
document.addEventListener("offline", app.onOffline, false);
},
// Handle the offline event
//
onOffline: function() {
alert("off line");
},
checkConnection: function() {
var networkState = navigator.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.CELL] = 'Cell generic connection';
states[Connection.NONE] = 'No network connection';
alert('Connection type: ' + states[networkState]);
},
有人可以指出我正确的方向。
干杯
韦恩。
答案 0 :(得分:2)
感谢您的帮助,我在设备离线时使用离线EventListener重定向来调用该函数。
checkState: function(){
window.location.href = "offline.html";
},
答案 1 :(得分:0)
添加另一个事件监听器:
document.addEventListener("online", app.onOnline, false);
和
onOnline: function() {
alert("on line");
},
只有当你的应用程序已加载并获得连接时才会触发此侦听器 - 要么已启动连接但未获得连接,要么在应用程序加载后重新连接后丢失连接。
我使用设备airplane mode
来测试这个监听器。
答案 2 :(得分:0)
尝试这个
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
checkConnection();
}
function checkConnection() {
var networkState = navigator.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.CELL] = 'Cell generic connection';
states[Connection.NONE] = 'No network connection';
if ((states[networkState]) == states[Connection.NONE]) {
alert('No Internet Connection. Click OK to exit app');
navigator.app.exitApp();
}
}
请务必先添加这些插件。