Last active
January 10, 2020 17:36
-
-
Save yoavweiss/8490dabb3e0aa112fc74 to your computer and use it in GitHub Desktop.
DOMTokenList supports() example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var DOMTokenListSupports = function(tokenList, token) { | |
if (!tokenList || !tokenList.supports) { | |
return; | |
} | |
try { | |
return tokenList.supports(token); | |
} catch (e) { | |
if (e instanceof TypeError) { | |
console.log("The DOMTokenList doesn't have a supported tokens list"); | |
} else { | |
console.error("That shouldn't have happened"); | |
} | |
} | |
}; | |
var linkSupportsPreload = DOMTokenListSupports(document.createElement("link").relList, "preload"); // true | |
var classListSupportsFoobar = DOMTokenListSupports(document.createElement("div").classList, "foobar"); // undefined + console.log | |
var linkSupportsFoobar = DOMTokenListSupports(document.createElement("link").relList, "foobar"); // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this guaranteed to work? Works on Chrome but isn't it possible some browser may implement preload without having
tokenList
ortokenList.support
. If that's the case, then a preload resource may get inserted twice. Once because ofonload="..."
and once due to JS which doesn't detect preload and inserts the link.