PHP Developer / Blog

May
31st, 2008

Keeping third party JavaScript from slowing down your site

Digg this article · Save to del.icio.us · Stumble it!

All too many times I go to a blog or website and half of the page loads. Then it sits there waiting while a JavaScript file from another site loads. Some JavaScript cannot be placed at the bottom of the DOM because they use document.write(). It’s unrealistic to expect everyone to abandon third party sites (I use a script from Photagious for the photos on the right). But is there anything we can do to make them at least appear to load faster?

What not to do
Here’s an example of what I’m talking about.

<div id="myDomElement">
<script src="http://thirdparty.com/path/to/file.js"></script>

What we’d like to do
Of course. The ideal scenario is that you can load your entire page and then once it’s completed start fetching third party JavaScript files. Turns out that this is extremely easy and there’s no excuse for sites to not be doing it.

How we do it
What you want to do is to attach handler to the window’s onload event. I’ll show an example using plain JavaScript but can be done using any of the popular JavaScript libraries.

window.onload = function(){
  var el = document.createElement('script');
  el.src = 'http://thirdparty.com/path/to/file.js';
  document.getElementById('myDomElement').appendChild(el);
}

It’s that simple! Now do it :).

5 Responses to “Keeping third party JavaScript from slowing down your site”

  1. Peter Michaux Says:

    You could use this technique even for your own JavaScript files. Some people do.

    There may be a trend to start using more JavaScript files from third-party servers.

    http://ajaxian.com/archives/announcing-ajax-libraries-api-speed-up-your-ajax-apps-with-googles-infrastructure

    I think the JavaScript files in mashups slow things down because there is database interaction to dynamically generate that JavaScript. The part that the JavaScript is from another server is probably not what makes the page painfully slow to finish loading. It may make it slower however as extra connections need to be made.

  2. jaisen Says:

    @Peter, the google hosted js libs are a great idea. Google’s infrastructure is as reliable and fast as you can expect from a third party.

    My main concern is that startups while providing great services don’t have infrastructure that’s as reliable as one might expect. So if you *must* include their scripts it should become a standard practice to minimize your dependency on them.

    I would like for these startups to address this issue instead of just giving you a script tag to paste into your site without thinking of the possible cost.

  3. Peter Michaux Says:

    I don’t think using a central source for common JavaScript libraries is such a great idea. At least not in all cases. I don’t think I would use such a source.

    http://peter.michaux.ca/article/7906

    These startups you mention don’t want you to think about the possible costs. That would discourage adoption.

  4. Joey Mazzarelli Says:

    Have you considered the security aspect of directly including 3rd-party scripts? You are effectively giving them the ability to steal cookies, alter the behavior of your site, read user data, etc.

    It requires little effort to make a local copy of the script and use that one instead. And you don’t have to worry bad behavior (malicious or accidental) nor updates breaking your site.

    Just a thought.

  5. Chris Blow Says:

    @joey: the js is dynamically written from the server, so you need a “live” connection to the third party.

    But WRT the security issue, you are right on to raise the issue: it was just a few months ago that perl.com was hacked because one of their ad partners (read: remote js) had their domain name expire! ….

    When was the last time you checked your
    “content partner’s” registration expiry? :)

Leave a Reply

Captcha
Enter the letters you see above.


About this site:
This is my (Jaisen Mathai) personal site for potential employers who want to see my resume or portfolio. My ideal job would be to work as a PHP developer on a large scale consumer website. My experience is in using PHP, MySQL, Ajax and JSON. I really enjoy creative brainstorming...taking a problem apart and narrowing 100 solutions down to the best one.

Thanks for stopping by. Be sure to drop me a line.