HTMLAnchorElement: attributionSrc 属性

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

安全上下文: 此功能仅在安全上下文(HTTPS)中可用,且支持此功能的浏览器数量有限。

实验性: 这是一项实验性技术
在生产中使用此技术之前,请仔细检查浏览器兼容性表格

HTMLAnchorElement 接口的 attributionSrc 属性以编程方式获取和设置 <a> 元素上的 attributionsrc 属性,反映该属性的值。attributionsrc 指定您希望浏览器发送 Attribution-Reporting-Eligible 标头。在服务器端,这用于触发在响应中发送 Attribution-Reporting-Register-Source 标头,以注册一个基于导航的归因来源

当浏览器接收到导航响应时,它会存储与基于导航的归因来源相关联的来源数据(如 Attribution-Reporting-Register-Source 响应标头中所提供)。

有关更多详细信息,请参阅 归因报告 API

注意: <a> 元素不能用作归因触发器,只能用作来源。

一个字符串。此属性有两个版本可供获取和设置。

  • 空字符串,即 aElem.attributionSrc=""。这指定您希望将 Attribution-Reporting-Eligible 标头发送到 href 属性指向的同一服务器。当您在同一服务器上处理归因来源注册时,这是可以的。

  • 包含一个或多个 URL 的值,例如

    js
    aElem.attributionSrc =
      "https://a.example/register-source https://b.example/register-source";
    

    这在所请求的资源不在您控制的服务器上,或者您只想在不同的服务器上处理归因来源注册的情况下很有用。在这种情况下,您可以将一个或多个 URL 指定为 attributionSrc 的值。当发生资源请求时,除了资源来源之外,Attribution-Reporting-Eligible 标头还将发送到 attributionSrc 中指定的 URL。这些 URL 然后可以响应 Attribution-Reporting-Register-Source 来注册来源。

    注意:指定多个 URL 意味着可以在同一功能上注册多个归因源。例如,您可能正在尝试衡量不同广告系列的成功,这涉及生成关于不同数据的不同报告。

示例

设置空的 attributionSrc

html
<a href="https://shop.example"> Click to visit our shop </a>
js
const aElem = document.querySelector("a");
aElem.attributionSrc = "";

设置包含 URL 的 attributionSrc

html
<a href="https://ourshop.example"> Click to visit our shop </a>
js
// 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 aElem = document.querySelector("a");
aElem.attributionSrc = `${encodedUrlA} ${encodedUrlB}`;

规范

规范
归因报告
# dom-htmlattributionsrcelementutils-attributionsrc

浏览器兼容性

另见