Deferring Javascript loads

Just discovered this way to speed up site loading times, by deferring the javascript:

function downloadJSAtOnload() {
var element = document.createElement(“script”);
element.src = “defer.js”;
if (window.addEventListener)
window.addEventListener(“load”, downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent(“onload”, downloadJSAtOnload);
else window.onload = downloadJSAtOnload;

The ‘defer.js’ is the name of the (non-crucial) file which you want to defer from loading at the start.

How I would use this is to:

  1. setup the basic site with all the essential theme loading elements.
  2. make note of the javascript libraries called.
  3. continue building the site with all additional plugins.
  4. check the javascript calls again, and defer all new ones.

I’ve got a few of new sites to do soon, so I’ll be trying this out and will report back of the progress and how it improves things.

I found this here, btw.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s