[英]Phonegap - onDeviceReady() wasn't fired
請看看,並幫助我解決此問題。 我已經頭痛了2天。 永遠不會調用onDeviceReady函數。 這是我的代碼:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>DemoSimpleControls</title>
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
<link rel="shortcut icon" href="images/favicon.png">
<link rel="apple-touch-icon" href="images/apple-touch-icon.png">
<link href="jqueryMobile/jquery.mobile-1.3.0.css" rel="stylesheet">
<link rel="stylesheet" href="css/DemoSimpleControls.css">
<script src="jqueryMobile/jquery.mobile-1.3.0.js"></script>
<script src="../js/jquery-1.9.1.min.js"></script>
< script type="text/javascript" charset="utf-8" src="../cordova-2.5.0.js"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
// Handler for .ready() called.
document.addEventListener("deviceready", onDeviceReady, true);
});
$(function() {
document.addEventListener("deviceready", onDeviceReady, true);
});
function onDeviceReady(){
alert("ready");
$("#mysavedData").html("XYZ");
$("#mysavedData").html(window.localStorage.getItem("data"));
}
</script>
</head>
<body id="content" onLoad="onLoad();" >
<div data-role="page" id="page">
<div data-role="header" >
<a data-rel="back" href="#" >Back</a>
<h1>My page</h1>
</div>
<div data-role="content" style="padding: 15px" data-theme="e">
<div id="mysavedData">My data</div>
</div>
</div>
</body>
</html>
該事件是可怕的,有很多問題,不值得使用。 相反,只需通過檢查window.device
輪詢直到PhoneGap准備就緒。
function initializePhoneGap( success, failure ) {
var timer = window.setInterval( function () {
if ( window.device ) {
window.clearInterval( timer );
success();
};
}, 100 );
window.setTimeout( function () { //failsafe
if ( !window.device ) { //phonegap failed
window.clearInterval( timer );
failure();
};
}, 5000 ); //5 seconds
};
window.onload = function () {
initializePhoneGap( function success() {
//start app
document.getElementById( 'result' ).textContent = 'phonegap: success';
}, function failure() {
//phonegap failed
document.getElementById( 'result' ).textContent = 'phonegap: failure';
} );
};
should fail on desktop after 5 seconds
<div id="result">phonegap:</div>
在PhoneGap網站上查看以下示例。 將事件監聽器放入onLoad()並將其從jQuery ready函數中刪除。
<script type="text/javascript" charset="utf-8" src="cordova-2.5.0.js"></script>
<script type="text/javascript" charset="utf-8">
// Call onDeviceReady when Cordova is loaded.
//
// At this point, the document has loaded but cordova-2.5.0.js has not.
// When Cordova is loaded and talking with the native device,
// it will call the event `deviceready`.
//
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
// Cordova is loaded and it is now safe to make calls Cordova methods
//
function onDeviceReady() {
// Now safe to use the Cordova API
}
</script>
說明:Cordova由兩個代碼庫組成:本機代碼和JavaScript。 加載本機代碼時,將顯示自定義加載圖像。 但是,僅在DOM加載后才加載JavaScript。 這意味着您的Web應用程序可能會在加載之前調用Cordova JavaScript函數。
一旦Cordova完全加載,就會觸發Cordova deviceready事件。 觸發設備后,您可以安全地調用Cordova函數。
通常,一旦HTML文檔的DOM加載完成,您將希望將事件監聽器與document.addEventListener附加在一起。
該代碼對我有用
<body onload="init()"></body>
function init() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
// Now safe to use the Cordova API
}
快樂的編碼。
似乎您不知道要觸發此事件,還需要包含phonegap.js
文件,
並且像我一樣使用它,以便首先加載視圖,然后應用程序處理phonegap,否則用戶將不得不等待更多時間。
<html>
<head>
....
<title>Index file</title>
</head>
<body>
....
<script type="text/javascript" src="phonegap.js"></script> //you forgot this
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.