tags:

views:

164

answers:

5

In order to shorten the time to open the page,

I'm thinking of load part of the scripts after the page is loaded.

The standard is to append new scripts into <head> part,right?

+3  A: 

Just put your scripts at the end of the <body>, that way everything before will load first.

peirix
what's with the downvote and no comment?
peirix
Didn't think a comment was necessary, no need to be uptight. Do you have any explanation/source for why this helps? Unless your scripts are hundreds of kb I don't think the "blocking" is really an issue.
DisgruntledGoat
Wasn't really being uptight, just feels like a comment is in place with a downvote. And it's pretty self-explanatory. The OP wanted to load scripts after the document was finished loading. Placing them at the end of the body does just that. Since there was no mention of sizes, I assumed the OP has his reasons to want to avoid blocking.
peirix
A: 

To add to that point, if you are going to place all script tags at the bottom, and you are using some javascript libraries. Please be careful in handling your javascript embedded in your html, with onclick, onchange.. options. they might start to throw errors.

Trying to keep javascript as unobtrusive as possible is the key to make this above method work.

Rishav Rastogi
+2  A: 

If you don't want to put it at the end of body, as suggested, you might to take a look at jQuery.getScript(url, callback)

Henk
This is what I was looking for if not putting right before </body>
Shore
+1
Shore
A: 

In general I would suggest putting your own scripts in the head and external scripts just before </body>, for a few reasons:

  • Your own scripts won't block your site too much, particularly if you have only one or two. If they're taking a long time to download then the whole site (images etc) will be slow.
  • External scripts can often suffer delays (I've had loads of trouble with addthis.com's script and even google ads/analytics occasionally), which is why I suggest putting them at the bottom.
  • jQuery modifies the DOM as it loads, so putting the scripts at the end of the page can make elements appear "unstyled" or unfinished. For example a drop-down menu would not work until the entire page is loaded.
DisgruntledGoat
A: 

Some user agents understand the defer="true" attribute on the element.

See here.

meanstreakuk