HTMLScriptElement: supports() 静态方法
HTMLScriptElement 接口的 supports() 静态方法提供了一种简单而一致的方法来检测用户代理支持哪些类型的脚本。
该方法预计会为经典脚本和模块脚本返回 true,这两种脚本都得到了大多数现代浏览器的支持。
语法
js
HTMLScriptElement.supports(type)
参数
type-
一个指示要检查其支持的脚本类型的字符串字面量。支持的值区分大小写,包括:
"classic"-
测试是否支持经典脚本。“经典”脚本是在模块脚本之前出现的普通/传统的 JavaScript 文件。
"module"-
测试是否支持模块脚本。
"importmap"-
测试是否支持import maps。
"speculationrules"-
测试是否支持并启用了speculation rules。
任何其他值都会导致该方法返回
false。
返回值
如果指示的脚本类型受支持,则返回 true,否则返回 false。
示例
下面的代码显示了如何检查 HTMLScriptElement.supports() 是否已定义,如果是,则使用它来测试是否支持特定类型的脚本。
js
const log = document.getElementById("log");
function checkSupport(type) {
const result = HTMLScriptElement.supports(type) ? "true" : "false";
log.textContent += `HTMLScriptElement.supports('${type}') is ${result}\n`;
}
if (typeof HTMLScriptElement.supports === "undefined") {
log.textContent = "HTMLScriptElement.supports() method is not supported";
} else {
// Check if various script types are supported
checkSupport("module");
checkSupport("classic");
checkSupport("importmap");
checkSupport("speculationrules");
// Any other value will cause the method to return false
checkSupport("anything else");
}
规范
| 规范 |
|---|
| HTML # dom-script-supports-dev |
浏览器兼容性
加载中…