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

浏览器兼容性