JavaScript: The Definitive Guide

Previous Chapter 10
Client-Side Program Structure
Next
 

10.2 Including JavaScript Files

In Navigator 3.0 and Internet Explorer 3.0, the <SCRIPT> tag supports a new SRC attribute. The value of this attribute specifies the URL of a file of JavaScript code. It is used like this:

<SCRIPT src="../../javascript/util.js"></script>

A JavaScript file is just that--pure JavaScript, without <SCRIPT> tags or any other HTML. A JavaScript file typically has a .js extension, and should be exported by a web server with MIME-type "application/x-javascript". This last point is important, and may require special configuration of your web server in order to successfully use JavaScript files in this way.

The behavior of the <SCRIPT> tag with the SRC attribute specified is exactly as if the contents of the specified JavaScript file appeared directly between the <SCRIPT> and </SCRIPT> tags. Any code that does appear between the open and close <SCRIPT> tags will be ignored by browsers that support the SRC attribute (although it would still be executed by browsers, like Navigator 2.0, that do not recognize the tag). Note that the closing </SCRIPT> tag is required even when the SRC attribute is specified and there is no JavaScript between the <SCRIPT> and </SCRIPT> tags.

Since both Navigator 3.0 and Internet Explorer 3.0 both support the SRC attribute, you cannot assume that any browser that understands the SRC tag also understands JavaScript 1.1. Thus it is a good idea to use the LANGUAGE attribute with the SRC attribute:

<SCRIPT LANGUAGE="JavaScript1.1" src="../../javascript/util.js"></script>
Note that the web server that exports the included file also specifies the scripting language that the file contains (although perhaps not the version of the language) by specifying a MIME type for the file.

There are a number of advantages to using the SRC tag:


Previous Home Next
The <SCRIPT> Tag Book Index JavaScript and Events

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell
This HTML Help has been published using the chm2web software.