The Document securitypolicyviolation event fires when a Content Security Policy (CSP) is violated. Learn how to listen on document, read SecurityPolicyViolationEvent fields like blockedURI and violatedDirective, and practice with five try-it labs.
01
Kind
Document event
02
Type
SecurityPolicyViolationEvent
03
Bubbles
Yes (to Window)
04
Composed
Yes
05
Topic
CSP violations
06
Status
Baseline · Widely available
Fundamentals
Introduction
A Content Security Policy tells the browser which scripts, images, styles, and other resources a page is allowed to load. When something breaks that policy—for example an image from a blocked host—the browser can fire securitypolicyviolation on the Document.
The event object is a SecurityPolicyViolationEvent. It carries details such as which URI was blocked and which CSP directive was violated, so you can log or report the problem without guessing.
💡
Beginner tip
Attach the listener on document or window (top level). MDN notes that a blocked <img> usually targets document directly—it does not reliably bubble from the image element.
Concept
Understanding Document securitypolicyviolation
A standard Document event that answers: “Did this page just break its Content Security Policy?”
Fires when a Content Security Policy is violated (MDN).
Bubbles to the Window and is composed.
Listen ondocument or window (recommended top-level targets).
Event type — SecurityPolicyViolationEvent (inherits from Event).
Handler — document.onsecuritypolicyviolation or addEventListener("securitypolicyviolation", ...).
Status — Baseline Widely available since March 2022 (MDN).
Foundation
📝 Syntax
Use the event name with addEventListener, or set the handler property:
event.violatedDirective or event.effectiveDirective
Full policy text
event.originalPolicy
Enforce vs report
event.disposition
MDN status
Baseline Widely available (Mar 2022)
Snapshot
🔍 At a Glance
Four facts to remember about Document securitypolicyviolation.
Event type
SecurityPolicyViolationEvent
CSP details included
Means
CSP broken
Resource blocked / reported
Listen on
document
Or window (top level)
Baseline
yes
Widely available
Hands-On
Examples Gallery
Examples follow MDN Document: securitypolicyviolation event. Try-it labs use a page CSP meta tag that blocks images from other hosts, then load a blocked <img> so you can inspect the event. Some sandboxes may limit CSP—feature-detect and fall back to a synthetic event when needed.
📚 Getting Started
Listen and log the main SecurityPolicyViolationEvent properties.
Example 1 — Log Violation Details (MDN style)
Print blockedURI, violatedDirective, and originalPolicy.
disposition tells you whether the UA enforced the policy or only reported it. effectiveDirective is the modern name; violatedDirective is a historical alias.
Example 4 — Trigger with a Blocked Image
Set img-src 'none' via meta CSP, then insert a remote image.
The meta tag defines the policy; the image request violates img-src; the Document event reports the details. Keep script-src 'unsafe-inline' in labs so your demo script can still run.
Example 5 — Synthetic Event (Demo Fallback)
Dispatch a SecurityPolicyViolationEvent when a real CSP block is unavailable.
Useful for teaching the listener shape when the try-it iframe cannot apply a real CSP. In production, prefer real violations from your server CSP headers.
Applications
🚀 Common Use Cases
Logging CSP failures to your monitoring or analytics pipeline.
Debugging why a script, image, font, or frame was blocked.
Showing a gentle “resource blocked by security policy” notice in admin UIs.
Comparing enforce vs report-only rollouts via disposition.
Teaching beginners how CSP connects HTTP policy to JavaScript events.
Under the Hood
🔧 How It Works
1
CSP is active
Policy arrives via HTTP header or <meta http-equiv="Content-Security-Policy">.
policy
2
Something breaks a rule
A script, image, style, or other resource violates a directive such as img-src.
violation
3
Document event fires
securitypolicyviolation runs on the document (and can bubble to window).
event
4
✓
You inspect the details
Read blockedURI, directives, disposition, and related fields.
Important
📝 Notes
Baseline Widely available (since March 2022)—no Deprecated / Experimental / Non-standard banner.
Bubbles to Window and is composed—still prefer top-level listeners.
Blocked elements often target document directly (MDN), not the element itself.
The event observes violations; fixing them means adjusting your CSP policy.
Document securitypolicyviolation is marked Baseline Widely available on MDN (since March 2022). Logos use the shared browser-image-sprite.png sprite from this project. Pair it with real CSP headers in production.
✓ Baseline · Widely available
Document securitypolicyviolation
Fires when a Content Security Policy is violated. Inspect SecurityPolicyViolationEvent fields such as blockedURI and violatedDirective.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium Edge
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerNot a modern target for this Baseline feature
Not supported
securitypolicyviolationExcellent
Bottom line: Listen on document or window, read SecurityPolicyViolationEvent details, and tighten CSP based on real violations—not only on the event.
Wrap Up
Conclusion
Document securitypolicyviolation connects CSP enforcement to JavaScript. When a resource breaks the policy, you get a SecurityPolicyViolationEvent with the blocked URI and the directive that failed—perfect for logging and learning.
Assume the event fires on the blocked <img> itself
Treat the listener as a substitute for a correct CSP
Ignore report-only vs enforce differences
Ship overly open CSP just to silence the event
Call this Experimental—it is Baseline Widely available
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about securitypolicyviolation
CSP broke a rule — read SecurityPolicyViolationEvent for the details.
5
Core concepts
📄01
CSP violated
Policy blocked a resource
Event
🔍02
Rich event type
SecurityPolicyViolationEvent
API
🔒03
Key fields
blockedURI + directive
Debug
🎯04
Top-level listen
document or window
Pattern
✅05
Baseline ready
Widely available
Status
Next up: selectionchange — when the document Selection changes.
Questions
❓ Frequently Asked Questions
What is the Document securitypolicyviolation event?
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.
Is securitypolicyviolation deprecated or experimental?
No. MDN marks Document securitypolicyviolation as Baseline Widely available (since March 2022). It is not Deprecated, Experimental, or Non-standard.
Does the event bubble?
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.
What event type is it?
A SecurityPolicyViolationEvent, which inherits from Event. Useful properties include blockedURI, violatedDirective (alias of effectiveDirective), originalPolicy, disposition, and sample.
Where should I attach the listener?
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>.
What is disposition?
It indicates whether the user agent is configured to enforce or just report the policy violation ("enforce" or "report"), such as with Content-Security-Policy-Report-Only.
Did you know?
CSP can run in report-only mode. Violations still fire securitypolicyviolation, but disposition is "report" and the browser does not necessarily block the resource the same way as enforce mode.