javascript

  • Thread starter Rafal 'Raf256' Maj
  • Start date
R

Rafal 'Raf256' Maj

What is the correct way of put javascript code in XHTML 1 document, in

1) head

2) body
 
D

David Dorward

Rafal said:
What is the correct way of put javascript code in XHTML 1 document, in

1) head

2) body

Its valid in either, it usually only makes sense if you stick it in the head
(becuase it isn't content unless you document.write() and document.write
isn't generally a good idea in XHTML).
 
L

Louis Somers

What is the correct way of put javascript code in XHTML 1 document, in

1) head

2) body

I prefer the head because non-supporting browsers are less likely to print
the script on the users screen. If you choose to put it in the body
remember to use the <!-- comments, so non-supporting browsers won't
display the code.
 
T

Toby A Inkster

Louis said:
I prefer the head because non-supporting browsers are less likely to print
the script on the users screen. If you choose to put it in the body
remember to use the <!-- comments, so non-supporting browsers won't
display the code.

You can't use <!-- to hide JavaScript in XHTML: the comment marks will
*really* make it into a comment and it will be ignored by conforming
browsers.
 
R

Rafal 'Raf256' Maj

(e-mail address removed)
I prefer the head because non-supporting browsers are less likely to
print the script on the users screen. If you choose to put it in the
body remember to use the <!-- comments, so non-supporting browsers
won't display the code.

Thanks, and - how exacly looks sample JS code in XHTML?

<!DOCTYPE ...>
<html>
<head> ...
<script language="javascript 1.1">

here_some_js_code

</script>
</head>
<body>
....


?
 
M

m

Rafal said:
(e-mail address removed)


Thanks, and - how exacly looks sample JS code in XHTML?

<!DOCTYPE ...>
<html>
<head> ...
<script language="javascript 1.1">

here_some_js_code

</script>
</head>
<body>
...


?
No, like:
<script language="JavaScript">
<![CDATA[
.......................your javascript code.............
]]>
</script

The CDATA section prevents your javascript from being
interpreted as part of an XML document proper.

Just do yourself a favor: link to a separate
file and avoid all this.

I don't know why I can never convince beginners
that separate files will save you work instead
of costing; but I theorize that it has something
to do with being awkward on an editor. Put your
CSS and Javascript in separate files _now_-- you'll be
glad you did as your site grows.
 
R

rf

Louis Somers said:
I prefer the head because non-supporting browsers are less likely to print
the script on the users screen. If you choose to put it in the body
remember to use the <!-- comments, so non-supporting browsers won't
display the code.

The only non-javascript that does not simply ignore the entire <script>
element (as it should) was a IIRC certain release of Netscape 3 which only
lasted for a few months anyway. The use of HTML comments inside a <script>
element has been cargo cult from then on :)

Cheers
Richard.
 
S

Steve Pugh

Rafal 'Raf256' Maj said:
(e-mail address removed)

Yeah, there are so many Netscape 1 browsers still in use...

These days even browsers that con't support JavaScript (or any other
Thanks, and - how exacly looks sample JS code in XHTML?

<script language="javascript 1.1">

here_some_js_code

</script>

In theory the combination needed to satisfy both XHTML rules and older
tag soup browsers is

<script type="text/javascript">
<!-- --> <![CDATA[ /> <!--
here_some_js_code
// --> <! ]]>
</script>

In practice putting it in an external .js file is a much better idea.

Steve
 
T

Toby A Inkster

rf said:
The only non-javascript that does not simply ignore the entire <script>
element (as it should) was a IIRC certain release of Netscape 3 which only
lasted for a few months anyway. The use of HTML comments inside a <script>
element has been cargo cult from then on :)

This is the case for <script> elements in the document's <head>, but for
<script> elements in the <body>, a lot of older browsers will display the
script contents.
 
N

nice.guy.nige

While the city slept said:
What is the correct way of put javascript code in XHTML 1 document, in

1) head

2) body

Traditionally, javascript has been placed in the head so that any functions
called from the body will have been loaded in. It also makes sense (as
suggested by other posters) to link to a javascript file - especially if
your script is being used on more than one page - as changes to the script
only have to be made on one file.

Cheers,
Nige

--
Nigel Moss.

Email address is not valid. (e-mail address removed). Take the dog out!
http://www.nigenet.org.uk | Boycott E$$O!! http://www.stopesso.com
In the land of the blind, the one-eyed man is very, very busy!
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top