我正在尝试在我的 phonegap 应用程序加载时加载数据馈送。它工作得很好,除了当我没有数据连接时,我希望系统能感知到这一点,然后给出一个很好的消息,比如“对不起,你没有数据连接。”。页面加载完毕后,我可以单击一个按钮并运行一个告诉我连接状态的函数。但是,它根本不会在页面加载时自动运行此代码,我不知道为什么。当我在 iOS 或 Android 上进行测试时,我遇到了同样的问题。我只剥离了基础知识,所以希望有人无法发现问题。
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/main.css" />
<script type="text/javascript" charset="utf-8" src="js/jquery-1.7.min.js"></script>
<script type="text/javascript" charset="utf-8" src="js/main.js"></script>
<script src="js/jquery.jsonp-2.1.4.min.js"></script>
<script>
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]);
if(states[networkState] == 'No network connection') { show_no_connection(); }
}
$(document).ready(function(){
checkConnection();
});
function show_no_connection() {
$('#status').html('You Have No Data Connection!');
}
</script>
</head>
<body>
<div id="status">SO FAR, YOU HAVE A CONNECTION.</div>
<a href='javascript:void(0);' onclick='checkConnection();'>Check your connection</a><br/><br/>
<script type="text/javascript" src="cordova-2.2.0.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
</script>
</body>