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