[JavaScript]
[Previous page] [Section contents page] [Next page]
Inserting JavaScripts into HTML Files

The Script Tag

JavaScripts are inserted into HTML files by means of the script tag:

	<script language="JavaScript">
	<!--
	[JavaScript code goes here]
	// -->
	</script>
	

Note that the JavaScript code is placed inside an HTML comment, which prevents browsers that do not support JavaScript from rendering the script code as plain text. Also note the // at the beginning of the line containing the closing tag for the HTML comment. This makes the line a JavaScript comment, and prevents Netscape from attempting to interpret --> as part of the script (Internet Explorer seems not to do this, but isn't bothered by the //).

The Language Attribute

The language attribute may be set to JavaScript for all browsers that support JavaScript, or you can specify the following versions:

  • JavaScript 1.1
  • JavaScript 1.2
  • JScript

If you specify the language version, only browsers that support that version will attempt to execute the script. The advantage of this is that you run less risk of script errors resulting from differences between different versions of JavaScript. The disadvantage is that you're preventing older browsers from even attempting to execute the script, which in many cases they could do successfully. But omitting the results of the script may be safer than sending error messages to users of older browsers, particularly if you are working on an official site for a company or other organization. The last word: testing scripts on multiple browsers is a better way to avoid errors than specifying the version, when it's possible.

The SRC Attribute and JS Files

Instead of writing the script code inside the script tag, you can put it in a separate file (with the extension .js) and access that file through the SRC attribute -- for example:

	<script language="JavaScript 1.1" src="check_link.js">
	</script>
	

The separate JS file is an attractive option, especially for scripts that are reused in a number of different files. I offer a caution, though: this method is supported only in Javascript 1.1 or later. Using a separate JS file works in IE 4.0 and Netscape 4.0; it works in IE 3.0 only if the script does not write any text into the HTML document. This method does not work in Opera 3.0.

Placement of Scripts

Technically, scripts can be placed anywhere in an HTML file, but there are some important considerations:

  • If the script contains functions that will be called by a user-initiated event (such as clicking), make sure that the script is placed in the HEAD of the document, so that the script will be read and the functions written to memory before the browser renders the body.
  • If the script writes information to the HTML file, then the script needs to be located at the point where the information should be written.
  • If neither of the previous conditions applies, place the script inside the HEAD tag (or at least before the BODY tag), so the script is read before the user moves to another page, and to minimize the chances that a browser that doesn't support JavaScript will render the script code as text.
[Previous page] [Section contents page] [Next page]