Where to place Javascript code?

S

shapper

Hello,

Where should I place my Javascript code in my HTML document?

Inside the Head tag or Body tag? I saw the two versions.

And where should I place references to .js files?

Thanks
Miguel
 
N

Nag

Hello,
Its a good practice to palce the Javascript code in the
<Head>.........</Head> section. You can still add the code within
<BODY> section only if it is required.
Usually .JS file will be included in the <HEAD> section


Regards,
Nagendra B.R
 
T

Thomas 'PointedEars' Lahn

Douglas said:
Put your scripts in external .js files.

That contradicts with rule #1 of your recommendations below. Not every
script has to be put in another resource, forcing another HTTP request.
Put your script tags as close to the </body> as possible.
See http://developer.yahoo.com/performance/rules.html#js_bottom

There is the `onload' event handler so that this is unnecessary.

Other of those recommendations are even less sound. For example, to minify
script code does not necessarily serve to reduce the length of the script
resource, and it can increase the length of the script resource. But it
serves to make the resulting code less legible, and it introduces the QA
issue that the resulting code is only as good as the minifier because it is
virtually impossible to verify that the minimization was done correctly and
did not change the semantics of the code and did not introduce less
efficient syntax.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,383
Latest member
EzraGiffor

Latest Threads

Top