在Chrome与Chrome应用中,Javascript的行为有所不同

时间:2012-07-25 12:27:27

标签: javascript google-chrome-extension code-access-security

我正在学习如何开发离线Chrome应用。

我有三个组件:main.html,main.css,main.js

Main.js使用以下标记加载到main.html / html / head / script中:

<script type="text/javascript" src="main.js"></script>

并且正文标记中的onLoad事件触发了初始操作:

<body onLoad="initOnLoad(); ">

函数initOnLoad()操作由其HTML id找到的一些元素:

...
1:  function initOnLoad() {
2:     alert("initOnLoad started");
3:     initWaveArray();
4:     addWaveButtons(document.getElementById("wave_selector"));
5:     addBandButtons(document.getElementById("band_selector"));
6:     e = document.getElementById("wave_HF");
7:     activeWaveDiv = e;
8:     setActiveWave();
9:     initDateTime();
10: }

第4行和第5行的调用将一些元素填充到main.html中。

现在,当我在开发期间在Chrome中加载此页面时,一切都按预期工作。但是,当我将项目加载为Chrome应用时,我在 manifest.json 中拥有这些属性:

"app": {"launch": {"local_path": "main.html"}},
"web_accessible_resources": [
  "main.js", "main.css", "main.html"]
...

当我启动应用程序时,它会加载页面,启动 initOnLoad()功能,但第4行和第5行中对 document.getElementById()的调用会产生 null 引用,因此HTML文档中未加载任何内容。

虽然这显然与安全有关,但我无法找到我的错误。

0 个答案:

没有答案
相关问题