What's the difference between using a javascript in the head and within a body? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What's the difference between using a javascript in the head and within a body?

9th Mar 2018, 9:45 AM
Bismay Mohanty
Bismay Mohanty - avatar
3 Answers
+ 12
@Brains : There still can be problems if we add the <script> tag in body :p ..It should be placed at the bottom (just above the </body> ) Alternatively, We can use :- window.onload = function (){ // code here }
9th Mar 2018, 10:07 AM
Nikhil
Nikhil - avatar
+ 8
I think you using a script tag.well when you write a script in the head it runs before the body has even loaded,so expect errors! writing a script in the body however allows all html code positioned before it in the body to run before executing. hope I helped
9th Mar 2018, 9:56 AM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
+ 5
I quote " writing a script in the body however allows all html code positioned before it in the body to run before executing. " This means any element positioned before the script tag is declared in the body loads soo the script must be at the end
9th Mar 2018, 11:45 AM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar