大网数据广告活动
Web 标准与前端开发

HTMLScriptElement: attributionSrc property

· 52CE 我爱测

The **`attributionSrc`** property of the HTMLScriptElement interface gets and sets the `attributionsrc` attribute on a script element programmatically, reflecting the value of that attribute. `attributionsrc` specifies that you want the browser to send an Attribution-Reporting-El

Attribution Reporting API

The attributionSrc property of the HTMLScriptElement interface gets and sets the attributionsrc attribute on a script element programmatically, reflecting the value of that attribute. attributionsrc specifies that you want the browser to send an Attribution-Reporting-Eligible header along with the script resource request.

On the server-side this is used to trigger sending an Attribution-Reporting-Register-Source or Attribution-Reporting-Register-Trigger header in the response, to register a JavaScript-based attribution source or attribution trigger, respectively. Which response header should be sent back depends on the value of the Attribution-Reporting-Eligible header that triggered the registration.

[!NOTE] Alternatively, JavaScript-based attribution sources or triggers can be registered by sending a Window/fetch request containing the attributionReporting option (either set directly on the fetch() call or on a Request object passed into the fetch() call), or by sending an XMLHttpRequest with XMLHttpRequest.setAttributionReporting invoked on the request object.

See the Attribution Reporting API for more details.

Value

A string. There are two versions of this property that you can get and set:

  • Empty string, i.e., scriptElem.attributionSrc="". This specifies that you want the Attribution-Reporting-Eligible header sent to the same server as the src attribute points to. This is fine when you are handling the attribution source or trigger registration on the same server. When registering an attribution trigger this property is optional, and an empty string value will be used if it is omitted.

  • Value containing one or more URLs, for example:

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

    This is useful in cases where the requested resource is not on a server you control, or you just want to handle registering the attribution source on a different server. In this case, you can specify one or more URLs as the value of attributionSrc. When the resource request occurs the Attribution-Reporting-Eligible header will be sent to the URL(s) specified in attributionSrc in addition to the resource origin. These URLs can then respond with an Attribution-Reporting-Register-Source or Attribution-Reporting-Register-Trigger header as appropriate to complete registration.

    [!NOTE] Specifying multiple URLs means that multiple attribution sources can be registered on the same feature. You might for example have different campaigns that you are trying to measure the success of, which involve generating different reports on different data.

Examples

Setting an empty attributionSrc

<script src="advertising-script.js"></script>
const scriptElem = document.querySelector("script");
scriptElem.attributionSrc = "";

Setting an attributionSrc containing URLs

<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}`;

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。