SecurityPolicyViolationEvent - 表示违反内容安全策略时发送的事件对象
这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。
SecurityPolicyViolationEvent 接口继承自 Event,表示在文档或 worker 上,违反其内容安全策略时发送的事件对象。
构造函数
SecurityPolicyViolationEvent()
创建一个新的 SecurityPolicyViolationEvent 对象实例。
属性
SecurityPolicyViolationEvent.blockedURI 只读
一个 USVString,代表因违反策略而被阻止的资源的 URI。
SecurityPolicyViolationEvent.columnNumber 只读
发生违规的文档或工作程序中的列号。
SecurityPolicyViolationEvent.disposition 只读
指示违反的策略如何配置为由用户代理处理。它的值是 "enforce" 或 "report"。
SecurityPolicyViolationEvent.documentURI 只读
一个 USVString,表示发现违规的文档或工作程序的 URI。
SecurityPolicyViolationEvent.effectiveDirective 只读
一个 DOMString,表示发现违规情况的指令。
SecurityPolicyViolationEvent.lineNumber 只读
表示发生违规的文档或工作程序中的行号。
SecurityPolicyViolationEvent.originalPolicy 只读
一个 DOMString,包含其执行发现违规的策略。
SecurityPolicyViolationEvent.referrer 只读
一个 USVString,表示违反其策略的资源的 referrer。它将是一个 URL 或 null。
SecurityPolicyViolationEvent.sample 只读
一个 DOMString,表示引起违规的资源样本,通常是前 40 个字符。只有当资源是内联脚本、事件处理程序或样式时,才会填充此内容 — 导致冲突的外部资源不会生成样本。
SecurityPolicyViolationEvent.sourceFile 只读
一个 USVString,表示在其中发现违规的文档或工作程序的 URI。
SecurityPolicyViolationEvent.statusCode 只读
一个数字,表示发生违规的文档或工作程序的 HTTP 状态代码。
SecurityPolicyViolationEvent.violatedDirective 只读
一个 DOMString,表示执行发现了违规情况的指令。
实例
document.addEventListener("securitypolicyviolation", (e) => {
console.log(e.blockedURI);
console.log(e.violatedDirective);
console.log(e.originalPolicy);
});
规范
| 规范 | 状态 | 备注 |
|---|---|---|
| Content Security Policy Level 2 SecurityPolicyViolationEvent 的定义 |
推荐 | 初始定义。 |
| Content Security Policy Level 3 SecurityPolicyViolationEvent 的定义 |
工作草案 | - |
桌面浏览器兼容性
| 特性 | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|---|
| 基础支持 | 支持 | 支持 | 63 59 | 不支持 | 支持 | 支持 |
SecurityPolicyViolationEvent() 构造函数 | 支持 | 支持 | 63 59 | 不支持 | 支持 | 支持 |
blockedURI | 支持 | 15 | 63 59 | 不支持 | 支持 | 支持 |
columnNumber | 支持 | 15 | 63 59 | 不支持 | 支持 | 支持 |
disposition | 支持 | 支持 | 63 59 | 不支持 | 支持 | 支持 |
documentURI | 支持 | 15 | 63 59 | 不支持 | 支持 | 支持 |
effectiveDirective | 支持 | 15 | 63 59 | 不支持 | 支持 | 支持 |
lineNumber | 支持 | 15 | 63 59 | 不支持 | 支持 | 支持 |
originalPolicy | 支持 | 15 | 63 59 | 不支持 | 支持 | 支持 |
referrer | 支持 | 15 | 63 59 | 不支持 | 支持 | 支持 |
sample | 59 | 支持 | 63 59 | 不支持 | 46 | 支持 |
sourceFile | 支持 | 15 | 63 59 | 不支持 | 支持 | 支持 |
statusCode | 支持 | 15 | 63 59 | 不支持 | 支持 | 支持 |
violatedDirective | 支持 | 15 | 63 59 | 不支持 | 支持 | 支持 |
| 在 Worker 中可用 | 56 | 支持 | 63 59 | 不支持 | 43 | 支持 |
移动浏览器兼容性
| 特性 | Android | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
|---|---|---|---|---|---|---|---|
| 基础支持 | 支持 | 支持 | 未知 | 63 59 | 未知 | 支持 | 支持 |
SecurityPolicyViolationEvent() 构造函数 | 支持 | 支持 | 未知 | 63 59 | 未知 | 支持 | 支持 |
blockedURI | 支持 | 支持 | 未知 | 63 59 | 未知 | 支持 | 支持 |
columnNumber | 支持 | 支持 | 未知 | 63 59 | 未知 | 支持 | 支持 |
disposition | 支持 | 支持 | 未知 | 63 59 | 未知 | 支持 | 支持 |
documentURI | 支持 | 支持 | 未知 | 63 59 | 未知 | 支持 | 支持 |
effectiveDirective | 支持 | 支持 | 未知 | 63 59 | 未知 | 支持 | 支持 |
lineNumber | 支持 | 支持 | 未知 | 63 59 | 未知 | 支持 | 支持 |
originalPolicy | 支持 | 支持 | 未知 | 63 59 | 未知 | 支持 | 支持 |
referrer | 支持 | 支持 | 未知 | 63 59 | 未知 | 支持 | 支持 |
sample | 59 | 59 | 未知 | 63 59 | 未知 | 43 | 支持 |
sourceFile | 支持 | 支持 | 未知 | 63 59 | 未知 | 支持 | 支持 |
statusCode | 支持 | 支持 | 未知 | 63 59 | 未知 | 支持 | 支持 |
violatedDirective | 支持 | 支持 | 未知 | 63 59 | 未知 | 支持 | 支持 |
| 在 Worker 中可用 | 56 | 56 | 未知 | 63 59 | 未知 | 43 | 支持 |