4 回答

TA貢獻1864條經(jīng)驗 獲得超2個贊
<a onClick=hellYeah("xxx")>
<a id="link">
addEventListener
popup.js
document.addEventListener('DOMContentLoaded', function() { var link = document.getElementById('link'); // onClick's logic below: link.addEventListener('click', function() { hellYeah('xxx'); });});

TA貢獻1853條經(jīng)驗 獲得超18個贊
原因
內(nèi)聯(lián)JavaScript不會被執(zhí)行。這一限制既禁止內(nèi)聯(lián),也禁止內(nèi)聯(lián)。 <script>
砌塊 和內(nèi)聯(lián)事件處理程序(例如: <button onclick="...">
).
如何檢測
拒絕執(zhí)行內(nèi)聯(lián)腳本,因為它違反了以下內(nèi)容安全策略指令:“script-src‘Self’chrom-擴展名:”。要么是‘不安全-內(nèi)聯(lián)’關(guān)鍵字,要么是散列(‘sha256-.’),或者是現(xiàn)在(‘none-.’)需要啟用內(nèi)聯(lián)執(zhí)行。
如何修復
<a onclick="handler()">Click this</a> <!-- Bad -->
onclick
<a id="click-this">Click this</a> <!-- Fixed -->
.js
popup.js
):
// Pure JS:document.addEventListener('DOMContentLoaded', function() { document.getElementById("click-this").addEventListener("click", handler);});// The handler also must go in a .js filefunction handler() { /* ... */}
DOMContentLoaded
<head>
<script src="popup.js"></script>
// jQuery$(document).ready(function() { $("#click-this").click(handler);});
放寬政策
問:
答:
沒有任何機制可以放松對執(zhí)行內(nèi)聯(lián)JavaScript的限制。特別是,設(shè)置腳本策略,其中包括 'unsafe-inline'
不會有任何效果。
最新情況:
從Chrome 46開始,可以通過在策略中指定源代碼的base 64編碼哈希來白化內(nèi)聯(lián)腳本。此散列必須以所使用的哈希算法(Sha 256、Sha 384或Sha 512)作為前綴??匆?/trans> 散列用法 <script>
元素 舉個例子。
onclick="code"
.

TA貢獻1836條經(jīng)驗 獲得超13個贊
我發(fā)現(xiàn),通過使用調(diào)用的ID將腳本放在DIV之前,腳本無法工作。 如果腳本在另一個DIV中,它也不能工作。 腳本必須使用document.addEventListener(‘DOMContentLoade’)編寫,函數(shù)() <body> <a id=id_page href ="#loving" onclick="load_services()"> loving </a> <script> // This script MUST BE under the "ID" that is calling // Do not transfer it to a differ DIV than the caller "ID" document.getElementById("id_page").addEventListener("click", function(){ document.getElementById("mainbody").innerHTML = '<object data="Services.html" class="loving_css_edit"; > </object>'; }); </script> </body> <div id="mainbody" class="main_body"> "here is loaded the external html file when the loving link will be clicked. " </div>
添加回答
舉報