元素:securitypolicyviolation 事件
當 內容安全策略 被違反時,會觸發 securitypolicyviolation 事件。
當 CSP 策略被違反時,該事件會在元素上觸發。
語法
在諸如 addEventListener() 之類的方法中使用事件名稱,或設定事件處理程式屬性。
js
addEventListener("securitypolicyviolation", (event) => { })
onsecuritypolicyviolation = (event) => { }
事件型別
一個 SecurityPolicyViolationEvent。繼承自 Event。
示例
在 Window 上監聽 securitypolicyviolation
下面的程式碼展示瞭如何使用 onsecuritypolicyviolation 全域性事件處理程式屬性或在頂層 Window 上使用 addEventListener() 新增事件處理程式函式(您可以在 Document 上使用完全相同的方法)。
js
window.onsecuritypolicyviolation = (e) => {
// Handle SecurityPolicyViolationEvent e here
};
window.addEventListener("securitypolicyviolation", (e) => {
// Handle SecurityPolicyViolationEvent e here
});
規範
| 規範 |
|---|
| 內容安全策略級別 3 # eventdef-globaleventhandlers-securitypolicyviolation |
| HTML # handler-onsecuritypolicyviolation |
瀏覽器相容性
載入中…
另見
Document介面的Document介面的securitypolicyviolation事件WorkerGlobalScope介面的securitypolicyviolation事件- HTTP > 內容安全策略