HTMLImageElement:attributionSrc 属性
HTMLImageElement 接口的 attributionSrc 属性用于以编程方式获取和设置 <img> 元素上的 attributionsrc 属性,并反映该属性的值。attributionsrc 指定您希望浏览器在图像请求中附带一个 Attribution-Reporting-Eligible 标头。
在服务器端,这用于触发在响应中发送 Attribution-Reporting-Register-Source 或 Attribution-Reporting-Register-Trigger 标头,以分别注册基于图像的 归因源或归因触发器。应该返回哪个响应标头取决于触发注册的 Attribution-Reporting-Eligible 标头的值。
源/触发器在浏览器收到包含图像文件的响应后注册。
注意: 请记住,用户可能根本无法感知到图像——它可能只是一个 1x1 的透明跟踪像素,仅用于归因报告。
有关更多详细信息,请参阅 归因报告 API。
值
一个字符串。此属性有两个版本可供获取和设置。
-
空字符串,即
imgElem.attributionSrc=""。这指定您希望Attribution-Reporting-Eligible标头发送到src属性指向的同一个服务器。当您在同一服务器上处理归因源或触发器注册时,这没有问题。注册归因触发器时,此属性是可选的,如果省略,将使用空字符串值。 -
包含一个或多个 URL 的值,例如
jsimgElem.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
<img src="advertising-image.png" />
const imgElem = document.querySelector("img");
imgElem.attributionSrc = "";
设置包含 URL 的 attributionSrc
<img src="advertising-image.png" />
// 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 imgElem = document.querySelector("img");
imgElem.attributionSrc = `${encodedUrlA} ${encodedUrlB}`;
规范
| 规范 |
|---|
| 归因报告 # dom-htmlattributionsrcelementutils-attributionsrc |
浏览器兼容性
加载中…