+ 2
What's the difference if I put the script in the body, rather then the head?
6 Answers
+ 9
Add the JS in the <head> with the <script> tag and use the onload event in an external ".js" file.
*Just the best practice*
+ 8
In a nutshell... if you put the <script> tag at the end of the <body>, DOM elements are loaded and you can access to them directly (document.getElementById for example), if you put the script in the <head>, in most cases you have to use the 'onload' event:
Example -
onload = function ()
{
document.querySelector('something');
}
+ 3
Hmm just try to do it and see the result
0
so it's easier to access in the body, while in the head I would have to add extra code for the script to run? or for what's in the script to run?
0
I'm a beginner in java
0
Alright thank you very much