Skip to content

Instantly share code, notes, and snippets.

@yoavweiss
Last active January 10, 2020 17:36
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yoavweiss/8490dabb3e0aa112fc74 to your computer and use it in GitHub Desktop.
Save yoavweiss/8490dabb3e0aa112fc74 to your computer and use it in GitHub Desktop.
DOMTokenList supports() example
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
@superpoincare
Copy link

Is this guaranteed to work? Works on Chrome but isn't it possible some browser may implement preload without having tokenList or tokenList.support. If that's the case, then a preload resource may get inserted twice. Once because of onload="..." and once due to JS which doesn't detect preload and inserts the link.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment