The <script> TagTo accommodate the inclusion of JavaScript programs in a normal HTML document, Netscape introduced the <script> tag. By placing a <script> tag in a document, you tell the web browser to treat any lines of text following the tag as script rather than as content for the web page. This continues until a corresponding </script> tag is encountered, at which point the browser reverts to treating text as web content. When used in a document, every script tag must include a language attribute to declare the scripting language to be used. If you're writing a script in JavaScript, you should use the attribute language="JavaScript". Note JavaScript has now appeared in five versions of Netscape Navigator and two versions of Microsoft Internet Explorer, plus Mozilla Firefox and Opera. This means that there are now several possible values for the language attribute. With Navigator 3, Netscape extended JavaScript to JavaScript 1.1. Netscape Navigator 4.0 added even more to JavaScript and called it JavaScript 1.2. Navigator 4.5 introduced JavaScript 1.3. Even though many browsers have been released since Netscape Navigator 4.5, the version number for JavaScript remains the same. The Structure of a JavaScript ScriptWhen you include any JavaScript code in an HTML document (apart from using the <script> tag), you should also follow a few other conventions:
Taking these three points into consideration, the basic structure for including JavaScript code inside an HTML document looks like this: <html> <head> <title>Test script</title> <script language="JavaScript"> // Your JavaScript code goes here </script> </head> <body> Your Web document goes here </body> </html> The src AttributeBesides the language attribute, the <script> tag can also include an src attribute, which allows a JavaScript script stored in a separate file to be included as part of the current web page. This option is handy if you have several web pages that use the same JavaScript code and you don't want to copy and paste the scripts into each page's code. When used this way, the <script> tag takes the following form: <script language="JavaScript" src="/books/2/631/1/html/2/http://www.myserver.com/script.js"> In this form, script can be any relative or absolute URL, and .js is the file extension for a JavaScript file. |