Created
August 21, 2015 20:47
-
-
Save gvinson/f692897708807be32dae to your computer and use it in GitHub Desktop.
Load Scripts and Stylesheets without Blocking Render
This file contains hidden or 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
/** | |
* The following must be inserted before the | |
* closing head tag. | |
*/ | |
var scripts = [ '/js/script.js', '/js/script2.js' ]; | |
var stylesheets = [ '/css/styles.css', '/css/styles.css' ]; | |
// Load stylesheets | |
for (var i=0; i < stylesheets.length; i++) { | |
var link = document.createElement('link'); | |
var before = window.document.getElementsByTagName( 'script ')[0]; | |
link.href = stylesheets[i]; | |
link.type = 'text/css'; | |
link.rel = 'stylesheet'; | |
// Set the media type to an incorrect type to ensure | |
// the asset will be fetched without blocking render | |
link.meida = 'new-sheet'; | |
document.head.insertBefore(link, before); | |
// Now set the correct media type | |
link.media = 'screen,print'; | |
} | |
// Load scripts | |
for (var i=0; i < scripts.length; i++) { | |
var script = document.createElement('script'); | |
script.src = scripts[i]; | |
document.head.appendChild(script); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment