Scripts

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

JavaScript Sample:

<SCRIPT language="JavaScript">
document.write("Hello, World.")
</SCRIPT>

This displays as:


VBScript Sample:

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

This displays as:


Using Scripts during Events

One 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="Button2" VALUE="JavaScript" onClick="DoBtn1()" LANGUAGE="JavaScript">
<INPUT TYPE="button" NAME="Button1" VALUE="VBScript" onClick="DoBtn2" LANGUAGE="VBScript">
</FORM>

<SCRIPT LANGUAGE="JavaScript">
function DoBtn1()
{
document.Form1.Button2.value="Pressed"
alert("Pressed the JavaScript button.")
}
</SCRIPT>

<SCRIPT LANGUAGE="VBSCRIPT">
Sub DoBtn2
document.Form1.Button1.value="Pressed"
alert "Pressed the VBScript button"
End Sub
</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.



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('Hello World.')">Click me to say hello.</A>

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


Events

Following is a list of commonly used events that that might occur while a page is being browsed. Note that this is not a complete list. For more information on events, please see the complete references for either or both of the scripting languages.

OnBlur
Occurs when an object or element loses focus
<input type="input" size="20" onBlur="alert('Lost Focus')">
 
OnLoad
Occurs when a page is loaded into a browser
<body bgcolor="#ffffff" onLoad="DoMyFunction()">
 
OnMouseOut
Occurs when mouse pointer moves out of an object or element
<input type="button" value="Button1" onMouseOut="window.status=('Moved Out')">
 
OnMouseOver
Occurs when mouse pointer moves over or across an object or element
<input type="button" value="Button1" onMouseOver="window.status=('Moving Over')">