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

瀏覽器相容性

另見