+ 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?
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!
+ 7
Maybe if you write it at the beggining of the body.
+ 4
yes you can....
+ 3
You can try it out and see what happens.
That is a great way to learn and to become a better programmer.
+ 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
+ 1
If the javascript code will query any element below where the script tag is placed, then it won't find such element
0
Maybe