+ 6

Will it be legal ?

If i write the JavaScript code in body which has to be written in head , then will the code works?

22nd Apr 2019, 5:37 AM
RISHABH MISHRA
RISHABH MISHRA - avatar
7 Réponses
+ 9
"Will it be legal?" Yes, you can put a <script> tag in either the body or in the head. "Will the code works?" It depends on what the code does. If the script is supposed to be there before the document loads, it is best to put the script in the head. If the script needs to be there after the document loads (e.g. if it references or operates on an element in the body), then it should be at the end of body or it needs to include a mechanism to defer running the script until after the body loads. I hope this helps. Happy coding!
22nd Apr 2019, 7:29 AM
Janning⭐
Janning⭐ - avatar
+ 7
Maybe if you write it at the beggining of the body.
22nd Apr 2019, 6:05 AM
🍇 Alex Tușinean 💜
🍇 Alex Tușinean 💜 - avatar
+ 4
yes you can....
22nd Apr 2019, 2:55 PM
Md Shadab Alam
Md Shadab Alam - avatar
+ 3
You can try it out and see what happens. That is a great way to learn and to become a better programmer.
22nd Apr 2019, 7:02 AM
Dragonxiv
Dragonxiv - avatar
+ 2
Yes it will be legal using both ways but it all boils down to what is the code doing. If you want the code to perform when the page loads it will be preferable to put it in the head. Note: you are the best person to decide where your script tag should be place because after it is your program
22nd Apr 2019, 7:38 AM
MrHumble
MrHumble - avatar
+ 1
If the javascript code will query any element below where the script tag is placed, then it won't find such element
22nd Apr 2019, 1:06 PM
Victor Mbamara
Victor Mbamara - avatar
0
Maybe
22nd Apr 2019, 12:01 PM
Somesh Kumar
Somesh Kumar - avatar