Previous Next

Scripts


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

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 heading 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 JScript would read:


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

Evaluation of SCRIPT

There are three ways to attach and invoke scripts in HTML:

Using the SCRIPT Element

Use the SCRIPT element to add scripts to HTML. SCRIPT is an element for embedding script code in a document. Using SCRIPT, the full source code of a script can be included within the document. The SCRIPT element can be used to point to external scripts, using the SRC attribute.

For example, this HTML describes a page with a SCRIPT element that includes code written in VBScript:


<SCRIPT language="VBScript">
	Document.write("Hello, Webmaster.")    
</SCRIPT>

This displays as:


The example in JScript would read:


<SCRIPT language="JavaScript">
	Document.write("Hello, Webmaster.")    
</SCRIPT>

This displays as:


Evaluation of SCRIPT

The SCRIPT element is evaluated when the document is loaded. All code is executed at load time in the order in which it appears in the document. Therefore, any reference to an object must appear in the text after the script element in which the object is defined.

The document object's write method can insert both text and objects—such as buttons and ActiveX controls. These objects can be referenced only in a script block following the script block that defined them. You will be able to refer to and to copy references to objects that are the result of a code download. You can invoke any method on an object, but only when the object has been downloaded.

Using Scripts as Attributes of HTML Elements

Another way to insert scripts is to use the attributes of HTML elements that support scripts. When these attributes match with events on the elements, the script is executed when the event occurs. This can be done with HTML 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 scriptlet for Button1 is implemented in VBScript, and that for Button2 in JScript.


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

<script language="VBSCRIPT">
	sub pressed
		document.Form1.Button1.value="Pressed"
                alert "Pressed the VBScript button"
	end sub	
</script>
<script language="JavaScript">
	function pressed2()
	{
                document.Form1.Button2.value="Pressed"
		alert("Pressed the JScript button.")
	}
</script>

This displays as:


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

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

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 script example, but it uses a different syntax:


<form name="Form1">
        <input type="button" name="Button1" value="Click">
	<script for="Button1" event="onClick" language="VBScript">
		alert "Button has been pressed"
                document.Form1.Button1.value="PRESSED"
	</script>
</form>

This displays as:


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 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 a hyperlink, use:


<title> JScript 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 written as "Javascript").
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.

Previous Next

© 1996 Microsoft Corporation