![]() |
|
![]() ![]() ![]() |
|
Inserting JavaScripts into HTML Files
The Script TagJavaScripts are inserted into HTML files by means of the script tag: <script language="JavaScript"> <!-- [JavaScript code goes here] // --> </script> Note that the JavaScript code is placed inside an HTML comment, which prevents browsers that do not support JavaScript from rendering the script code as plain text. Also note the // at the beginning of the line containing the closing tag for the HTML comment. This makes the line a JavaScript comment, and prevents Netscape from attempting to interpret --> as part of the script (Internet Explorer seems not to do this, but isn't bothered by the //). The Language AttributeThe language attribute may be set to JavaScript for all browsers that support JavaScript, or you can specify the following versions:
If you specify the language version, only browsers that support that version will attempt to execute the script. The advantage of this is that you run less risk of script errors resulting from differences between different versions of JavaScript. The disadvantage is that you're preventing older browsers from even attempting to execute the script, which in many cases they could do successfully. But omitting the results of the script may be safer than sending error messages to users of older browsers, particularly if you are working on an official site for a company or other organization. The last word: testing scripts on multiple browsers is a better way to avoid errors than specifying the version, when it's possible. The SRC Attribute and JS FilesInstead of writing the script code inside the script tag, you can put it in a separate file (with the extension .js) and access that file through the SRC attribute -- for example: <script language="JavaScript 1.1" src="check_link.js"> </script> The separate JS file is an attractive option, especially for scripts that are reused in a number of different files. I offer a caution, though: this method is supported only in Javascript 1.1 or later. Using a separate JS file works in IE 4.0 and Netscape 4.0; it works in IE 3.0 only if the script does not write any text into the HTML document. This method does not work in Opera 3.0. Placement of ScriptsTechnically, scripts can be placed anywhere in an HTML file, but there are some important considerations:
|
|
![]() ![]() ![]() |