Javascript is usually placed in the <head> tag if you need to make function or for Preloading things. It is used for things to recall later in your page, which will be used by the body area of you page at a later time.
Javascript that is in the <body> tag is used to create things that are
triggered by objects on you page in the body section, such as messages that
Javascript will write on the page for you, with the document.write line. It
is also used to make image rollovers in the body section of your page or basically
any time a link or image needs to call Javascript for some sort of behavior.
Always set the Default Scripting Language:
XML specifications require you to state your scripting language. You must declare
this in your <meta> tag.
ex.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Script-Type" content="text/javascript"
/>
<title>Auto Script</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<script type="text/javascript" language="Javascript">
document.write("Hello World!")
</script>
<p>Text not written with Javascript.</p>
</body>
</html>
Automatic Scripts:
There are two kinds of scripts---those that are "triggered" by a user
doing something and those that happen automatically. The ones that happen automatically
happen in the order that they appear in the html code.
ex.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type"
content="text/html;charset=iso-8859-1" />
<title>Auto Script</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<script type="text/javascript" language="Javascript">
document.write("Hello World!")
</script>
<p>Text not written with Javascript.</p>
</body>
</html>
note: "document.write" will write information on the
page that is between the brackets.
Calling external script pages
You can store external script files just like you would store a style sheet.
Put all javascript in a separate page and call it as needed.
ex.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type"
content="text/html;charset=iso-8859-1" />
<title>Comment</title>
<script type="text/javascript"
language="Javascript" src="scripts.txt">
</script>
</head>
<body>
<p>Hello.</p>
</body>
</html>
scripts.txt is the location of the script file on the server. This file just
has script in it.
Hiding Script from Older Browsers
Older browsers don't always know what to do with the <script> tag.
They will display the scripts if this happens. To avoid this, comment out scripts.
ex.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type"
content="text/html;charset=iso-8859-1" />
<title>calling scripts</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<script type="text/javascript"
language="Javascript">
<!--
document.write("Hello
World!")
//-->
</script>
Hello.
</body>
</html>
Hiding Script from XML Parsers
Sometimes scripts can use symbols that will mess up XML (symbols: &,
>).
To hide scripts from XML parsers (the program that makes sure the XML is written
properly):
ex.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type"
content="text/html;charset=iso-8859-1" />
<title>calling scripts</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<script type="text/javascript" language="Javascript">
<![CDATA[
document.write("Hello
World!")
]]>
</script>
Hello.
</body>
</html>