Created
May 2, 2019 05:59
-
-
Save xavianaxw/f433b54fdce791158ffd9ed73da5894c to your computer and use it in GitHub Desktop.
Script to defer CSS if non-critical to improve PageSpeed
This file contains 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
// 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