2

I have a section of a webpage that loads a JavaScript file from an external source and then kicks off an Ajax query.

When I load the page, I see the browser saying "waiting for example.com" a lot, so I think the dependency on this external JavaScript is slowing my initial page load.

Is there a way I can load this external JavaScript asynchronously so it doesn't slow the loading of the rest of my page at all?

1
  • 1
    defer isn't widely supported. I deleted my answer. Commented Jul 5, 2010 at 16:27

4 Answers 4

6

It's good practice to put JS at the bottom, right above the closing body tag. In addition, use load events window.onload or $(document).ready() to fire your JavaScript after the page has loaded.

As far as loading JavaScript files themself asynchronously or on demand, you could inject it from another JavaScript function or event. But really you are doing the same thing as placing it at the bottom.

Check out the YSlow Guidelines for front-end optimizations.

Sign up to request clarification or add additional context in comments.

4 Comments

I really dislike the recommendation to put scripts at the bottom of a page. I personally put scripts (other than those that call document.write(), which I almost never use anyway) in the HEAD as it seems more semantically correct and looks cleaner grouped with all the link tags. Besides, the "waiting for" status will show up anyway when you fire the AJAX request immediately after the page loads. So to the user it shouldn't make a difference.
@Lèse, this is a proven, industry backed optimization. It's not a personal recommendation or a requirement. But as far as semantic, there's nothing in the HTML spec against script tags outside the head tag. As such, there is really only an organization reason. So what's the difference between the head and bottom of the body? Agreed about the status message though.
I understand that this is a recommendation by Yahoo and a lot of big sites use it, or at least use it for some of their external scripts like analytics, and not just you throwing it out there. I just don't like the practice. And semantics isn't necessarily tied to the language specification. It's perfectly legal to use tables for layouts, but that doesn't make it semantically correct. The point is that when you link a JS library, it's meta information that isn't part of the document (or document body at least). It's the same reasoning that the link tag is placed in the head.
True about semantic. It gets used synonymously with valid, which is what I should have said. But your analogy about the link tag is false. That is, in fact, part of the HTML spec and would make your page invalid if you placed it outside the head.
4

You could use jQuery's .getScript() method, which is simply a wrapper for an AJAX call.

http://api.jquery.com/jquery.getscript/

This makes the request asynchronous, and gives you a callback that runs after the script has loaded.

Comments

0

You can see my answer here: Dynamic (2 levels) Javascript/CSS Loading

And grab the script from here (see the source). Use it at the bottom, and your scripts will not block other resources (and if you got more than one they will be downloaded in parallel cross-browser).

Comments

0

I wrote a library to asynchronously load javascript files with callbacks for when it loads:

https://github.com/ssoroka/sigma

Sigma.async_script_load('http://example.com/underscore/underscore-min.js', '_', function() {
  _([1,2,3,2,3,1]).uniq();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.