Book Section
-----
TY JavaScript 3rd Ed.
Teach Yourself JS 1.5
Teach Yourself DHTML
Teach Yourself JS 1.3
LLWW: JavaScript

General Section
-----
Discussion Forum
Articles / Tips
JavaScript Links
About the Author
Privacy Policy
Contact Me



Other Sites
-----
Website Workshop
JavaScript Weblog

Article: Using the <SCRIPT SRC> Tag

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 and later or IE 4.0 and later. 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:
  • Don't include HTML in the file; only JavaScript commands should be included.
  • Do not include <SCRIPT> tags or HTML comments.
  • Save the file with the .js extension. It should be placed on the server with the same rights as the HTML files.

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">
</SCRIPT>
If your script simply defines functions, you can place these tags in the <HEAD> section of the document; otherwise place it where its output should go. The closing </SCRIPT> tag is required, and you should not use JavaScript commands between the tags.

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:
  • If you use several different files, you'll need to keep track of which one contains which code.
  • Not all servers support this feature; if you move your documents to a new server you may run into trouble.
  • This will slow down access of your pages, since the browser has to request a separate file. If your network connection is particularly slow, you should not use this method.
(c) 1997-2002 Starling Technologies and Michael Moncur. Portions (c) Sams Publishing.