Newsgroup: it.comp.lang.javascript
Titolo: In the head? Or not in the head?

Hi All
Down below I've got two code examples. One embeds a JS include in the
<head>, the other does not (it puts the include between the </head> and the
<body> tags. Both work equally well (from IE and Nestcape version 3, to
current browsers). I understand that the recommended practice is to embed JS
in the <head>. But...
Is there a practical reason why JS should be embedded in the head rather
than where it is embedded in the second example? I'm not talking about the
"way things should be done" or the "way it's always been done" or "there's
no reason not to put it in the head". I'm talking real, practical reasons
that make one implementation different from the other.
Thanks
Rob
============ Begin code example 1 ===============
<html>
    <head>
        <title>A Page</title>
        <script language="JavaScript" src="ClickHandler.js">
    </head>
    <body>
        <img src="image.gif" onclick="clickHandler();">
    </body>
</html>
============ End code example 1 ===============
============ Begin code example 2 ===============
<html>
    <head>
        <title>A Page</title>
    </head>
<script language="JavaScript" src="ClickHandler.js">
    <body>
        <img src="image.gif" onclick="clickHandler();">
    </body>
</html>
============ End code example 2 ===============

 Rob
D'ya want to know the reason? The REAL reason? In the golden days (and by
this I mean the mid-nineties) many browsers would render JS as html on the
page unless it was properly commented out, which many authors didn't want to
do. The practice of placing JS in the <head> section traces to the implicit
contract with the browser that nothing placed between the <head> tags will
be rendered in the main page area.
Everything Karl says is correct, by the way.
Matt

be rendered in the main page area.
This isn't true at all, all browsers, have an implied any content on
the page will start the body, <BODY> is an almost universal optional
tag in renderers.
<head>
MOOMIN
</head>
<body>
SNORK
</body>
Will display, MOOMINSNORK on most (all?) browsers.
So even javascript in the head has _had_ to be commented out
throughout its life.
Jim.
-- 
FAQ: 
http://jibbering.com/faq/
Old FAQs: 
http://jibbering.com/faq/old/

 throughout its life.
That's simply not true.
Scripts in the head don't show, commented out or not, with at least NS and
IE 4+ (probably earlier versions, too, but I don't have any around to
check).
---
Hal Pawluk
 
(626) 966-0058
 
http://www.pawluk.com/
   It's not marketing if it doesn't go for the throat.

 
In the Netscape JavaScript Guide Version 4.0 I read:
"In Navigator JavaScript, it is a good practice to define all your functions in 
the HEAD of a page so that when a user loads the page, the functions are loaded 
first".
So I thing that placing all the functions in the HEAD section of the document, 
they're loaded and ready for the browser if some document.write (for example) 
in the BODY section requires them. Ant this is true for Explorer and other 
borwsers too. 
I'm right?
p.s.
I apologize if I commited some english-language error :-)
-- 
Ciao, Riccardo
------------------------------------------------------------------------
WebMaster: ----> 
http://www.jsdir.com
 <---- (aka 
http://www.jspoint.it
)
              All the JavaScript you need
------------------------------------------------------------------------
               Nessun traguardo è abbastanza lontano.
------------------------------------------------------------------------

check).
Of course not they are SCRIPT aware browsers, you don't need to
comment out scripts in any SCRIPT aware browser.  Try the same but
with MOOMIN instead of script - for some future MOOMIN tag that I've
just invented and is supported in the Snufkin browsers, you'll need to
comment out anything between the Moomin tags, to stop them being
displayed in browsers less advanced than Snufkin - this is the same as
SCRIPT happens pre script.
Jim.

the functions are loaded first".
FYI: in IE 5 I currently place most of the UI-related scripts in the 
style sheet (oh yeah - start to get used to it :P) and external XML 
files. You can't imagine how clean the code looks :P (I can almost hear 
the kiddiez cry "WH3R3'Z TH3 SKR1P7!!!" - hmmyeah... delight...)
Scripts in the <body> (not inlines) are almost always bad design choices 
or ugly compromises. They generate content? document.write()'s? then you 
should very likely have used server-side code. Yes, some hosts don't 
allow you to do server-side thigs, the answer is simple: Yahoo Geocities 
is NOT a web hosting service. Get a REAL one, like Freedom to Surf, 
instead. Geocities is for beginners who think Javascript has something 
to do with coffee and theatres, and 1337 H4X0R5 looking for a safe place 
to dump their pr0n, mp3z & W4R3Z (and have their accounts revoked before 
they can say "BR17N3Y 5P34RZ"). We serious hardcore webmasters look 
elsewhere :P
A good thing to place in the <body>, instead, are scriptlets. They look 
much like <iframe>'s, but the scripts of the page displayed inside them 
can customize the interface exposed to the parent object, and generate 
custom events too. That is, custom interactive embeds. Object-oriented, 
scalable, easy to maintain. Microsoft ships the scriptlet host as a 
plugin since IE 4, Netscape just doesn't seem to get it
PS - DAMN ANGLOPHONES!!! the "it" is for "ITaly", not "Information 
Technologies"!!! ;)

plugin since IE 4, Netscape just doesn't seem to get it
Some people think it's important to support all browsers, and browser
specific ones - especially misguided implementations like scriptlets
that will not find support outside IE ever - are therefore avoided (MS
themselves deprecated them in IE5 in preference to the more sensible
"behaviors" [sic] )
Jim.