The securitypolicyviolation event fires when a Content Security Policy (CSP) is violated. Learn SecurityPolicyViolationEvent, why you usually listen on document / window, key properties such as blockedURI and violatedDirective, and five try-it labs.
01
Kind
Instance event
02
Type
SecurityPolicyViolationEvent
03
When
CSP is violated
04
Handler
onsecuritypolicyviolation
05
Bubbles?
Yes (composed)
06
Status
Baseline widely available
Fundamentals
Introduction
A Content Security Policy tells the browser which scripts, images, styles, and other resources a page may load. When something breaks that policy, securitypolicyviolation fires so you can log, debug, or report it.
The event is documented on Element, but MDN notes you should generally attach the handler to a top-level object (Window or Document). In practice, a blocked resource (such as an <img>) often targets document directly rather than bubbling from the element.
💡
Beginner tip
Prefer document.addEventListener("securitypolicyviolation", ...) or window for reliable coverage. Read blockedURI and violatedDirective first—they answer “what was blocked?” and “which rule stopped it?”
Concept
Understanding securitypolicyviolation
An instance event that answers: “Did this page break its Content Security Policy?”
Trigger — a resource or inline content violates CSP.
Event type — SecurityPolicyViolationEvent.
Bubbles & composed — reaches Window.
Listen high — prefer document / window.
Baseline Widely available on MDN (since October 2018).
Context
🔒 CSP in One Minute
Policies can arrive via HTTP headers or a <meta http-equiv="Content-Security-Policy"> tag. Example that blocks remote images while allowing inline scripts for demos:
securitypolicyviolation is marked Baseline Widely available on MDN (since October 2018). Logos use the shared browser-image-sprite.png sprite from this project. Listen on document or window and read SecurityPolicyViolationEvent details.
✓ Baseline · Widely available
Element securitypolicyviolation
Fires when Content Security Policy is violated. Chrome 41+, Firefox 63+, Safari 10+, Edge 15+, Opera 28+.
FullWidely available
Google Chrome41+
Supported
Mozilla Firefox63+
Supported
Apple Safari10+
Supported
Microsoft Edge15+
Supported
Opera28+
Supported
Internet ExplorerNot supported
Unavailable
securitypolicyviolationBaseline
Bottom line: Attach to document/window, inspect blockedURI and violatedDirective, and use disposition to tell enforce from report-only.
Wrap Up
Conclusion
securitypolicyviolation is the standard client-side signal that CSP blocked or reported something. Listen on document or window, read SecurityPolicyViolationEvent fields, and use that data to harden your policy safely.
Register the listener before triggering test loads
Use Report-Only CSP while tuning a new policy
Prefer addEventListener for multiple handlers
❌ Don’t
Rely on listening only on a child <img> / script element
Ignore disposition when mixing enforce and report-only
Ship a broken CSP without monitoring violations
Assume every property is filled for every violation type
Overwrite onsecuritypolicyviolation if you need multiple listeners
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about securitypolicyviolation
CSP broke something—listen high, read blockedURI and the directive.
5
Core concepts
🔒01
On violate
CSP blocked / reported
Trigger
📄02
Event
SecurityPolicyViolationEvent
API
🔍03
Listen
document / window
Target
🚩04
Fields
blockedURI + directive
Data
🎯05
Baseline
since Oct 2018
Status
❓ Frequently Asked Questions
It fires when a Content Security Policy (CSP) is violated—for example when a blocked script, image, or style is refused by the policy.
No. MDN marks Element securitypolicyviolation as Baseline Widely available (since October 2018). It is not Deprecated, Experimental, or Non-standard.
A SecurityPolicyViolationEvent, which inherits from Event. Useful properties include blockedURI, violatedDirective, effectiveDirective, originalPolicy, and disposition.
MDN recommends adding the handler on a top-level object such as Window or Document. In practice the event often targets document (for example a blocked image), rather than bubbling from the HTML element.
Yes. It bubbles to the Window object and is composed, so top-level listeners can observe violations from the page.
A string indicating whether the user agent is configured to enforce the policy or only report the violation (enforce vs report).
Did you know?
A blocked <img> usually fires securitypolicyviolation with document as the target—not the image element itself. That is why MDN steers you toward top-level listeners.