简体   繁体   English

deviceReady无法在PhoneGap应用程序中运行,如何操作?

[英]deviceReady not working in PhoneGap application, how to?

I have a simple PhoneGap application as fallows: 我有一个简单的PhoneGap应用程序作为休闲:

<!DOCTYPE HTML>
<html>
    <head>
        <title>PhoneGap powered App</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="css/jquery.mobile-1.0.min.css" />
        <script type="text/javascript" charset="utf-8" src="phonegap-1.3.0.js"></script>
        <script src="js/jquery-1.7.1.min.js"></script>
        <script src="js/jquery.mobile-1.0.min.js"></script>


    <script type="text/javascript" charset="utf-8">

        document.addEventListener("deviceready", onDeviceReady, true); 
        function onDeviceReady() {
            alert ('123');
        }
    </script>

    </head>
    <body onload="onDeviceReady()">
        <div data-role="page">

            <div data-role="header">
                <h1>title</h1>
            </div><!-- /header -->

            <div data-role="content">   
                <h2>Begin by inserting your credentials.</h2>
                ...
            </div><!-- /content -->

        </div><!-- /page -->

        <script type="text/javascript" charset="utf-8">
            $(document).ready(function () {

            });
        </script>
    </body>
</html>

What happens here is that the alert alert ('123'); 这里发生的是警报alert ('123'); never gets fired. 永远不会被解雇 But if I take out the other JavaScript code and leave only the alert it is fired. 但是,如果我取出其他JavaScript代码并仅保留警报就会被解雇。

Also if I use $(document).ready(function () { alert ('123'); } I get the alert. 另外,如果我使用$(document).ready(function () { alert ('123'); }我会收到警报。

What is happening here, why the deviceready is not getting fired? 这里发生了什么,为什么deviceready没有被解雇?

Any ideas? 有任何想法吗?

Try it this way : 试试这种方式:

 document.addEventListener("deviceready", function(){
      alert("123");
 },true);

What @GPRathour provided works because document.addEventListener() is listening for deviceready , then if true, running your alert function. @GPRathour提供的功能是因为document.addEventListener()正在侦听deviceready ,如果为true,则运行alert函数。 I didn't work how you had it because of two reasons: 由于两个原因,我没有按你的方式工作:

1) when the DOM loaded and got down to your body tag it was calling OnDeviceReady() and the listener never got the call, so phonegap doesn't know it's ready to run. 1)当DOM加载并下载到你的body标签时,它调用OnDeviceReady()并且监听器从未接到调用,因此phonegap不知道它已准备好运行。

2) you would have to call your listener from within a function and use 'false': 2)你必须在函数内调用你的监听器并使用'false':

function init(){
  document.addEventListener("deviceready", onDeviceReady, false);
}

function onDeviceReady(){
  alert('123');
}

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

Check out the phonegap API as to why to use false instead of true in your listener, has to do with the default setting, but it's worth the read to understand how phonegap listeners work. 查看phonegap API,了解为什么在监听器中使用false而不是true,与默认设置有关,但值得阅读以了解phonegap监听器的工作原理。

Just in case you have the same problem as me, I didn't know is needed to include the cordova.js script in your index.html, you don't have to provide the file or the reference, just include this line: 如果您遇到与我相同的问题,我不知道是否需要在index.html中包含cordova.js脚本,您不必提供文件或引用,只需包含以下行:

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>  

and then Cordova will use the library when compiling, after that the event is dispatched. 然后Cordova将在编译时使用该库,之后将调度该事件。

this code work for me 这段代码对我有用

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

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

happy coding....... 快乐的编码.......

When using PhoneGap 3.0 with WP8 Device Ready will not work because Phonegap.js is NOT INCLUDED in the Visual Studio solution. 当使用带有WP8的 PhoneGap 3.0时 ,Device Ready将无法正常工作,因为Visual Studio解决方案中不包含Phonegap.js

The solution is to include it manually for now. 解决方案是暂时手动包含它。

在一个或多个平台上发布未完成ondeviceready事件的主要原因是当你尝试使用错误平台的cordova / phonegap js时。

Add you event listener for deviceready inside you doc ready... 在你准备好doc的内部为你的设备添加事件监听器...

<script type="text/javascript" charset="utf-8">
    $(document).ready(function () {
         document.addEventListener("deviceready", onDeviceReady, true); 
    });

    function onDeviceReady() {
        alert ('123');
    }
</script>

You dont want to call onDeviceReady() as this will run the function when you add the listener... 您不想调用onDeviceReady(),因为这将在您添加侦听器时运行该函数...

Check the versions of the Cordova/phonegap library/jar files that you have added with the project (under libs) and the < script src="~/Scripts/cordova-3.0.0.js"/> script that you are referring in the HTML files. 检查随项目添加的Cordova / phonegap库/ jar文件的版本(在libs下)和您所指的<script src =“〜/ Scripts / cordova-3.0.0.js”/>脚本HTML文件。 If there is a mismatch, the < script/> may not be able to refer it in the project. 如果不匹配,<script />可能无法在项目中引用它。 So that cordova fails to execute it's functionality. 因此,cordova无法执行它的功能。

就我而言,我需要删除元http-equiv =“Content-Security-Policy”content =“...

You're binding a handler to deviceready before you've defined the handler. 在定义处理程序之前,您将处理程序绑定到deviceready。

Correct would be: 正确的是:

function onDeviceReady(){
    alert('123')
}

document.addEventListener("deviceready", onDeviceReady, false);

And obviously your phonegap-2.0.0.js file (or other version) should be included in the file before this point. 显然,在此之前,你的phonegap-2.0.0.js文件(或其他版本)应该包含在文件中。

Make sure below path is correct and both are need to be included into html : 确保下面的路径是正确的,并且两者都需要包含在html中:

        <script type="text/javascript"  src="cordova.js"></script>
        <script src="js/jquery-1.11.2.min.js"></script>

  <script type="text/javascript">
        $(document).ready(function(){
        document.addEventListener("deviceready", onDeviceReady, false);
        });

        function onDeviceReady() {
        alert("inside onDeviceReady");
        }
        </script>

I'm see in your code one problem, in the method, you need add onDeviceReady() equals here: 我在你的代码中看到一个问题,在方法中,你需要在这里添加onDeviceReady()等于:

document.addEventListener("deviceready", onDeviceReady(), false);

that worked for me!! 这对我有用!!

Biggest problem with PhoneGap examples are incorrect javascript syntax. PhoneGap示例的最大问题是不正确的javascript语法。 Please be careful with this.. for this question,onDeviceReady should have braces... 请小心这个...对于这个问题,onDeviceReady应该有括号......

document.addEventListener("deviceready", onDeviceReady(), true); 
function onDeviceReady() {
    alert ('123');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM