开发者

When is it ever appropriate to place Javascript inside the body of a document?

开发者 https://www.devze.com 2023-01-27 21:36 出处:网络
The client has a bunch of Javascript that calls Microsoft Ajax etc. Aside from web page analytics which make sense to load at the same time static resour开发者_运维技巧ces are loaded, wouldn\'t it mak

The client has a bunch of Javascript that calls Microsoft Ajax etc. Aside from web page analytics which make sense to load at the same time static resour开发者_运维技巧ces are loaded, wouldn't it make sense to load any AJAX scripts in the head instead of the body?

Ultimately the result is a bloated load time for the page since all the Javascript is being loaded at time of render.

What is your ideal placement of certain Javascript in a web page?


Placing Javascript at the end of the body can help with the perception of good page load speed. Scripts in the head block other activity while they're run.

The claim of "bloated load time" is actually backwards. Putting the scripts in the <head> means that the page won't be parsed and none of it will be rendered until all the scripts are loaded and executed. (Well, all the directly-loaded scripts.)


Best practice is to put your scripts at the bottom of the document. Yahoo covers it on their Best Practices.


you can place javascript anywhere you like.

you usually wanna put external JS in the header section.

Other than that. you can put them anywhere.

0

精彩评论

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