Skip to content

Instantly share code, notes, and snippets.

@xavianaxw
Created May 2, 2019 05:59
Show Gist options
  • Save xavianaxw/f433b54fdce791158ffd9ed73da5894c to your computer and use it in GitHub Desktop.
Save xavianaxw/f433b54fdce791158ffd9ed73da5894c to your computer and use it in GitHub Desktop.
Script to defer CSS if non-critical to improve PageSpeed
// List of css file(s) to be deferred
var deferred_css = [
"example-1.css",
"example-2.css",
];
function insertCss(value, index) {
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = value;
link.type = 'text/css';
var existingLink = document.getElementsByTagName("link")[0];
existingLink.parentNode.insertBefore(link, existingLink);
}
// loops through list and insert stylesheet to DOM
deferred_css.forEach(insertCss);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment