+ 2

What's the difference if I put the script in the body, rather then the head?

20th Jun 2017, 8:35 PM
Brandon Stevenson
6 odpowiedzi
+ 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*
20th Jun 2017, 8:49 PM
Maz
Maz - avatar
+ 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'); }
20th Jun 2017, 8:44 PM
Maz
Maz - avatar
+ 3
Hmm just try to do it and see the result
20th Jun 2017, 8:56 PM
Achmad Imannudin
Achmad Imannudin - avatar
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?
20th Jun 2017, 8:48 PM
Brandon Stevenson
0
I'm a beginner in java
20th Jun 2017, 8:48 PM
Brandon Stevenson
0
Alright thank you very much
20th Jun 2017, 8:50 PM
Brandon Stevenson