Deep dive into the murky waters of script loading

In this article I’m going to teach you how to load some JavaScript in the browser and execute it.

.. Ok ok. If you want to load scripts in a way that doesn’t block rendering, doesn’t involve repetition, and has excellent browser support, here’s what I propose:

<script src="//other-domain.com/1.js"></script>
<script src="2.js"></script>

That. At the end of the body element

How to defer Javascripts.

Add the following small code (created by Google Page Speed experts) to the bottom of your page just above the closing </body> tag:

<script type="text/javascript">
function downloadJSAtOnload() {
var element = document.createElement("script");
element.src = "yourdeferscripts.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>

Replace yourdeferscripts.js with the .js file you created in the step before this one. By using the above scripts on your page you tell the browser to load the yourdeferscripts.js file after your page has finished loading.