deviceReady 无法在 PhoneGap 应用程序中工作,如何处理?

发布于 2024-12-28 12:28:08 字数 1675 浏览 1 评论 0原文

我有一个简单的 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>

这里发生的情况是警报 alert ('123'); 永远不会被触发。但是如果我取出其他 JavaScript 代码并只留下警报,它就会被触发。

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

这里发生了什么,为什么 deviceready有没有被解雇的

想法?

I have a simple PhoneGap application as fallows:

<!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'); never gets fired. But if I take out the other JavaScript code and leave only the alert it is fired.

Also if I use $(document).ready(function () { alert ('123'); } I get the alert.

What is happening here, why the deviceready is not getting fired?

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

窗影残 2025-01-04 12:28:08

尝试这样:

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

Try it this way :

 document.addEventListener("deviceready", function(){
      alert("123");
 },true);
终止放荡 2025-01-04 12:28:08

@GPRathour 提供的功能有效,因为 document.addEventListener() 正在侦听 deviceready,如果为真,则运行警报函数。我没有按照你的方式工作,因为有两个原因:

1)当 DOM 加载并到达你的 body 标签时,它正在调用 OnDeviceReady() ,并且侦听器从未接到呼叫,因此 PhoneGap 不知道它已准备好跑步。

2)您必须从函数内调用您的侦听器并使用“false”:

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

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

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

查看phonegap API以了解为什么在侦听器中使用 false 而不是 true,与默认设置有关,但值得一读了解phonegap监听器是如何工作的。

What @GPRathour provided works because document.addEventListener() is listening for deviceready, then if true, running your alert function. 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.

2) you would have to call your listener from within a function and use '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.

中性美 2025-01-04 12:28:08

以防万一您遇到与我相同的问题,我不知道是否需要在您的index.html中包含cordova.js脚本,您不必提供文件或参考,只需包含这一行:

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

和那么 Cordova 将在编译时使用该库,然后调度事件。

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:

<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.

海之角 2025-01-04 12:28:08

这段代码对我来说

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

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

是快乐的编码......

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.......

画▽骨i 2025-01-04 12:28:08

PhoneGap 3.0WP8 结合使用时,Device Ready 将无法工作,因为 Phonegap.js 未包含在 Visual Studio 解决方案中。

目前的解决方案是手动包含它。

When using PhoneGap 3.0 with WP8 Device Ready will not work because Phonegap.js is NOT INCLUDED in the Visual Studio solution.

The solution is to include it manually for now.

抹茶夏天i‖ 2025-01-04 12:28:08

在您的文档准备中添加 deviceready 的事件侦听器...

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

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

您不想调用 onDeviceReady() 因为这将在您添加侦听器时运行该函数...

Add you event listener for deviceready inside you doc ready...

<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...

爱的故事 2025-01-04 12:28:08

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

The main reason for unfired ondeviceready event on one or more platform, is when you try use cordova/phonegap js of the wrong platform.

秋意浓 2025-01-04 12:28:08

检查您在项目中添加的 Cordova/phonegap 库/jar 文件的版本(在 libs 下)和 <脚本 src="~/Scripts/cordova-3.0.0.js"/>您在 HTML 文件中引用的脚本。如果存在不匹配,则 <脚本>>可能无法在项目中引用它。所以科尔多瓦无法执行它的功能。

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. If there is a mismatch, the < script/> may not be able to refer it in the project. So that cordova fails to execute it's functionality.

拥醉 2025-01-04 12:28:08

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

In my case, I needed to remove
meta http-equiv="Content-Security-Policy" content="...

请恋爱 2025-01-04 12:28:08

在定义处理程序之前,您需要将处理程序绑定到 deviceready。

正确的是:

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

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

显然您的phonegap-2.0.0.js 文件(或其他版本)应该包含在此时之前的文件中。

You're binding a handler to deviceready before you've defined the handler.

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.

那些过往 2025-01-04 12:28:08

确保以下路径正确并且两者都需要包含在 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>

Make sure below path is correct and both are need to be included into 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>
风铃鹿 2025-01-04 12:28:08

我在您的代码中看到一个问题,在该方法中,您需要在此处添加 onDeviceReady() equals:

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

这对我有用!

I'm see in your code one problem, in the method, you need add onDeviceReady() equals here:

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

that worked for me!!

热风软妹 2025-01-04 12:28:08

PhoneGap 示例的最大问题是不正确的 JavaScript 语法。请小心这个..对于这个问题,onDeviceReady应该有大括号...

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

Biggest problem with PhoneGap examples are incorrect javascript syntax. Please be careful with this.. for this question,onDeviceReady should have braces...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文