5 Replies - 623 Views - Last Post: 01 May 2012 - 03:52 AM

#1 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

In the body or in the head?

Posted 24 April 2012 - 09:17 PM

No I'm not talking about POT. I assumed since this was a javascript forum you would suddenly think the question was "where to put the code"?

Some say inside the head is better. Others say inside the body is fine. While others don't care and do both. If you talk to some one who has chosen one over they other either is a best practice to them.

Why would either be a best a practice?

I not talking about the script attribute of html. I mean the script tags and the code that goes in them.
<html>
<head>
<script type="text/javascript">//whatch you down there script tag?!!</script>
</head>
<body>
<script type="text/javascript">//nothin! what are you doin up there?</script>
</body>
</html>



I kinda get the "occasion for this or that" scenario. I'm just wondering if there is enough of a reason to take it way beyond opinion. I mean way beyond. Like stuff will happen dude. If you put that there stuff will happen that is going to feel like a rough massage from a hairy burly dude that has eyes for you. Unless you like that kinda stuff. I don't judge.

Is This A Good Question/Topic? 0
  • +

Replies To: In the body or in the head?

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3530
  • View blog
  • Posts: 10,175
  • Joined: 08-June 10

Re: In the body or in the head?

Posted 24 April 2012 - 10:30 PM

View Posthiddenghost, on 25 April 2012 - 06:17 AM, said:

Some say inside the head is better. Others say inside the body is fine. While others don't care and do both. If you talk to some one who has chosen one over they other either is a best practice to them.

you just have to visualise the pros and cons of both variants. it then becomes relatively clear in which case either way is best/worst practice.
Was This Post Helpful? -1
  • +
  • -

#3 vasdueva  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 03-April 07

Re: In the body or in the head?

Posted 25 April 2012 - 06:45 PM

Check out:
http://developer.yah....html#js_bottom
Was This Post Helpful? 1
  • +
  • -

#4 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: In the body or in the head?

Posted 27 April 2012 - 04:35 PM

View PostDormilich, on 25 April 2012 - 12:30 AM, said:

View Posthiddenghost, on 25 April 2012 - 06:17 AM, said:

Some say inside the head is better. Others say inside the body is fine. While others don't care and do both. If you talk to some one who has chosen one over they other either is a best practice to them.

you just have to visualise the pros and cons of both variants. it then becomes relatively clear in which case either way is best/worst practice.


From what I've read there are two different reasons for either.
If a script is used for additions to the functionality at page load it is ok to place it in the head.
If the script modifies content after page load then it should be in the body just before the closing body tag.

A third option is if the script is a set of functions that modify the content after page load you can place the set of functions(library) in the head, and place the function calls in the body.

What I think is that there really is no defining characteristic except personal feeling about organization, and whether or not the script modifies content after the html loads.

I'm looking for any other reasons that might be applicable to these points of view.

I've seen source code where the javascript is splattered all throughout the body. Intermittent html and script tags flowing through the page like they just didn't care.
Don't know if that violates the MVC paradigm, but it definitely has some benefits. Like how the script part is very close in the markup to the DOM object it controls.

I guess you could say I was looking for what I haven't thought of yet.

This post has been edited by hiddenghost: 27 April 2012 - 04:37 PM

Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3530
  • View blog
  • Posts: 10,175
  • Joined: 08-June 10

Re: In the body or in the head?

Posted 27 April 2012 - 10:37 PM

View Posthiddenghost, on 28 April 2012 - 01:35 AM, said:

I've seen source code where the javascript is splattered all throughout the body. Intermittent html and script tags flowing through the page like they just didn't care.
Don't know if that violates the MVC paradigm, but it definitely has some benefits. Like how the script part is very close in the markup to the DOM object it controls.

it doesn’t violate MVC, because that’s something completely different. but it violates Good Coding Practice ("where the heck do I call ...?"). JS doesn’t care how "near" it is the element concerned and if you have two (or more) different elements in that block, where is it "nearest"?

one downside of JS in the body I would mention is that this JS still needs time to load after the HTML element is rendered, so you might run into FOUC issues.

This post has been edited by Dormilich: 27 April 2012 - 10:39 PM

Was This Post Helpful? 1
  • +
  • -

#6 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: In the body or in the head?

Posted 01 May 2012 - 03:52 AM

Had to look up FOUC. Ya, I could see that being a problem.

When I was thinking about "nearness" I was considering the relationship of the script to the html it's manipulating.

The ability of js not to care how close it is to html is why I started this thread. JS can be anywhere.

So far the only reasons I'm seeing are that:
When the script controls elements during page load it should be before the html it manipulates to prevent FOUC.
When the script controls elements after page load it should be after so the html nodes load for access by the control.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1