HTML & CSS
Thanks in Advance!
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.
But that doesn't validate, right? I thought all script tags be to be in the head
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".
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.
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?
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>.
Okay. Thank you all— I think I got it now!
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".
This topic is now closed. New replies are no longer allowed.