• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

1.8. JavaScript in HTML

Client-side JavaScript code may be embedded in HTML files in several ways:

<SCRIPT> tag

Most JavaScript code appears in HTML files between a <SCRIPT> tag and a </SCRIPT> tag. The <SCRIPT> tag can also be used to include an external file of JavaScript code into an HTML document. The <SCRIPT> tag supports a number of attributes, including these three important ones:


Specifies the scripting language in which the script is written. In most browsers, this attribute defaults to "JavaScript". You must set it if you are mixing scripting languages, such as JavaScript and VBScript.

Set this attribute to "JavaScript1.1" to specify that the code uses JavaScript 1.1 features, and that it should not be interpreted by JavaScript 1.0 browsers. Set this attribute to "JavaScript1.2" to specify that only JavaScript 1.2 browsers should interpret the code. (Note, however, that Navigator 4 has some non-standard behaviors when "JavaScript1.2" is specified.)


Specifies the URL of an external script to be loaded and executed. Files of JavaScript code typically have a .js extension. Note that the </SCRIPT> tag is still required when this attribute is used. Supported in JavaScript 1.1 and later.


Specifies the URL of a JAR file that contains the script specified by the SRC attribute. Supported in JavaScript 1.2 and later. Archives are required to use Navigator 4 signed scripts.

Event handlers

JavaScript code may also appear as the value of event handler attributes of HTML tags. Event handler attributes always begin with "on". The code specified by one of these attributes is executed when the named event occurs. For example, the following HTML specifies a button that displays a dialog box when clicked:

<INPUT TYPE=button VALUE="Press Me"
     onClick="alert('hello world!');">

JavaScript URLs

JavaScript code may appear in a URL that uses the special javascript: pseudo-protocol. The JavaScript code is evaluated, and the resulting value (converted to a string, if necessary) is used as the contents of the URL. Use the void operator if you want a JavaScript URL that executes JavaScript statements without overwriting the current document:

<FORM ACTION="javascript:void validate()" >

JavaScript entities

In JavaScript 1.1, HTML attribute values may contain JavaScript code in the form of JavaScript entities. An HTML entity is a string like &lt; that represents some other character or string. A JavaScript entity is JavaScript code contained within &{ and };. Its value is the value of the JavaScript expression within:

<BODY BGCOLOR="&{getFavoriteColor()};" >



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint