如何检查移动互联网连接
How to check mobile internet connection
我必须根据互联网/连接可用性调用两个函数。
如果存在互联网连接,则调用插入函数,否则调用更新。
我试过这样:
var online = window.navigator.onLine;
if (online==ture) {
alert('connection down.');
}else
{
alert('Please check your internet connection and try again');
}
此代码仅适用于浏览器,不适用于设备。在这两种情况下,它总是显示alert('connection down');
,即是否存在互联网。
请指导此代码,因为我是Phonegap的新手。
使用此插件满足您的需求(https://github.com/apache/cordova-plugin-network-information)。安装后,请尝试使用此代码以检查互联网连接。
document.addEventListener("deviceready",checkInternet,false);
function checkInternet(){
if(navigator.connection.type=="none"){
//no internet connection
alert("No connection");
}else{
alert("Connection ok");
}
}
它是
HTML5 API的一部分。检查 window.navigator.onLine 的值 -- 如果用户处于脱机状态,则为 false。当浏览器联机和脱机时,指示器会更新。
<head>
<title>Online status</title>
<script>
function updateIndicator()
{
document.getElementById('indicator').textContent = navigator.onLine ? 'online' : 'offline';
}
</script>
</head>
<body onload="updateIndicator()" ononline="updateIndicator()" onoffline="updateIndicator()">
<p>The network is: <span id="indicator">(state unknown)</span>
</body>
欲了解更多信息
您使用的 API 在移动设备上不起作用。您需要改用网络 API。
您需要做:
-
通过CL使用以下命令添加网络插件
cordova plugin add org.apache.cordova.network-information
-
现在获取网络状态为
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'; alert('Connection type: ' + states[networkState]); } checkConnection();
相关文章:
- 当没有互联网连接时,谷歌地图Java脚本API处理
- 如果没有互联网连接,Android WebView离线消息
- 捕获”;没有互联网连接”;AngularJS中
- 我如何通过从服务器调用gif来检查是否有互联网连接
- 如果没有互联网连接,请隐藏iframe
- 如何捕捉不稳定的互联网连接
- 使用jquery检查互联网连接
- 使用 html5 的互联网连接代码
- 如果没有互联网连接,我想显示吐司,不知道该怎么做
- WebRTC - 数据通道 - 适用于移动互联网
- Jquery 移动错误消息,当没有互联网连接加载谷歌地图
- Jquery移动检查时没有互联网连接显示错误消息,而不是加载谷歌地图
- 当互联网连接丢失时,如何从 Ajax 调用中获取信息
- Angular js,如何在调用url之前检查用户是否有互联网连接
- 互联网连接检查不起作用
- 基于英特尔 xdk html5 的应用程序只能在启用互联网连接时工作
- 在javascript中检查互联网连接的最佳实践
- 如何检查移动互联网连接
- 将jQuery移动应用程序连接到服务器数据库,并在互联网连接关闭时打开弹出窗口
- 每分钟使用jquery移动检查设备互联网连接