Phonegap没有互联网提醒弹出问题

时间:2015-02-11 22:10:45

标签: javascript jquery html cordova

我在网上搜索,当应用程序打开时告诉连接类型时我会收到此脚本。

<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">

2 个答案:

答案 0 :(得分:0)

我没有将states[]值分配给字符串,而是使用true/false并且知道NONEUNKOWN都是没有连接的指标我设置如下:

    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
 }

答案 1 :(得分:0)

这可能是一个更好的选择,可以选择离线&#39;离线&#39;事件,然后在事件触发时显示未连接的消息/警报。您可以在“设备已准备好”的同时将此消息与您显示的消息一起使用。事件火灾。

确保安装了org.apache.cordova.network-information插件才能使其正常工作。

  

当先前连接的设备丢失时,将触发离线事件   网络连接使应用程序无法再访问   互联网。它依赖于与Connection API相同的信息   当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