HTMLScriptElement: attributionSrc 属性
HTMLScriptElement 接口的 attributionSrc 属性可以通过编程方式获取和设置 <script> 元素上的 attributionsrc 属性,反映该属性的值。attributionsrc 指定您希望浏览器在请求脚本资源时附加一个 Attribution-Reporting-Eligible 头部。
在服务器端,这用于在响应中触发发送 Attribution-Reporting-Register-Source 或 Attribution-Reporting-Register-Trigger 头部,以分别注册一个基于 JavaScript 的 归因源 或 归因触发器。应该发送哪个响应头部取决于触发注册的 Attribution-Reporting-Eligible 头部的值。
注意: 或者,可以通过发送包含 attributionReporting 选项的 fetch() 请求(直接在 fetch() 调用中设置,或在传递给 fetch() 调用的 Request 对象上设置),或者通过发送调用了 setAttributionReporting() 的 XMLHttpRequest 来注册基于 JavaScript 的归因源或触发器。
有关更多详细信息,请参阅 归因报告 API。
值
一个字符串。此属性有两个版本可供获取和设置。
-
空字符串,即
scriptElem.attributionSrc=""。这指定您希望将Attribution-Reporting-Eligible头部发送到src属性指向的同一台服务器。当您在同一台服务器上处理归因源或触发器注册时,这是可以的。在注册归因触发器时,此属性是可选的,如果省略,则会使用空字符串值。 -
包含一个或多个 URL 的值,例如
jsscriptElem.attributionSrc = "https://a.example/register-source https://b.example/register-source";这在请求的资源不在您控制的服务器上,或者您只想在不同的服务器上处理归因源注册的情况下很有用。在这种情况下,您可以指定一个或多个 URL 作为
attributionSrc的值。当发生资源请求时,除了资源来源外,Attribution-Reporting-Eligible头部还将发送到attributionSrc中指定的 URL。然后,这些 URL 可以根据需要使用Attribution-Reporting-Register-Source或Attribution-Reporting-Register-Trigger头部进行响应,以完成注册。注意:指定多个 URL 意味着可以在同一功能上注册多个归因源。例如,您可能正在尝试衡量不同广告系列的成功,这涉及生成关于不同数据的不同报告。
示例
设置空的 attributionSrc
<script src="advertising-script.js"></script>
const scriptElem = document.querySelector("script");
scriptElem.attributionSrc = "";
设置包含 URL 的 attributionSrc
<script src="advertising-script.js"></script>
// encode the URLs in case they contain special characters
// such as '=' that would be improperly parsed.
const encodedUrlA = encodeURIComponent("https://a.example/register-source");
const encodedUrlB = encodeURIComponent("https://b.example/register-source");
const scriptElem = document.querySelector("script");
scriptElem.attributionSrc = `${encodedUrlA} ${encodedUrlB}`;
规范
| 规范 |
|---|
| 归因报告 # dom-htmlattributionsrcelementutils-attributionsrc |
浏览器兼容性
加载中…