Is there a valid reason about putting JS in the head of document?
Question by markzzz
I mean : I know the JS is cached only if it come from a .js file. Also, 90% of my functions must be rendered when the page (html) is loaded (rendered), so it is better put JS before closing the body tag. (this prevent also to use document .ready(); and the loading of the page itself will be more faster).
So, which is the advantage on putting JS in the <head></head>
? Expect the “order” of the code, which I don’t mind so much to be honest…
Answer by Starx
The important thing about the elements in the <head>
section are that they are loaded, before the <body>
starts to load.
This is a very efficient feature, which are used a lot (IMO).
Loading of libraries, scripts, that have to run before the DOM has loaded have to done in <head>
section.
I will give you a scenario
Imagine, You needed to calculate the 30% size of the screen’s total size and assign it to the element inside.
It would be foolish and wait for the element to load big, then run the script to load resize it again.