我在网上搜索,当应用程序打开时告诉连接类型时,我得到了这个脚本。
<script type="text/javascript" src="cordova-2.5.0.js"></script>
<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>但是我想要它做的是在没有互联网的情况下显示警报,所以我找到了一个脚本,它有一个条件语句,但是它不能工作。
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");
}请有人指点我的出路,我也有一份我提交的表格,如果没有网上提交,我希望这份表格也能返回到同一页。
<form id="loginform" name="loginform" method="POST" action="http://mysite/login.php">发布于 2015-02-11 22:29:09
我没有将states[]值分配给字符串,而是使用true/false,并且知道NONE和UNKOWN都是我设置的没有连接的指示器:
states[Connection.UNKNOWN] = false;
states[Connection.ETHERNET] = true;
states[Connection.WIFI] = true;
states[Connection.CELL_2G] = true;
states[Connection.CELL_3G] = true;
states[Connection.CELL_4G] = true;
states[Connection.CELL] = true;
states[Connection.NONE] = false;然后你就可以:
if (states[networkState] === false) {
//do something
}发布于 2015-02-12 00:13:10
如果连接到“脱机”事件,然后在事件触发时显示未连接的消息/警报,则可能是更好的选择。当“deviceready”事件发生时,您可以在显示消息的同时使用此信息。
请确保安装了org.apache.cordova.network-information插件,以使这些插件都能正常工作。
当先前连接的设备失去网络连接,以致应用程序无法再访问Internet时,脱机事件将触发。它依赖于与Connection相同的信息,并在connection.type的值变为NONE时触发。
document.addEventListener("offline", onOffline, false);
function onOffline() {
// Handle the offline event
}参考: https://github.com/apache/cordova-plugin-network-information/blob/master/doc/index.md#offline
https://stackoverflow.com/questions/28465524
复制相似问题