team1504 — 2012-02-09T18:03:08-05:00 — #1
Thanks in Advance!
ralphm — 2012-02-09T19:38:38-05:00 — #2
Actually, the current recommendation is to put JS links just before the closing </body> tag. That way, the page (HTML/CSS) gets to load visually without being held up by all the JS stuff—and you also avoid the problem of the JS having no HTML elements to apply itself to.
team1504 — 2012-02-09T22:03:03-05:00 — #3
But that doesn't validate, right? I thought all script tags be to be in the head
ralphm — 2012-02-09T23:26:23-05:00 — #4
No, they can be anywhere. Those recommending the JS links be put just before the closing </body> tag include some of the most prominent "standardistas".
xhtmlcoder — 2012-02-10T09:04:53-05:00 — #5
They should be linked externally is probably what you are thinking of since scripts are not markup and thus the LINK would be within the HEAD.
team1504 — 2012-02-10T18:10:42-05:00 — #6
oh okay. so what I have now learned is that <link />s are in the head and <script>s can be anywhere. Its best to put them just before the closing head tag (</head>) or closing body tag (</body>). Is this correct?
ralphm — 2012-02-10T18:13:31-05:00 — #7
The current thinking is that <script>s are best placed just before the closing </body> tag, but I've not heard that if they are in the <head> section they should be just before the closing tag, but I guess it makes some sense—at least from an organizational point of view. But yes, <link> should always be in the <head>.
team1504 — 2012-02-11T10:17:31-05:00 — #8
Okay. Thank you all— I think I got it now!
system — 2012-02-11T17:46:10-05:00 — #9
Honestly, each placement has it's own 'job' -- and depending on what I want it to do is likely where I'll place it. For example, if a script is running at 'onload' it doesn't matter where you put it, it can't execute until everything else is done... if it's a library that has to be loaded before it can be run in the first few lines of BODY, putting it right before /BODY means it's going to break... on the other hand if it's going to modify markup and you want it to run BEFORE onload as it say... modifies the markup in a manner you want applied before CSS loads (that way you only get one FOUC instead of TWO), then it goes before /BODY.
The last of those is the real reason I often put scripting right before </body> -- FOUC is bad enough for CSS or images applied via the CSS, without something like dynamic content or scripted controls suddenly popping in AFTER the CSS is applied -- waiting for onload can literally be like a second "Flash of unstyled content". (part of why I don't really use onload anymore)
This is one of those cases where I think people are looking for a "always do this" which is the wrong question. It's just not that simple an answer and is part of why it's called "work" and not "happy happy fun time".