科尔多瓦。无法向按钮添加事件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了科尔多瓦。无法向按钮添加事件相关的知识,希望对你有一定的参考价值。
我正在尝试使用事件监听器在单独的JS脚本中使按钮执行某些操作。我在浏览器中遇到此错误(指的是<body onload="onLoad()">
行):Refused to execute inline event handler because it violates the following Content Security Policy directive...这是我的html:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link rel="stylesheet" type="text/css" href="css/index.css">
<title>Test</title>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</head>
<body onload="onLoad()">
<div id="left_panel">
<div class="btn" id="add" onclick="insert()">Add</div>
</div>
<div id="list">
</div>
</body>
</html>
JS脚本:
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
document.addEventListener("pause", onPause, false);
document.addEventListener("resume", onResume, false);
document.addEventListener("menubutton", onMenuKeyDown, false);
document.getElementById("list").addEventListener("insert", insert, false);
}
function insert() {
...
}
function onPause() {}
function onResume() {}
function onMenuKeyDown() {}
答案
消息显示您的错误位于HTML文档的第三行。有内容安全策略,请仔细阅读错误并添加带有正确参数的'script-src'以激活javascript的执行:
script-src 'self' 'unsafe-inline' 'unsafe-eval'
另一答案
试试这个 :
添加到配置.xml:
<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<platform name="android">
<allow-intent href="market:*" />
</platform>
并在index.html中:
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'; img-src * data: 'unsafe-inline'; connect-src * 'unsafe-inline'; frame-src *;">
<meta http-equiv="Content-Security-Policy" content="default-src * gap://ready file:; style-src 'self' 'unsafe-inline' *; script-src 'self' 'unsafe-inline' 'unsafe-eval' *">
以上是关于科尔多瓦。无法向按钮添加事件的主要内容,如果未能解决你的问题,请参考以下文章