- Tips and Tricks -

Using the <SCRIPT SRC> Tag

September 20, 1996

You can use the <SCRIPT SRC> feature to keep your JavaScript program in a separate file rather than in the HTML file. This is convenient in that it keeps the HTML file small, and prevents non-JavaScript browsers from mistakenly displaying the JavaScript source.

NOTE: This feature works only in Netscape 3.0 (beta or final version). Netscape 2.0 and MSIE 3.0 do not support this feature.

Creating the JavaScript File

To take advantage of this feature, create a separate file with your JavaScript program. If you wish, you can use scripts both inside the HTML file and in external files. JavaScript files are simple text files with the .js extension; you can create them with any text editor. Follow these guidelines when creating the file:

Modifying the HTML File

Once you have a JavaScript file, you simply need to refer to it in your HTML document. Use this tag:
<SCRIPT LANGUAGE="JavaScript" SRC="filename">
If your script simply defines functions, you can place it in the <HEAD> section of the document; otherwise place it where its output should go. The closing </SCRIPT> tag is not required.

Configuring the Server

There's one catch - you can't use this feature unless your server supports it. Luckily, it's not hard to configure most servers to support JavaScript files. If you're using a typical UNIX server, you can simply create a file called .htaccess in the same directory as your web pages. Only one line is required in the file:
AddType application/x-javascript .js
This adds JavaScript files and their MIME type to the server's configuration for this directory. If you already have a .htaccess file, add this line to the existing file.

NOTE: These instructions may differ for your server. If the above does not work, or if you are unable to create the .htaccess file, contact your system administrator; they should be able to add the MIME type for you. Tell them the extension is .js and the MIME type is "application/x-javascript."

Disadvantages

This technique is not without its disadvantages:
michael moncur ... [email protected]