CKTIS / HTML
onsecuritypolicyviolation — handling CSP violation events
The onsecuritypolicyviolation attribute specifies JavaScript that handles the securitypolicyviolation event associated with a Content Security Policy (CSP) violation. It does not establish a security policy, and CSP can block its inline code.
Version: WHATWG HTML Living Standard; index snapshot date recorded separately
Syntax
onsecuritypolicyviolation="JavaScript statements"onsecuritypolicyviolation is an event handler content attribute for securitypolicyviolation, an event associated with a Content Security Policy (CSP) violation. It can be written on any HTML element, but adding the attribute does not guarantee that the element will receive such an event. The attribute neither defines nor enforces CSP.
The attribute value contains JavaScript statements—a function body rather than a function declaration. Inside the handler, event is the delivered event and this is the element handling it. Distinguish the attribute name onsecuritypolicyviolation from the event name securitypolicyviolation, which has no on prefix.
The corresponding JavaScript property, for example element.onsecuritypolicyviolation, accepts a function or null, rather than code text. CSP can block inline event handler attributes; code intended to observe violations is subject to those restrictions too. Do not weaken the policy merely to run this attribute.
The example dispatches a synthetic Event without causing a real CSP violation. It checks only the connection between the attribute and the event name and displays securitypolicyviolation. This object does not contain the details of a real violation.
Examples
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Handler test</title>
<div id="target" onsecuritypolicyviolation="this.textContent = event.type;">Waiting</div>
<script>
const target = document.getElementById('target');
target.dispatchEvent(new Event('securitypolicyviolation'));
</script>
</html>Result
securitypolicyviolation
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.