views:

109

answers:

3

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 xxx.com" alot 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 async so it doesn't slow the loading of the rest of my page at all.

A: 

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).

galambalazs
+4  A: 

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.

patrick dw
Interesting... That's probably also good for JSON/P.
Lèse majesté
+5  A: 

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.

Jason McCreary
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 majesté
@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.
Jason McCreary
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`.
Lèse majesté
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.
Jason McCreary