JavaScript in or just before ?

I think a lot of developers run JavaScript just before the </body> so that it is run 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 run until the page and its elements are fully rendered by doing the following:

   //Code here

Then the script reference can be put in the head tag.

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.

Leave a Comment