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 |
瀏覽器相容性
載入中…