JavaScript Element securitypolicyviolation Event

Beginner
5 min read
Updated: Nov 2026
3 examples
Event

Definition

In JavaScript, the securitypolicyviolation event fires when a Content Security Policy (CSP) is violated (MDN). The event type is a SecurityPolicyViolationEvent.

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)
CallWhat it does
addEventListener("securitypolicyviolation", fn)Primary signature.

Built into the browser. Listen with el.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 ValueDescription
*Fires as an Element event (listener receives the Event object).

JavaScript Examples

Example 1 — Listen on window

MDN’s top-level pattern using addEventListener.

JavaScript
window.addEventListener("securitypolicyviolation", (e) => {
  console.log("CSP violation");
  console.log(e.type);
});
Try It Yourself

How It Works

  1. Choose a top-level target. window or document matches MDN.
  2. Use the event name. Exact string: "securitypolicyviolation".
  3. Wait for a real block. The handler runs only when CSP rejects something.
  4. Confirm the type. e.type is the event name string.

Example 2 — Log Key Fields (MDN)

Read blockedURI, violatedDirective, and originalPolicy.

JavaScript
document.addEventListener("securitypolicyviolation", (e) => {
  console.log(e.blockedURI);
  console.log(e.violatedDirective);
  console.log(e.originalPolicy);
});
Try It Yourself

How It Works

  1. blockedURI. Which resource was rejected.
  2. violatedDirective. Which CSP directive failed (same idea as effectiveDirective).
  3. originalPolicy. The policy text that caused the block.
  4. Use for reports. Log locally or send to your monitoring endpoint.

Example 3 — Sample Dispatch (Lab Demo)

When a real CSP block is unavailable, dispatch a sample event to practice reading fields.

JavaScript
document.addEventListener("securitypolicyviolation", (e) => {
  console.log(e.blockedURI);
  console.log(e.violatedDirective);
});

document.dispatchEvent(
  new SecurityPolicyViolationEvent("securitypolicyviolation", {
    blockedURI: "https://cdn.example/x.png",
    violatedDirective: "img-src",
    effectiveDirective: "img-src",
    originalPolicy: "img-src 'none'",
    disposition: "enforce",
    documentURI: document.documentURI,
    statusCode: 200
  })
);
Try It Yourself

How It Works

  1. Attach first. The listener must exist before you dispatch.
  2. Build the event. new SecurityPolicyViolationEvent(…) fills demo fields.
  3. Dispatch on document. Same target MDN recommends for real CSP events.
  4. Label it clearly. This teaches the API — a real violation still needs a CSP policy.

Browser Support

securitypolicyviolation is Baseline Widely available (MDN: since October 2018). Safe to use in modern evergreen browsers.

✓ Baseline · Widely available

securitypolicyviolation

Fires when a Content Security Policy is violated — listen on window or document.

99% Widely available
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
securitypolicyviolation Excellent

Bottom line: Listen on window or document, log SecurityPolicyViolationEvent fields, and pair with a real CSP policy for production monitoring.

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 (MDN).
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 a top-level object such as Window or Document. In practice the event often targets document (for example a blocked image), rather than 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 enforces the policy or only reports the violation (enforce vs report).

Next: touchcancel

Learn when a touch sequence is interrupted by the browser.

touchcancel tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful