Forum Moderators: open

Message Too Old, No Replies

0ptimising pages by moving JavaScript out of head

When must JS be in head and when can it be placed before closing head tag?

         

SilverLining

1:23 pm on Feb 19, 2008 (gmt 0)

10+ Year Member



I'm in the process of optimising web pages as much as possible and have read before that one of the recommended ways is to move JavaScript scripts to the bottom of an HTML page, just before the closing head tag. I take it this is mostly for scripts such as urchin tags or scripts which the page layout is not dependent upon.

Could you please give guidelines as to when scripts do not need to be inside the head tags. Thanks.

Fotiman

6:34 pm on Feb 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




just before the closing head tag.

You mean just before the closing body tag.

I take it this is mostly for scripts such as urchin tags or scripts which the page layout is not dependent upon.

Uh... you should never have scripts for which the page layout is dependent upon. JavaScript should be an enhancement, not a requirement.

All scripts should be able to be placed just before the closing body tag unless they use bad practices.

SilverLining

6:40 pm on Feb 21, 2008 (gmt 0)

10+ Year Member



Thanks, Fotiman. Yes, I did mean the closing body tag. I just wanted clarification if it mattered whether JS files were moved out of the <head> section.

Achernar

7:02 pm on Feb 21, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



When javascript code is linked from the <head>, it can start acting on the page code before it starts displaying (document.body still doesn't exist).

SilverLining

10:00 am on Feb 25, 2008 (gmt 0)

10+ Year Member



Thanks Achernar.