The script Tag






The <script> Tag

To 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 Script

When you include any JavaScript code in an HTML document (apart from using the <script> tag), you should also follow a few other conventions:

  • As a rule, the <script> tag should be placed between the <head> and </head> tags at the start of your document, not between the <body> tags. This isn't a hard-andfast requirement (as you'll learn later), but it's a standard that you should adopt whenever possible. Because the code for your scripts won't be displayed on the web page itself, it should be included in the <head> section with all the other control and information tags, such as <title> and <meta>.

  • Unlike HTML, which uses the <!-- comment tag -->, comments inside JavaScript code use the // symbol at the start of a line. Any line of JavaScript code that starts with this symbol will be treated as a comment and ignored.

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 Attribute

Besides 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="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.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows