This section includes the following topics:
A script, represented by a special script tag within the HTML document, invokes methods that have been implemented in the applet's Java program. Using scripting, you have the capability to call applet methods from within the HTML page.
In addition to invoking a method on an applet, you can also use scripts to:
You need to include the following in your applet's HTML page:
These tags are explained in the following sections.
The ID parameter is the symbolic name of the applet. Once you establish a symbolic name for an applet through the ID parameter, you can reuse this name later in the scripts to refer to this applet.
For example, suppose you have an applet called Fractal. You add the ID parameter to the OBJECT tag and set ID to the symbolic name of the applet. You might set the tag as follows:
ID="Fractal"Now, you can use the name Fractal within scripts to refer to the Fractal applet.
Using the same Fractal applet example, your HTML page would begin with a FORM tag, followed by an OBJECT tag, that together might look as follows:
<form name="Form1"> <OBJECT ID="Fractal" WIDTH=500 HEIGHT=120 CLASSID="CLSID:8AD9C840-044E-11d1-B3E9-00805F499D93" <PARAM NAME="code" value="CLSFractal.class"> <PARAM NAME="codebase" value="1.0.2"> <PARAM NAME="level" value="5"> ... </OBJECT> |
The HTML page defines components that are intended to invoke actions triggered by the user. You use the INPUT tag to define these components. You specify the TYPE of the component, such as button, its NAME, and VALUE. To have the button or other component actually invoke the intended action, you need to add tags that specify:
For example, suppose your HTML page creates a button that, when clicked, starts a particular animation sequence. Your HTML tag creates the button and gives that button a name and a value.
To do this you want to add two tags. One tag indicates that on a certain action, such as click, a corresponding script method should be called. You might have the tag onClick="method name". The method name is a script method within the same HTML page.
Thus, you might have the following in your HTML page:
<input type="button" name="Button1" value="Start" onClick="startVBFractal" language="JavaScript"> |
This INPUT tag creates a button, names the button "Button1", and gives it the value "Start". It also specifies the scripting method that will be called when a user clicks the button, and the scripting method's language. In this example, the scripting method is startJSFractal, and the scripting language is JavaScript. When the user clicks this button, the HTML page branches to the script method startJSFractal, which is written in JavaScriptscript.
For example, the same HTML page might have the following SCRIPT tag:
<SCRIPT language="JavaScript"> function startJSFractal() { document.Form1.Fractal.startFractal() } </SCRIPT> |
This example SCRIPT tag begins by specifying that the scripting language
is JavaScript. This is followed by the JavaScript function
statement,
which starts the definition of a scripting method. The function
statement supplies a label or name for the scripting method, calling it startJSFractal.
This name must match the method name given for the input component's action
parameter.
For this example, both the onClick parameter and the function statement specify the identical scripting method. The scripting method startJSFractal merely calls the actual method, startFractal, implemented in the applet code. It qualifies the method name by using the form name, then the applet name, then the method name itself, as follows:
document.Form1.Fractal.startFractal()