In JavaScript, the securitypolicyviolation event fires when a Content Security Policy (CSP) is violated (MDN). Listen on document (or window) to log or report what was blocked.
Register a listener with addEventListener. Check readiness and browser support before you rely on this event.
Syntax
Listen for securitypolicyviolation. Start with this line.
JavaScript
addEventListener("securitypolicyviolation", fn)
Call
What it does
addEventListener("securitypolicyviolation", fn)
Primary signature.
Built into the browser. Listen with document.addEventListener("securitypolicyviolation", …) — no import required.
Library declarations
JavaScript
addEventListener("securitypolicyviolation", fn)
Parameters
This member has no parameters beyond those shown in the signature.
Return Value
Return Value
Description
*
Fires as a Document event (listener receives the Event object).
Attach first. The listener must exist before you dispatch.
Build the event.new SecurityPolicyViolationEvent(…) fills demo fields.
Dispatch on document. Same target MDN recommends for real CSP events.
Label it clearly. This teaches the API — a real violation still needs a CSP policy.
Compatibility
Browser Support
Documentsecuritypolicyviolation is Baseline Widely available (MDN: since March 2022). Safe to use in modern evergreen browsers.
✓ Baseline · Widely available
securitypolicyviolation
Fires when the document Content Security Policy is violated.
99%Widely available
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
securitypolicyviolationExcellent
Bottom line: Listen on document or window, log SecurityPolicyViolationEvent fields, and pair with a real CSP policy for production monitoring.
Frequently Asked Questions
It fires when a Content Security Policy (CSP) for the document is violated — for example when a blocked script, image, or style is rejected by the policy (MDN).
No. MDN marks Document securitypolicyviolation as Baseline Widely available (since March 2022). It is not Deprecated, Experimental, or Non-standard.
Yes. MDN states it bubbles to the Window object and is composed. Still, you should usually listen on document or window. Blocked resources typically target document directly.
A SecurityPolicyViolationEvent, which inherits from Event. Useful properties include blockedURI, violatedDirective (alias of effectiveDirective), originalPolicy, disposition, and sample.
On a top-level object: Document or Window. MDN notes that although HTML elements can theoretically be targets, a blocked image typically fires with document as the target — not bubbling from the img.
It indicates whether the user agent enforces the policy ("enforce") or just reports it ("report"), such as with Content-Security-Policy-Report-Only.