<head> <script language="JavaScript"> document.bgColor="#00cc00" </script> </head>
<body> <script language="JavaScript"> document.bgColor="#00cc00" </script> </body>
<html> <body> <script> document.forms[0].elements[0].focus(); // this will throw an error because it is loaded before the form element is loaded </script> <form> <input type="text"> </form> </body> </html>
<head>External JavaScript files cannot contain HTML, and must have the ".js" extension. Source file names follow the same rules as HTML document names. Additional statements may NOT be placed between an opening and closing script tag pair when they link to a source file.
<script language="JavaScript" src="myExternalJavaScript.js"></script>
</head>
<script language="javascript" type="text/javascript">You may have seen the word "JavaScript" appended with a version number. There are three supported versions of JavaScript, the first, which needs no declaration, 1.2 and 1.3. Although version numbers are not required, appending including it is one way to weed out older browsers that may not support the latest language functionality. For instance, including "1.2" or "1.3" will keep non DHTML browsers from attempting to execute scripts.
<script language="JavaScript1.3"> // no space between javascript and the version number!To hide scripts from any browser that does not support JavaScript (or if your document is written in XHTML), place an HTML comment directly below the opening script tag, and a closing HTML comment directly above the closing script tag, preceded by a JavaScript line comment. I don't use them in my examples because I assume my visitors will have a JavaScript capable browser!
<script language="JavaScript">
<--
statements here...
// -->
</script>
// this is my commentTo comment out an entire block, place a forward slash followed by an asterisk at the beginning of the block, and the same characters in opposite order at the end of the block.
/* here is my
entire comment
block */