1

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...:)

4
  • 1
    "Writing script inside head tag in javascript is best option, Why?" It's not always. Commented Apr 7, 2012 at 5:20
  • So, can you please share with me what are cases in which it is best and vice versa..?? Commented Apr 7, 2012 at 5:21
  • 2
    Here's an accepted answer with some discussions why the answer is not trivial: stackoverflow.com/questions/1213281/… Commented Apr 7, 2012 at 5:21
  • I for example think that the best option is to put the script tag as the last tag of the body instead... Commented Apr 7, 2012 at 5:21

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.