1

请看一下并帮助我解决此问题。我头痛了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>
4

4 回答 4

4

那件事太可怕了,而且有很多问题,不值得使用它。相反,只需通过检查window.device.

演示: jsFiddle

Javascript:

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';
    } );

};

HTML:

should fail on desktop after 5 seconds
<div id="result">phonegap:</div>
于 2013-03-25T05:42:36.990 回答
1

这段代码对我有用

<body onload="init()"></body>

function init() {
    document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
    // Now safe to use the Cordova API
}

快乐的编码......

于 2014-04-08T14:29:17.870 回答
1

似乎您不知道要触发此事件,您还需要包含phonegap.js文件,
并且还像我一样使用它,以便首先加载您的视图,然后应用程序处理 phonegap 否则用户将不得不等待更多时间。

<html>
  <head>
  ....
  <title>Index file</title>
   </head>
 <body>
   ....


    <script type="text/javascript" src="phonegap.js"></script> //you forgot this
</body>
</html>
于 2014-04-29T09:36:01.970 回答
1

请看以下来自 PhoneGap 网站的示例。将事件侦听器放入 onLoad() 并将其从 jQuery 就绪函数中删除。

<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 由两个代码库组成:native 和 JavaScript。在加载本机代码时,会显示自定义加载图像。但是,只有在 DOM 加载后才会加载 JavaScript。这意味着您的 Web 应用程序可能会在加载之前调用 Cordova JavaScript 函数。

一旦 Cordova 完全加载,就会触发 Cordova deviceready 事件。设备启动后,您可以安全地调用 Cordova 函数。

通常,一旦 HTML 文档的 DOM 加载完毕,您将希望使用 document.addEventListener 附加一个事件侦听器。

于 2013-03-25T05:22:46.907 回答