TrustedTypePolicyFactory: getAttributeType() 方法

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

注意:此功能在 Web Workers 中可用。

TrustedTypePolicyFactory 介面的 getAttributeType() 方法允許 Web 開發者檢查某個元素是否需要 Trusted Type,如果需要,則檢查使用哪種 Trusted Type。

語法

js
getAttributeType(tagName, attribute)
getAttributeType(tagName, attribute, elementNS)
getAttributeType(tagName, attribute, elementNS, attrNS)

引數

tagName

包含 HTML 標籤名稱的字串。

attribute

包含屬性名稱的字串。

elementNS 可選

包含名稱空間的字串,如果為空,則預設為 HTML 名稱空間。

attrNS 可選

包含名稱空間的字串,如果為空,則預設為 null。

返回值

返回以下其中一個字串:

  • "TrustedHTML"
  • "TrustedScript"
  • "TrustedScriptURL"

或者 null。

示例

在這個示例中,將 <script> 元素和 src 屬性傳遞給 getAttributeType() 會返回 "TrustedScriptURL"。

js
console.log(trustedTypes.getAttributeType("script", "src")); // "TrustedScriptURL"

規範

規範
Trusted Types
# dom-trustedtypepolicyfactory-getattributetype

瀏覽器相容性