|
||||
|
Book Section
Other Sites Website Workshop JavaScript Weblog |
Article: Using the <SCRIPT SRC> TagUsing the <SCRIPT SRC> TagSeptember 20, 1996You 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 FileTo 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 FileOnce 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 ServerThere'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 .jsThis 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." DisadvantagesThis technique is not without its disadvantages:
|
|||