Script tag | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Script tag

In the previous question of my tutorial the script tag was placed inside a body tag but in the next question it is said to place the script tag inside a head tag why ???

31st Mar 2018, 4:40 AM
Kishore
2 Answers
+ 4
<script> tag can be placed in <body> or <head> tag. It is not mandatory that it should be in body or head. You can add it where you think it is best. But I recommend you to add it in head section to make the page load easily.
31st Mar 2018, 4:46 AM
Akash Pal
Akash Pal - avatar
+ 3
As Akash Pal mentioned, it can be used in both <head> and <body> without problems. The biggest difference between both methods is when your script will run: In the head your script will load before the elements of the page, so if you do some manipulation to the DOM will have an undefined behavior (in these cases you can always use window .onload or any similar method). The Body the script will load next to the elements / after the elements of the page (depending on if you put it at the end of the body or in some specific place). Some people recommend putting the scripts at the bottom of the pagem, so in case the loading of the same fails your page would be displayed correctly. And depending on the size of the script (and the visitor connection) it also gives the impression that the site load faster
31st Mar 2018, 5:16 AM
Mickel
Mickel - avatar