簡體   English   中英

在線/離線連接重定向PhoneGap / Cardova?

[英]Online/Offline Connection Redirect PhoneGap/Cardova?

我正在使用PhoneGap / Cordova 3.3.x開發應用程序,但我的腳本運氣不佳,可以檢查連接是否可用,如果顯示,則顯示“ main.html”。 如果沒有連接,則顯示“ offline.html”。 現在,我的index.html當前重定向到我的“ main.html”,並且在“ index.html”中有此腳本,盡管它不起作用。.我嘗試了其他幾種腳本,但無濟於事。 讓我知道是否還需要一個合適的.js文件,我知道您以前必須在資產中包含一個phonegap.js文件的較舊的phonegap版本嗎? ::

<script>
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.NONE]     = 'No network connection';
}
  if (networkState == Connection.NONE ){
   window.location = "offline.html";
   } 
            }
        }

 window.location='./main.html';
</script>

我也嘗試過“ ./offline”,它只給我默認的android頁面不可用。 提前謝謝!

您可以在onLoad中預訂兩個事件(與設備就緒位置相同)。

這兩個事件是“在線”和“離線”:

document.addEventListener("offline", this.onReceivedOffline, false);
document.addEventListener("online", this.onReceivedOnline, false);

所以會的

document.addEventListener("offline", onOffline, false);
function onOffline()
{
    window.location = "offline.html";
}

您可以在此處了解更多信息: http : //docs.phonegap.com/en/3.3.0/cordova_events_events.md.html#online

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM