Created
February 22, 2017 15:40
-
-
Save carmolim/50d5bd1eca21d091434e376950c06316 to your computer and use it in GitHub Desktop.
Defer loading javascript
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
| // ref: https://varvy.com/pagespeed/defer-loading-javascript.html | |
| function downloadJSAtOnload() { | |
| const scripts = ['defer.js', 'defer2.js', 'defer3.js']; | |
| scripts.forEach(function(script){ | |
| const element = document.createElement("script"); | |
| element.src = script; | |
| document.body.appendChild(element); | |
| }) | |
| } | |
| downloadJSAtOnload(); | |
| if (window.addEventListener){ | |
| window.addEventListener("load", downloadJSAtOnload, false); | |
| } | |
| else if (window.attachEvent){ | |
| window.attachEvent("onload", downloadJSAtOnload); | |
| } | |
| else { | |
| window.onload = downloadJSAtOnload; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment