我已编写脚本来检查网络连接但它无法正常工作。
<script type="text/javascript">
app.initialize();
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 Internet';
return states[networkState];
}
var result=checkConnection();
if(result=="No Internet")
{
alert("Status: "+states[networkState]);
}
else
{
alert("Have Internet");
}
</script>
这里在body标签中我调用onload =“checkConnection()”并将值返回到var result = checkConnection();