Document: securitypolicyviolation event
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2022.
Sự kiện securitypolicyviolation được kích hoạt khi Content Security Policy bị vi phạm.
Sự kiện được kích hoạt trên tài liệu khi có vi phạm chính sách CSP của tài liệu (và cũng có thể nổi bọt từ các phần tử trong tài liệu).
Sự kiện này nổi bọt đến đối tượng Window, và được cấu thành.
Note:
Bạn nói chung nên thêm trình xử lý cho sự kiện này vào một đối tượng cấp cao (tức là Window hoặc Document).
Mặc dù các phần tử HTML về mặt kỹ thuật có thể là mục tiêu của sự kiện securitypolicyviolation, nhưng trong thực tế sự kiện này không được kích hoạt trên chúng — ví dụ: một nguồn <img> bị chặn trực tiếp kích hoạt sự kiện này trên document làm mục tiêu, thay vì nổi bọt từ phần tử <img>.
Cú pháp
Sử dụng tên sự kiện trong các phương thức như addEventListener(), hoặc đặt một thuộc tính trình xử lý sự kiện.
addEventListener("securitypolicyviolation", (event) => { })
onsecuritypolicyviolation = (event) => { }
Loại sự kiện
Một SecurityPolicyViolationEvent. Kế thừa từ Event.
Ví dụ
Đoạn mã dưới đây cho thấy cách bạn có thể thêm một hàm trình xử lý sự kiện bằng thuộc tính trình xử lý sự kiện onsecuritypolicyviolation hoặc addEventListener() trên Document.
document.onsecuritypolicyviolation = (e) => {
// Xử lý SecurityPolicyViolationEvent e tại đây
};
document.addEventListener("securitypolicyviolation", (e) => {
// Xử lý SecurityPolicyViolationEvent e tại đây
});
Đặc tả kỹ thuật
| Specification |
|---|
| HTML> # handler-onsecuritypolicyviolation> |
Tương thích trình duyệt
Xem thêm
- Sự kiện
unsupported templ: domxref("element/securitypolicyviolation_event", "securitypolicyviolation")} của giao diện {{ - Sự kiện
unsupported templ: domxref("workerglobalscope/securitypolicyviolation_event", "securitypolicyviolation")} của giao diện {{ - HTTP > Content Security Policy