I think a lot of developers run javascript just before the </body>
so that it is ran after all the elements have been rendered.
However, if you organise your code correctly, the position on the page doesn't matter.
For example, when using jQuery, you can ensure the code isn't ran until the page and its elements are fully rendered by doing the following:
$(document).ready(function(){
//Code here
});
Then the script reference can be put in the head
tag.
Update - Script tags should be referenced just before </body>
. This prevents render blocking while the scripts load and is much better for site perception speed.
No obtrusive javascript should be used when using this technique.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…