Favorites

HTML onsecuritypolicyviolation Event Attribute

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
onsecuritypolicyviolation
HTML Event AttributeThis event attribute is supported by all modern browsers.
Updated 22nd November 2026

Description

The onsecuritypolicyviolation event attribute runs a script when the page breaks its Content Security Policy (CSP), for example by loading a script, image or style from a source that the policy does not allow. The event fires on the element that caused the violation and bubbles up to the document and window.

The event is a SecurityPolicyViolationEvent with properties such as blockedURI, violatedDirective, effectiveDirective, originalPolicy, sourceFile and lineNumber. It is useful for testing a new policy with Content-Security-Policy-Report-Only and for logging violations, in addition to the server-side report-to directive.

Syntax

<element onsecuritypolicyviolation="script">

Values

  • scriptThe name of the script to use when the event has been triggered.

Example

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="img-src 'self'">
<title>onsecuritypolicyviolation event</title>
</head>
<body>
<p>This page only allows images from its own origin.</p>
<img src="https://example.com/image.png" alt="Blocked image">
<ul id="log"></ul>
<script>
function log(text) {
var li = document.createElement("li");
li.textContent = text;
document.getElementById("log").appendChild(li);
}
document.addEventListener("securitypolicyviolation", function (event) {
log("Blocked " + event.blockedURI + " by " + event.violatedDirective);
});
</script>
</body>
</html>
Open in editor

Browser Support

The following information will show you the current browser support for the HTML onsecuritypolicyviolation event attribute. Hover over a browser icon to see the version that first introduced support for this HTML event attribute.

This event attribute is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee