在离线事件phonegap应用程序中显示div

时间:2017-06-19 02:29:40

标签: javascript jquery cordova phonegap-plugins phonegap

我正在使用phonegap创建webview应用程序
我使用iframe方法来获取我的网站

<iframe src="http://www.google.com" id="iframe"></iframe>

然后我安装cordova-plugin-network-information
https://github.com/apache/cordova-plugin-network-information
并将离线事件监听器添加到我的javascript代码中以显示div并在离线模式下隐藏iframe

    function onLoad() {
    document.addEventListener('deviceready', onDeviceReady.bind(this), false);
    function onDeviceReady() {
        setTimeout(function () {
            navigator.splashscreen.hide();
        }, 50);
    }
    document.addEventListener("offline", onOffline, false);
    function onOffline() {
        $("#iframe").hide();
        $(".error").show();
    }
}
无论如何它都无法正常工作 我回顾了几十个主题,但没有为我工作

我也尝试过但不能正常工作

// offline event
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';

           return networkState;

       }

function onDeviceReady() {
       var networkState = checkConnection();
    /* load local files if there is not network connection */
                  if (networkState == Connection.NONE) {
                      window.location="offline.html"; 
                  } 
     }

0 个答案:

没有答案