Events

onsecuritypolicyviolation

A Content Security Policy violation is reported, including violations of report-only policies. Listen on Document for page-level CSP reports; the event may originate on an element. Do not rely on an arbitrary inline handler under CSP. Event interface: SecurityPolicyViolationEvent. For the described native event: bubbles: yes; cancelable: no. Inspect effectiveDirective and disposition; an event is not a complete security audit and blockedURI may be sensitive. Example: Use a standalone page with an appropriate response CSP and a blocked test resource; the snippet alone does not create a violation. The event listener is registered with addEventListener(), which avoids inline script handlers and supports multiple listeners. Inline handlers require a CSP that permits them; do not insert untrusted code into handler attributes.

Formal Syntax & Structure

html syntaxhtml
target.addEventListener("securitypolicyviolation", event => { /* handle event */ });

Parameters & Attributes

NameTypeDescription
eventSecurityPolicyViolationEventEvent object delivered to the addEventListener callback; event.type is "securitypolicyviolation". Inspect target, currentTarget, bubbles and cancelable as appropriate.

Working Example

Example Snippethtml
<p>CSP violation monitor</p><p role="status" id="log">Waiting for securitypolicyviolation</p><script>  const target = document;  const log = document.getElementById("log");  target.addEventListener("securitypolicyviolation", event => {    log.textContent = String(event.effectiveDirective);  });</script>
HTML Reference