javascript在chrome-app中不起作用 [英] javascript not working in chrome-app

查看:143
本文介绍了javascript在chrome-app中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开始编写chrome应用程序.由于某些原因,当我以Chrome应用程序查看时,javascript无法正常运行,但可以作为网页正常运行.这是我问题的最简单实例.

I'm trying to get started writing chrome apps. For some reason when I view as a chrome app the javascript does not work but it works fine as a webpage. Here's the simplest instance of my issue.

使用chrome打开index.html可以正常工作-按下按钮时,"hello world"字符串变为"CLICK".按下按钮后,Chrome应用程序无法运行.

opening index.html with chrome works as expected--the "hello world" string becomes "CLICK" when the button is pressed. Running as a chrome app nothing happens when the button is pressed.

manifest.json:

manifest.json:

{
  "manifest_version": 2,
  "name": "My first app",
  "version": "0.0.1",
  "app": {
    "background": {
      "scripts": ["main.js"]
    }
  }
}

main.js

chrome.app.runtime.onLaunched.addListener(function() {
  chrome.app.window.create('index.html', {
    bounds: {
      width: 800,
      height: 609
    }
  });
});

index.html:

index.html:

<!DOCTYPE html>

<html>
<head>
    <title>test</title>
</head>

<body> 
    <button type="button" onclick="myFunction()">click me</button>
    <script>
        function myFunction(){
            document.getElementById("testdiv").innerHTML = "CLICK"
            }
    </script>
    <div id="testdiv">hello world</div>
</body>
</html>

推荐答案

好吧, Chrome扩展程序的内容安全政策不允许使用内嵌脚本.

将不执行内联JavaScript.此限制禁止 内联块和内联事件处理程序(例如).

Inline JavaScript will not be executed. This restriction bans both inline blocks and inline event handlers (e.g. ).

尝试在页面(<script src="script.js" type="text/javascript"></script">)中包含一个包含javascript的脚本文件,而不是在页面内使用javascript代码.

try to include a script file containing your javascript in your page (<script src="script.js" type="text/javascript"></script">) instead of using your javascript code within the page.

希望有帮助.

这篇关于javascript在chrome-app中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆