我正在使用以下代碼的cordova-plugin-network-information插件。cordova - 先檢查網絡連接,然後加載正文頁面
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
checkConnection();
}
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';
if(networkState != Connection.NONE){
window.location.href = "index2.html";
} else{
document.getElementById('custom-message').style.display = "block";
}
}
</script>
這個插件工作正常,但它重定向到第二頁(索引2)。
我想以這樣的方式,首先檢查連接&然後加載相同的頁面索引頁面(無需創建索引頁面),如果失敗(沒有互聯網),然後拋出自定義錯誤消息。
感謝
你有什麼alredy試過嗎? – valepu