[Chapter 10] 10.2 Including JavaScript Files
Author: David Flanagan
Date: Fri Aug 29 10:57:11 1997

#

| Chapter 10 Client-Side Program Structure |

## 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:

- It simplifies your HTML files by allowing you to remove large blocks of JavaScript code from them.

- When you have functions or other JavaScript code used by several different HTML files, you can keep it in a single file and read it into each HTML file that needs it. This reduces disk usage, and makes code maintenance much easier.

- When JavaScript functions are used by more than one page, placing them in a separate JavaScript file allows them to be cached by the browser, making them load much more quickly. When JavaScript code is shared by multiple pages, the time savings of caching more than outweigh the small delay required for the browser to open a separate network connection to download the JavaScript file the first time it is requested.

- Because the `SRC` attribute takes an arbitrary URL as its value, a JavaScript program or web page from one web server can employ code (such as subroutine libraries) exported by other web servers.

The <SCRIPT> Tag |  | JavaScript and Events