Javascript Cordova DeviceReady 未触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27865265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cordova DeviceReady not firing
提问by eb1
I've been banging my head on this one for a few days. After getting a number of "object not defined" errors when trying to create a new FileTransfer() object, it looks like the problem is more basic -- somehow the DeviceReady event is not firing.
几天来我一直在关注这个问题。在尝试创建新的 FileTransfer() 对象时遇到许多“未定义对象”错误后,问题看起来更基本了——不知何故 DeviceReady 事件没有触发。
Stack Overflow has a lot of hits on this issue, but most of them have to do with pre-3.x cordova builds that had a different architecture (I'm on 4.1.2). I've tried the suggestions in the newer topics I could find -- removing and adding plugins, updating cordova, etc. -- to no avail. To try to isolate the issue, I've commented out the startup code to just a few lines:
Stack Overflow 在这个问题上有很多成功案例,但其中大部分都与具有不同架构的 3.x 之前的cordova 版本有关(我使用的是4.1.2)。我已经尝试了我能找到的较新主题中的建议——删除和添加插件、更新科尔多瓦等——但无济于事。为了尝试隔离问题,我将启动代码注释为仅几行:
Index.html:
索引.html:
<!DOCTYPE html>
<html>
<head>
<title>Blah</title>
<meta charset="utf-8"/>
<meta name="viewport" content="initial-scale=1, user-scalable=no, minimum-scale=1, maximum-scale=1">
<link href="res/topcoat/css/topcoat-mobile-light.min.css" rel="stylesheet">
<link href="res/css/styles.css" rel="stylesheet">
<link href="res/css/pageslider.css" rel="stylesheet">
<script data-main="js/main" src="lib/require.js"></script>
</head>
Main.js:
主要.js:
require(["app/Application"], function (Application) {
"use strict";
document.addEventListener("deviceready", function(){
$('body').html("<p>device is ready</p>");
},true);
$('body').html("<p>waiting...</p>");
});
Instead of displaying "device is ready" in the body after a bit, the screen just displays "waiting...". This happens on both the iOS emulator and the browser (cordova emulate browser).
一段时间后,屏幕上不会显示“设备准备就绪”,而是显示“正在等待...”。这发生在 iOS 模拟器和浏览器(cordova 模拟浏览器)上。
Cordova info:
科尔多瓦信息:
$ cordova -v
4.1.2
Plugin info:
插件信息:
$ cordova plugins
org.apache.cordova.globalization 0.3.3 "Globalization"
(I get the same results if Globalization isn't there).
(如果不存在全球化,我会得到相同的结果)。
Is there some other place I should be looking?I'm running from the command line, if that makes a difference.
还有其他地方我应该找吗?我正在从命令行运行,如果这有区别的话。
回答by Alexander T.
I think in this case you need to include cordova.jsin your application, because I don't see cordova.jsin your example
我认为在这种情况下您需要包含cordova.js在您的应用程序中,因为我cordova.js在您的示例中没有看到
<script src="cordova.js"></script>
Note:pathto cordova.jsdepends on where it located in your app
注:路径以cordova.js取决于它位于你的应用程序
回答by A. Soufi
That did not initially fix it for me until I removed
最初并没有为我修复它,直到我删除
< meta http-equiv="Content-Security-Policy" content=".." / >
回答by Elliot B.
I recently had this same issue, but in my case cordova.jswas already properly included.
我最近遇到了同样的问题,但在我的情况下cordova.js已经被正确包含。
Eventually what worked for me was a simple removeand addof the iosplatform:
最终什么工作对我来说是一个简单的remove和add对的ios平台:
cordova platform remove ios
cordova platform add ios
It had been quite a while since I had completely re-built the iosplatform and other major changes had taken place during that time (Cordova upgrade, XCode upgrade, etc). It's possible that my config.xmlor existing iosbuild was somehow incompliant with the latest Cordova requirements.
自从我完全重建ios平台以来已经有一段时间了,在此期间发生了其他重大变化(Cordova 升级、XCode 升级等)。我的config.xml或现有的ios构建可能在某种程度上不符合最新的 Cordova 要求。
回答by Abraham Brookes
I have been trying to fix this issue for DAYS and I finally got devicereadyto trigger. The issue was that I had extended the js Objectto insert my own hide and show commands. Removing those lines allowed devicereadyto trigger:
我一直在尝试解决 DAYS 的这个问题,我终于deviceready触发了。问题是我已经扩展了 jsObject以插入我自己的隐藏和显示命令。删除那些允许deviceready触发的行:
Object.prototype.hide = function(){
this.style.display = 'none';
}
Object.prototype.show = function(){
this.style.display = 'initial';
}
note: I also had to have the line <script src="cordova.js"></script>
as mentioned by Alexander T
注意:我也必须有<script src="cordova.js"></script>
亚历山大 T 提到的那条线

