Urs
Urs

Reputation: 5132

javascript in the <body>

I used to believe that you should not insert javascript blocks

<script language="javascript"> 
<!-- 
//--> 
</script> 

into the body part of a (what, HTML, XHTML?) document, but rather into the head.

But is that still true?

Upvotes: 2

Views: 272

Answers (5)

Artilheiro
Artilheiro

Reputation: 4005

It's not recommended because if you try to access elements in the body itself (i.e forms, fields, etc) since they may only become available once the entire body has rendered. However, it's a valid and actually very common practice.

Upvotes: 0

bpeterson76
bpeterson76

Reputation: 12870

Script in the body (not links to external files) is like putting CSS in the head--people move toward separating it so that they can have the markup and logic separate for clarity and ease of maintenance.

I'm a big fan of the document ready feature of Jquery...but that's just personal preference. A dom loader is really the only way to guarantee loading is going to be identical between the various different browsers. Thanks, Microsoft!

I say use common sense...it's not worth doing another file for a single line of code...or even two. If we all went to the extremes that best practices sometimes ask us to go, we'd all be nuts.....or at least more nuts than we are now.

Upvotes: 1

Sarfraz
Sarfraz

Reputation: 382919

But is that still true?

It is a matter of good/best practices. HTML and Javascript should be separate. This is even knows as unobtrusive javascript/code.

More info at wikipedia:

Unobtrusive JavaScript

Although this is a good practice, but you can still put javascript at any part of the page, however you should avoid this as much as possible.

Some advocate that javascript should only go at the end of the page, for example they seem to say that is is better in terms of SEO (Search Engine Optimization) as well as performance as denoted by @David Dorward in his comment.

Upvotes: 1

DHuntrods
DHuntrods

Reputation: 686

According to Yahoo, for the best performance it's recommended to put any script tags at the end of your document just before the closing html tags:

http://developer.yahoo.com/performance/rules.html

Google suggests using a deferred method to load scripts:

http://code.google.com/speed/page-speed/docs/payload.html#DeferLoadingJS

But they should almost always be script calls to an external .js file. There are very few occasions where it's better to have the .js embedded on the page.

Upvotes: 0

Unicron
Unicron

Reputation: 7466

But is that still true?

I'm not sure it ever was. Are you thinking about <style> CSS elements? Because those are illegal in the body.

But it is usually the better choice to put Javascript code into the head or a separate script, and wrap it in a document.ready or onload event.

However, in-body Javascript can have its place, for example when embedding external JavaScripts that document.write() stuff into the document. Top-modern, bleeding-edge Google Analytics relies on a <script> segment being inserted into the very end of the body.

Upvotes: 1

Related Questions