Javascript cordova-首先检查互联网连接,然后加载正文页面
我正在使用这个cordova插件网络信息插件,代码如下Javascript cordova-首先检查互联网连接,然后加载正文页面,javascript,jquery,cordova,Javascript,Jquery,Cordova,我正在使用这个cordova插件网络信息插件,代码如下 <script type="text/javascript" charset="utf-8"> // Wait for device API libraries to load // document.addEventListener("deviceready", onDeviceReady, false); // device APIs are available // fu
<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;
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(networkState != Connection.NONE){
window.location.href = "index2.html";
} else{
document.getElementById('custom-message').style.display = "block";
}
}
</script>
//等待加载设备API库
//
文件。添加的监听器(“deviceready”,OnDeviceraddy,false);
//设备API可用
//
函数ondevicerady(){
检查连接();
}
函数checkConnection(){
var networkState=navigator.connection.type;
变量状态={};
状态[Connection.UNKNOWN]=“未知连接”;
状态[Connection.ETHERNET]=“以太网连接”;
状态[Connection.WIFI]=“WIFI连接”;
状态[Connection.CELL_2G]=“CELL 2G Connection”;
状态[Connection.CELL_3G]=“CELL 3G Connection”;
状态[Connection.CELL_4G]=“CELL 4G Connection”;
状态[Connection.CELL]=“单元通用连接”;
状态[Connection.NONE]=“无网络连接”;
if(networkState!=Connection.NONE){
window.location.href=“index2.html”;
}否则{
document.getElementById('custom-message').style.display=“block”;
}
}
这个插件工作正常,但它重定向到第二页(index2)
我希望它首先检查连接,然后加载相同的页面索引页面(不需要创建index2页面),如果失败(没有Internet),则抛出自定义错误消息
谢谢我在用这个:
function init() {
document.addEventListener("online", onOnline, false);
document.addEventListener("offline", onOffline, false);
document.addEventListener('deviceready', startUp, false);
document.addEventListener("backbutton", onBackKeyDown, false);
}
function onOnline() {
$('#noInternet').hide();
}
function onOffline() {
$('#noInternet').show();
}
它不仅检查pageload上的状态,还检查它何时更改。因此,如果连接脱机,将显示一条自定义错误消息(#noInternet
)。当连接重新联机时,消息将消失
除了我的代码之外,要使您的代码功能更改,请执行以下操作:
if(networkState != Connection.NONE){
致:
你试过什么?我怎么用你的代码?你能给我提供完整的代码吗。因为我是开发的初学者。即使我更改为if(states[networkState]!=states[Connection.NONE]){但页面只是不断刷新…好的,告诉我如何使用您的代码…?我的代码只有在重定向到index2.html时才起作用
if(states[networkState] != states[Connection.NONE]){