gpt4 book ai didi

javascript - Cordova 事件 DeviceReady 不适用于所有页面

转载 作者:行者123 更新时间:2023-12-03 08:17:17 27 4
gpt4 key购买 nike

我有多个页面

在cordova中,当它启动时,它首先显示index.html并触发javascript上的deviceready事件,当我点击类似搜索的链接时。 html 时,WebView 更改并加载 search.html 但不触发 deviceready javascript 事件。

So, right now i use onload in another pages because i think deviceready event only trigger when the App is ready to work(only one time at the start).

示例(index.html,这个效果很好):

<!DOCTYPE HTML>
<html>
<head>
...
<script>
document.addEventListener("deviceready", function(){
console.log('Device is Ready!!');
}, false);
</script>
...
</head>
<body>
<a href="search.html">Go to search</a>
</body>
</html>

示例(XYZ.html,这不会,因为永远不会触发deviceready):

<!DOCTYPE HTML>
<html>
<head>
...
<script>
document.addEventListener("deviceready", function(){
console.log('Device is Ready!!');
}, false);
</script>
...
</head>
<body>
<a href="index.html">Go to Index</a>
</body>
</html>

文件结构示例:

+ www/
- .
- ..
+ js/
- jquery.min.js
- bootstrap.min.js
+ css/
- bootstrap.min.css
+ img/
- logo.png
- default.png
+ icons/
- alert.png
- danger.png
- important.png
- icon.png
- index.html
- searh.html
- favorites.html
- contacts.html
- configuration.html
- about.html

配置.xml

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.cordovacrosswalk.app" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>CordovaAndCrosswalkApp</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<content src="index.html" />
<plugin name="cordova-plugin-whitelist" spec="1" />
<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
<allow-intent href="market:*" />
</platform>
<platform name="ios">
<allow-intent href="itms:*" />
<allow-intent href="itms-apps:*" />
</platform>
<preference name="xwalkVersion" value="15+" />
<preference name="xwalkCommandLine" value="--disable-pull-to-refresh-effect" />
<preference name="xwalkMode" value="embedded" />
<preference name="xwalkMultipleApk" value="true" />
</widget>

非常感谢!

最佳答案

@olaf,
你需要read the documentation和博客 Phonegap BuildCordova 。 Cordova/Phonegap 是一团糟。注意:Cordova 是*许多* hybrid platforms 的基础,包括Phonegap、Phonegap Build、Ionic、Sencha Touch等

这些常见问题解答也应该有所帮助。

您的修复

  1. 替换config.xml
  2. 添加到 index.html
  3. 关于您对 onload()deviceready 的困惑。
    阅读 4. In the code, did not listen for the 'deviceready' event.Top Mistakes by Developers new to Cordova/Phonegap

只是为了澄清这一点。 deviceready 等待直到加载完毕。所以理论上,它正在等待onload完成。我可以验证这一点。我刚刚完成了new boilerplate适用于 Phonegap Build 的最新版本 (cli-5.2.0)。

加载顺序为启动屏幕 -> onload() -> ondeviceready() -> 启动屏幕 hide()

更新:最后一件事。我有几个可以处理多个页面的程序,所以我知道大多数问题

祝你好运

1。替换config.xml

请注意,您的应用程序现在不安全。保护您的应用程序的安全取决于您。

<?xml version='1.0' encoding='utf-8'?>
<!-- your header is off. READ http://docs.build.phonegap.com/en_US/configuring_basics.md.html#The%20Basics -->
<widget xmlns = "http://www.w3.org/ns/widgets"
xmlns:gap = "http://phonegap.com/ns/1.0"
id = "com.cordovacrosswalk.app"
version = "0.0.1"
versionCode = "10"> <!-- versionCode is optional and Android only -->

<name>CordovaAndCrosswalkApp</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>

<!-- ONLY needed for IDE/SDK -->
<!-- <content src="index.html" /> -->

<!-- Tool Set Version -->
<!-- <preference name='phonegap-version' value='3.7.0' /> --> <!-- turns off required whitelist -->
<!-- <preference name='phonegap-version' value='cli-5.2.0' /> -->

<!-- Target Platforms -->
<platform name="android" />
<platform name="ios" />
<platform name="windows" />

<!-- You'll very likely need this on different platform. So you can tell them apart -->
<plugin name="cordova-plugin-device" source="npm" version="1.0.1" />

<!-- WHITELIST * WHITELIST * WHITELIST -->
<!-- https://github.com/jessemonroy650/top-phonegap-mistakes/blob/master/the-whitelist-system.md -->
<plugin name="cordova-plugin-whitelist" source="npm" spec="1.1.0" />
<allow-navigation href="*" />
<allow-intent href="*" />
<access origin="*" /> <!-- Required for iOS9 -->

<plugin name='org.crosswalk.engine' version='1.3.0' source='pgb' />
<preference name="xwalkVersion" value="15+" />
<preference name="xwalkCommandLine" value="--disable-pull-to-refresh-effect" />
<preference name="xwalkMode" value="embedded" />
<preference name="xwalkMultipleApk" value="true" />
</widget>

2。添加到您的 index.html

请注意,您的应用程序现在不安全。保护您的应用程序的安全取决于您。

添加到每个 HTML 文件的顶部。
阅读 HOW TO apply the Cordova/Phonegap the whitelist system关于CSP

的部分
<meta http-equiv="Content-Security-Policy" 
content="default-src *;
style-src 'self' 'unsafe-inline' 'unsafe-eval';
script-src 'self' 'unsafe-inline' 'unsafe-eval';">

关于javascript - Cordova 事件 DeviceReady 不适用于所有页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33883422/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com