[Contents] [Previous] [Next] [Alphabetical Tag List] [Index]

HTML Tag Reference

Scripts

This section discusses the tags for defining scripts in a document. Scripts allow you to include JavaScript code in your HTML documents.


SCRIPT

(client-side JavaScript code)

The SCRIPT tag specifies client-side JavaScript code, which means JavaScript code that runs directly in the browser. The browser treats everything between the <SCRIPT> and </SCRIPT> tags as script elements. For example, the browser interprets text within angle brackets as a script element, not as an HTML element.

For information about JavaScript, see the JavaScript Guide or the JavaScript Reference.

You can include <SCRIPT> tags anywhere in your HTML document. It is a good idea to define functions in a SCRIPT tag in the header portion of your document, since then the functions will be available to the document as it is displayed. You can also use SCRIPT tags in the body of your document to define JavaScript code to be executed in that part of the document.

You can use a NOSCRIPT tag to provide content that is displayed by browsers for which JavaScript is not available, while being ignored by browsers that can understand JavaScript. For example, you could use the NOSCRIPT tag to provide a warning that the page requires JavaScript.

Syntax

<SCRIPT
  LANGUAGE="
languageName"
  SRC="
location"               
>
...
</SCRIPT>

LANGUAGE="languageName"

specifies the program language. If the LANGUAGE attribute is not specified, the default value is JavaScript.

You can specify JavaScript 1.1 for scripts to be executed by the 1.1 version of JavaScript (compatible with Navigator 3.0) or JavaScript 1.0 for scripts to be executed by the 1.0 version. Navigator 3.0

SRC="location"

specifies the URL of the script, if you want to load a script from a separate file. The suffix on a location specifies the scripting language. The suffix .js indicates a JavaScript file. The web server maps the suffix to the MIME time. For JavaScript, the MIME type is "application/x-javascript". Navigator 3.0

Script Example 1. Using the SCRIPT Tag

The following example uses the SCRIPT tag to define a JavaScript script in the HEAD tag. The script is loaded before anything else in the document is loaded. The JavaScript code in this example defines a function, changeBGColor(), that changes the document's background color.

The body of the document contains a form with two buttons. Each button invokes the changeBGColor() function to change the background of the document to a different color.

<HTML>
<HEAD><TITLE>Script Example</TITLE>
</HEAD>
<SCRIPT language="JavaScript">
  function changeBGColor (newcolor) {
    document.bgColor=newcolor;
    return false;
  }
</SCRIPT>
<BODY >
<P>Select a background color:</P>
<FORM>
  <INPUT TYPE="button" VALUE=blue onClick="changeBGColor('blue');">
  <INPUT TYPE="button" VALUE=green onClick="changeBGColor('green');">
</FORM>
<NOSCRIPT><I>Your browser is not JavaScript-enabled.
These buttons will not work.</I>
</NOSCRIPT>
The file script.htm shows this example in action in a separate window.

Example 2. Using SCRIPT tags in the Document Body

This example uses a SCRIPT tag to use JavaScript code to dynamically decide what to write depending on which browser the user is using. The JavaScript code determines the user's browser type and version and writes content depending on the result. The document contains more HTML content following the SCRIPT tag.

This example uses properties and methods on the navigator and document objects. It uses the write() method of the document object to dynamically write content to the web page. For details of the navigator and document objects, see Chapter 3. Using Navigator Objects in the JavaScript Guide .

<H1>Welcome to The Script Example Page</H1>
<!-- run some JavaScript code now -->
<SCRIPT language="javascript">
  var navVersion = navigator.appVersion;
  var navName = navigator.appName;
  document.open();
  document.write("<P>You are using a " + navName +
    " web browser, version " + navVersion + "</P>");
  if (navName=="Netscape") {
    document.write("<H3>Thank you for using Netscape's Navigator to
    browse the web. </H3>");
  }
  else {
    document.write("<H3>Have you tried using <I>Netscape Communicator
    </I> to browse the web?</H3>");
  }
  document.close();
</SCRIPT>
<P>More HTML content goes here...</P>
The file script2.htm shows this example in action in a separate window.

Example 3: Using the SRC attribute

The following example uses the SRC attribute to specify the location of a file containing JavaScript code:

<SCRIPT LANGUAGE="JavaScript"
    SRC="http://www.mycompany.com/scripts/myScript.js">
</SCRIPT>

NOSCRIPT

(alternative text for JavaScript)

The NOSCRIPT tag specifies the content for a browser to display when JavaScript is not available or enabled. Navigator 3.0.

Syntax

<NOSCRIPT>...</NOSCRIPT>

Example

See Script Example 1. Using the SCRIPT Tag.


SERVER

(server-side script)

The SERVER tag specifies server-side JavaScript statements that are used in a JavaScript application on the server. (Contrast this with theSCRIPT tag, which specifies client-side JavaScript statements that run in the browser.) When a script is specified within the SERVER tag, it is run on the server before the page is passed to the browser. See Writing Server-Side JavaScript Applications for more information.

Syntax

<SERVER>...</SERVER>

Example

<P>Your IP address is <SERVER>write(request.ip);</SERVER>


[Contents] [Previous] [Next] [Alphabetical Tag List] [Index]

Last Updated: 01/26/98 21:33:44


Copyright 1998 Netscape Communications Corporation



Banner.Novgorod.Ru