4 回答

TA貢獻1864條經驗 獲得超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條經驗 獲得超18個贊
原因
內聯JavaScript不會被執行。這一限制既禁止內聯,也禁止內聯。 <script>
砌塊 和內聯事件處理程序(例如: <button onclick="...">
).
如何檢測
拒絕執行內聯腳本,因為它違反了以下內容安全策略指令:“script-src‘Self’chrom-擴展名:”。要么是‘不安全-內聯’關鍵字,要么是散列(‘sha256-.’),或者是現在(‘none-.’)需要啟用內聯執行。
如何修復
<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);});
放寬政策
問:
答:
沒有任何機制可以放松對執行內聯JavaScript的限制。特別是,設置腳本策略,其中包括 'unsafe-inline'
不會有任何效果。
最新情況:
從Chrome 46開始,可以通過在策略中指定源代碼的base 64編碼哈希來白化內聯腳本。此散列必須以所使用的哈希算法(Sha 256、Sha 384或Sha 512)作為前綴??匆?/trans> 散列用法 <script>
元素 舉個例子。
onclick="code"
.

TA貢獻1836條經驗 獲得超13個贊
我發現,通過使用調用的ID將腳本放在DIV之前,腳本無法工作。 如果腳本在另一個DIV中,它也不能工作。 腳本必須使用document.addEventListener(‘DOMContentLoade’)編寫,函數() <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>
添加回答
舉報