Skip to content

Instantly share code, notes, and snippets.

@botaor
Created January 2, 2014 14:20
Show Gist options
  • Save botaor/8219782 to your computer and use it in GitHub Desktop.
Save botaor/8219782 to your computer and use it in GitHub Desktop.
Defer loading of Javascript files untill the page is already loaded
<script type="text/javascript">
function downloadJSAtOnload() {
var element = document.createElement("script");
element.src = "defer.js";
document.body.appendChild(element);
}
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else
window.onload = downloadJSAtOnload;
</script>
/*
This was taken from:
http://www.feedthebot.com/pagespeed/defer-loading-javascript.html
Also have a look at the following URLs
http://ablogaboutcode.com/2011/06/14/how-javascript-loading-works-domcontentloaded-and-onload/
http://www.feedthebot.com/pagespeed/critical-render-path.html
http://vimeo.com/71362583
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment