Created
December 15, 2013 14:55
-
-
Save mundry/7973898 to your computer and use it in GitHub Desktop.
Google's snippet for lazy loading JavaScript.
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
<html> | |
<head> | |
</head> | |
<body> | |
<!-- Content --> | |
<a href="https://developers.google.com/speed/docs/best-practices/payload#DeferLoadingJS">Defer loading of JavaScript | Google Developers</a> | |
<script type="text/javascript"> | |
// Add a script element as a child of the body. | |
function downloadJSAtOnload() { | |
var element = document.createElement("script"); | |
element.src = "js.js"; | |
document.body.appendChild(element); | |
} | |
// Check for browser support of event handling capability. | |
if (window.addEventListener) { | |
window.addEventListener("load", downloadJSAtOnload, false); | |
} else if (window.attachEvent) { | |
window.attachEvent("onload", downloadJSAtOnload); | |
} else { | |
window.onload = downloadJSAtOnload; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment