开发者

the location of javascript in the html?

i am a new learner of javascript, from a book, i know there are three parts to put into javascript in html. one is in <head>...</head>. two is under <body>. the last is under</body>. which is best? what's difference 开发者_如何学JAVAbetween them?


Depends...

Advantage of having it appear before </body> is the loading of the scripts won't block the parsing of the page (because downloaded scripts are synchronous, because of possible document.write() etc), and you don't need to wait for DOM ready for descendants of the body element.

It is also recommended by Yahoo.

Keep in mind any scripts that modify the same element they are in will trigger Operation aborted in IE6 and 7.

In saying this, however, I place my scripts in head as it seems like a more semantically correct element to place them. It is also easier for maintenance, only having to check in one spot for scripts.

If the downloading of the JavaScript ever became an issue with my sites, however, I would have no issue with placing them before closing </body> tag.


YSlow recommends that you put JavaScript just before the </body> tag. That way they don't block parallel downloads:

http://developer.yahoo.com/performance/rules.html#js_bottom


The current popular convention is to place scripts before the closing </body> tag.

In general, you may want to place a script in the head that will modify the html/css behavior, libraries such as modernizr, html5shiv, etc. are meant to supplant the behavior of CSS, so should be in the <head>. Those are the only scripts I would place in the head section of the document.

Designing your page/site so that scripting isn't needed (to as much of an extent as possible) is the best guideline you can have for a public facing site that may rely on search traffic. If you are developing an intranet/extranet/saas application it isn't as important. What will go along with this is placing scripts at the bottom before the closing body tag, so that all other downloads on the page occur first.

Beyond any of this, having your scripts limited (through minification, and merging) to 6 on a page will allow for scripts to be downloaded simultaneously on browsers that support that behavior. There are tools/techniques that improve on this. Some will bind to the window/load or document/ready events so can be placed anywhere. Having your scripts, css and images on separate CDN domains can help (or spread across a few CDN domains). Avoid having more than 4 total domains in use on a given page as much as possible (excluding common ones such as google analytics, which is likely to already be in the local DNS cache.

I would suggest reading "Even Faster Websites" by Steve Souders for more coverage on this area. Optimizing performance is about more than just where to put your JS.


You should usually put JavaScript in the <head> section (unless certain conditions apply, which are explained below). Some people put it in the body to make quick and dirty changes to the document, but these days it's really not good practice.

Putting it in the head ensures that it loads when the document loads, and it's also easier to manage when it's all in one spot, rather than sprinkled around the page.

Some scripts that may request things from servers are put at the end of the body to prevent "hanging" the page load, but at this point you won't be doing that.

In short, it goes in the <head> section.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜