### Attaching and Invoking Scripts

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

- Use the SCRIPT tag.
- Use those attributes of HTML elements that support scripts.
- Use a custom URL type.

### 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 which 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 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 element.

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 script 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" ="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 Java 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 elements FORM, INPUT, BODY, and A 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 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>

This displays as: Click me to see a message.

Syntax

script-engine:script-code

Use the script-engine:script-code syntax to 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> 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. "Javascript" is the only acceptable value.
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 JScript; in particular, VBScript: will not work in Internet Explorer; however, you can call VBScript functions with the <A HREF="javascript:FunctionName()"> syntax. This syntax does not support VBScript processing in the URL itself. For example:

    <HTML>
    <SCRIPT language="VBScript">

    SUB CallMe( )
    MsgBox("Called")
    END SUB

    </SCRIPT>

    <BODY>
    This is a <A HREF="JavaScript:CallMe()">link</A>
    &lt/BODY>
    &lt/HTML>

© 1996 Microsoft Corporation