Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Preload feature detection
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");
if (!linkSupportsPreload) {
// Dynamically load the things that relied on preload.

This comment has been minimized.

Copy link

@triblondon triblondon commented Jun 30, 2016

If you wanted to reduce the code bloat for production, would this do?

var supportsPreload = (function() { try { document.createElement("link").relList.supports('preload'); return 1 } catch(e) { return 0; }}());

This comment has been minimized.

Copy link

@lsuchanek lsuchanek commented Mar 7, 2018

@triblondon I think, better solution is: var supportsPreload = (function() { try { return document.createElement("link").relList.supports('preload'); } catch(e) { return false; }}());

Your code return false positive for FF 58.

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