javascript - move to body tag? [duplicate]
-
29-04-2021 - |
Question
Possible Duplicate:
Where is the best place to put <script> tags in HTML markup?
I read this article. Now I am wondering, should I move my Javascript links into the <body>
tags rather than the <head>
tags for better performance (if the script is not needed during run-time)?
If so, should I put it right after the opening <body>
tag, or right before the closing </body>
tag?
Does it really matter where I put the Javascript if all the code is used in a window.onload
function?
Thank you!
Solution
Before the close of the body — after the content.
One of the things that means is that any stray <script>
tags with inline code that are scattered in the body cannot rely on external scripts. That can be a problem with some server-side templating systems that drop little blobs of code into the page. Sometimes those assume that something like jQuery or whatever are always included in the <head>
.