Reputation: 1235
I know, I am asking very simple and stupid question but i want to know each and every thing in java script. We can write script tag in head or body section. but, Writing script inside head tag in javascript is best option, Why?
I know one thing, it processes serially..apart from that, i want to know other reasons from you all experts.
would be grateful for help.
Thanks in advance...:)
Upvotes: 1
Views: 811
Reputation: 86505
It's not always the best option.
As you've said, scripts run serially. What's more, they're run pretty much as soon as they're seen in the page...and they tend to tie up the browser while they're running, and keep it from working on loading stuff and rendering the page. Many times it's better to have scripts at the end of the page, for just that reason -- by the time they run, the user has something to look at, so the delay isn't as obvious.
Personally, i only put scripts in the head
when other scripts in the page require them. Libraries and such. Scripts that actually do stuff while the page is loading, go into the body.
Upvotes: 3