Index Home Up Attaching and Invoking Scripts



There are three ways to attach and invoke scripts in HTML: contain them in the <SCRIPT> tag, use attributes of HTML elements, or use a custom URL type.


Index Home Up Using the SCRIPT Element



Use the SCRIPT element to add scripts to HTML. SCRIPT is a character-like element for embedding script code anywhere in the document HEAD or BODY. The SCRIPT element can be used to reference external scripts, using the SRC attribute, and to include script statements within the HTML document.

HTML documents can include multiple SCRIPT elements that can be placed in the document HEAD or BODY. This allows script statements for a form to be placed near the corresponding FORM element.

Here is a simple example of a page that uses the SCRIPT element:


<SCRIPT language="VBScript">
    '... Additional VBScript statements ... 
</SCRIPT>

The same example in JavaScript would read:


<SCRIPT language="JavaScript">
    //... Additional JavaScript statements ... 
</SCRIPT>


Index Home Up Evaluation of SCRIPT



The SCRIPT element is evaluated when the document is loaded, and all code is executed at load time. For functions like document.write, the order of script elements can affect the output of the document. For example, the page:


<HTML><BODY>
<SCRIPT LANGUAGE="JavaScript">
document.write ("Hello world.")
</SCRIPT>
This is a document.
</BODY></HTML>

results in:


Hello world. This is a document.

While the page:


<HTML><BODY>
This is a document.
<SCRIPT LANGUAGE="VBScript">
document.write ("Hello world.")
</SCRIPT>
</BODY></HTML>

results in:

This is a document. Hello world.

Second, because script statements are evaluated when the document is loaded, attempts to reference objects will fail if these objects are defined by HTML elements that occur later in the document.

The document object's write method can insert not just text but also objects such as buttons (defined using the INPUT tag) and ActiveX controls (defined using the OBJECT tag.) These objects can not be referenced only in a script block following the script block that output them. Future betas of Internet Explorer will provide full Netscape compatibility by allowing objects to referenced as soon as they are output.


Index Home Up Using Scripts as Attributes of HTML Elements



Another way to insert scripts is to add attributes to element tags in HTML. These attributes match with events on the elements, and the "scriptlet" is executed when the event is fired. This method can be used for any HTML intrinsic elements, such as forms, buttons, or links; however, this method does not work for items inserted using the OBJECT tag.

The following example uses this syntax in Button1 to handle the onClick event. To demonstrate the ability to combine multiple scripting languages on the same page, the scriplet for Button1 is implemented in VBScript and that for Button2 in JavaScript.


<form name="Form1">
	<input type="button" name="Button1" value="   Press me   " 			onClick="pressed" language="VBScript">
	<input type="button" name="Button2" value="Press me too!" 	onClick="pressed2()" language="JavaScript">
</form>

<script language="VBSCRIPT">
	sub pressed
		document.Form1.Button1.value="I'm VBS"
		alert "I've been pressed!"
	end sub	
</script>
<script language="JavaScript">
	function pressed2()
	{
		document.Form1.Button2.value="I'm JavaScript"
		alert("Are you impressed?")
	}
</script>

Note the use of the language attribute on the input tag to indicate the scriplet's language. If no language is specified, the scriplet defaults to the language of the most recently encountered script block. If no script block has been encountered, the language defaults to JavaScript.

The elements FORM, INPUT, BODY, and A support this syntax, but with differing events. See the individual tags referenced later in this document.


Index Home Up An Alternative Using SCRIPT



The SCRIPT element can also be used with the FOR="object" EVENT="eventname" syntax. This method can be used for any named elements, and for any elements inserted using the OBJECT tag. The following example is similar to the previous "scriptlet" example, but it uses a different syntax:


<form name="Form1">
	<input type="button" name="Button1" value="Press me">
	<script for="Button1" event="onClick" language="VBScript">
		alert "I've been pressed"
		document.Form1.Button1.value="OUCH"
	</script>
</form>


Index Home Up Using Scripts in URLs



Scripts can be invoked using the A element combined with a custom URL type. This allows a script to be executed when the user clicks on a hyperlink. This URL type is valid in any context, but is most useful when used with the A element. For example:


<A HREF="javascript:alert('hi there')">Click me to see a message.<A>

displays an alert message box that contains the text 'hi there'.

Syntax

script-engine:script-code

Executes the script code using the script engine when the URL is resolved. For example, to execute a script when the user clicks on a hyperlink, use:


<title> JavaScript example </title>
<A HREF=" javascript:alert(document.title)">Click here to see the title of the current document..</A>

Notice that the script is executed in the context of the current page, which means that document.title evaluates to the document containing the script.
Argument Type Description
script-engine String A string that names a scripting engine (must be JavaScript for Beta 1).
script-code String A string that evaluates to a script in the syntax supported by the scripting engine. This script is executed by the scripting engine when the URL is evaluated.

Note This syntax is only supported for JavaScript in the current build of Internet Explorer; in particular, vbscript: will not work in the current build. All scripting engines will be supported in future builds. Also, the JavaScript: syntax is currently supported only from scripts, not when typed into the address bar by users.