There are three ways to attach and invoke scripts in HTML:
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 JavaScript would read:
<SCRIPT language="JavaScript"> //... Additional JavaScript statements ... </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&151;but only when the object has been downloaded.
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 is 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 scriplet for Button1 is implemented in VBScript and that for Button2 in JavaScript.
<form name="Form1"> <input type="button" name="Button1" value="VB" onClick="pressed" language="VBScript"> <input type="button" name="Button2" value="Java" onClick="pressed2()" language="JavaScript"> </form> <script language="VBSCRIPT"> sub pressed document.Form1.Button1.value="VBScript" alert "Pressed the VBScript button" end sub </script> <script language="JavaScript"> function pressed2() { document.Form1.Button2.value="JavaScript" alert("Pressed the Java button.") } </script>
This displays as:
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.
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 presed" document.Form1.Button1.value="PRESSED" </script> </form>
This displays as:
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.