## Scripts

Scripts are blocks of code that can run when an HTML page is loaded or when an event happens, such as the click of a button. To learn more about scripts and scripting, read the Object Model for Scripting document included in the ActiveX SDK, as well as Microsoft's pages on VBScript and JavaScript. This section describes how to incorporate a script into an HTML document.

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

- Use the SCRIPT element.
- 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 documents. Scripts reside inside the container of a SCRIPT element.

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 as well.

For example, this HTML element 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 and its use with objects

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, such as an ActiveX Control, must appear in the text after the script element in which the object is defined. 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 at any time after the object has been downloaded to your computer.

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 scripting languages on the same page, the scriptlet for Button1 is implemented in VBScript, and that for Button2 in JScript.

    <SCRIPT 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 script'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

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'.

© 1996 Microsoft Corporation