Why is it recommended to put the javascript codes at the end of the body tag scope? [duplicate]

5

Do you have any rules regarding the use of scripts in the head tag? W3C has not made the usage rules very clear.

    
asked by anonymous 02.09.2015 / 05:25

1 answer

4

Because the main elements of the page, which should be the most relevant, can already be rendered before the end of the verification and interpretation of the parts in JS, which are usually important only after the page loads.

If the JS code is placed at the beginning or middle, page rendering is obstructed until all script is parsed.

Obviously this does not work so well in all cases. It may be that correct and complete rendering is only possible when script starts to run.

There are a number of techniques to better control this load and in a few cases this recommendation should be followed.

Then nothing prevents and in general put tag head is advantageous, especially if loading asynchronously or with delay:

<script defer>

or

<script async>

See how it works .

Where to use defer .

Where to use async .

    
02.09.2015 / 05:44