deviceReady 无法在 PhoneGap 应用程序中工作,如何处理?
我有一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
尝试这样:
Try it this way :
@GPRathour 提供的功能有效,因为
document.addEventListener()
正在侦听deviceready
,如果为真,则运行警报函数。我没有按照你的方式工作,因为有两个原因:1)当 DOM 加载并到达你的 body 标签时,它正在调用 OnDeviceReady() ,并且侦听器从未接到呼叫,因此 PhoneGap 不知道它已准备好跑步。
2)您必须从函数内调用您的侦听器并使用“false”:
查看phonegap API以了解为什么在侦听器中使用 false 而不是 true,与默认设置有关,但值得一读了解phonegap监听器是如何工作的。
What @GPRathour provided works because
document.addEventListener()
is listening fordeviceready
, 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':
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.
以防万一您遇到与我相同的问题,我不知道是否需要在您的index.html中包含cordova.js脚本,您不必提供文件或参考,只需包含这一行:
和那么 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:
and then Cordova will use the library when compiling, after that the event is dispatched.
这段代码对我来说
是快乐的编码......
this code work for me
happy coding.......
将 PhoneGap 3.0 与 WP8 结合使用时,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.
在您的文档准备中添加 deviceready 的事件侦听器...
您不想调用 onDeviceReady() 因为这将在您添加侦听器时运行该函数...
Add you event listener for deviceready inside you doc ready...
You dont want to call onDeviceReady() as this will run the function when you add the listener...
在一个或多个平台上未触发 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.
检查您在项目中添加的 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.
就我而言,我需要删除
元 http-equiv="Content-Security-Policy" content="...
In my case, I needed to remove
meta http-equiv="Content-Security-Policy" content="...
在定义处理程序之前,您需要将处理程序绑定到 deviceready。
正确的是:
显然您的phonegap-2.0.0.js 文件(或其他版本)应该包含在此时之前的文件中。
You're binding a handler to deviceready before you've defined the handler.
Correct would be:
And obviously your phonegap-2.0.0.js file (or other version) should be included in the file before this point.
确保以下路径正确并且两者都需要包含在 html 中:
Make sure below path is correct and both are need to be included into html :
我在您的代码中看到一个问题,在该方法中,您需要在此处添加
onDeviceReady()
equals:这对我有用!
I'm see in your code one problem, in the method, you need add
onDeviceReady()
equals here:that worked for me!!
PhoneGap 示例的最大问题是不正确的 JavaScript 语法。请小心这个..对于这个问题,onDeviceReady应该有大括号...
Biggest problem with PhoneGap examples are incorrect javascript syntax. Please be careful with this.. for this question,onDeviceReady should have braces...