Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
DOMTokenList supports() example
var DOMTokenListSupports = function(tokenList, token) {
if (!tokenList || !tokenList.supports) {
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

This comment has been minimized.

Copy link

@superpoincare superpoincare commented Aug 9, 2016

Is this guaranteed to work? Works on Chrome but isn't it possible some browser may implement preload without having tokenList or 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