Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.
The best practice is to put JavaScript <script> tags just before the closing </body> tag rather than in the <head> section of your HTML. The reason for this is that HTML loads from top to bottom. The head loads first, then the body, and then everything inside the body.
You can place the <script> tags, containing your JavaScript, anywhere within your web page, but it is normally recommended that you should keep it within the <head> tags. The <script> tag alerts the browser program to start interpreting all the text between these tags as a script.
Every once in a while I hear about placing HTML <script>
tags later in the HTML document rather than in the <head>
element.
Some people, or so I've heard, even advocate placing one's scripts as the last few tags before </body>
.
Is this due to a performance issue? Perhaps loading up a script is a blocking IO operation, considering that script-dependent scripts are placed after other scripts like so:
<script src="jQuery.js"></script>
<script src="myScriptThatUsesjQuery.js"></script>
Even if that's the case, why would placing one's scripts near the end of the HTML document help?
Thanks!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With