JavaScript to Java Communication (Scripting)



This section includes the following topics:

How to Script Applets

This chapter requires understanding the specific HTML tags required for Java Plug-in. For information about these tags, refer to Using OBJECT, EMBED, and APPLET Tags in Java Plug-in. You also should be familiar with JavaScript.

With scripting you have the capability to call applet methods from within an HTML page. In addition to invoking methods, you can use scripts to:

Using Scripts to Invoke a Method

You often want to use scripts to invoke methods on an applet. For example, you might have an HTML button that, when clicked, starts an animation sequence. You do this through a combination of HTML tags and scripting in the HTML file, plus the actual code in the applet itself.

You need to include the following in your applet's HTML page:

These tags are explained in the following sections.

Specify the Applet

You must designate an ID parameter within the OBJECT tag for your HTML page. Recall that the OBJECT tag includes such parameters as classid, width, height, and so on.

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>

Associate the Action to the Script

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 (label).

To do this you want to add two tags. One tag indicates that on a certain action, such as onclick, 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="startJSFractal" language="JavaScript">

This INPUT tag creates a button, names the button "Button1", and gives it the value "Start" (the label that appears on the button). 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.

The Script Tag and Method

You must include a SCRIPT tag for the method (function) that the onClick tag specifies. The SCRIPT tag must have the same name as the name used in the onClick tag. It also has a parameter that specifies the script language. More importantly, the script method calls the Java applet method. It identifies the method by using the name of the applet as specified by the ID tag, followed by the actual method name as implemented in the applet code.

For example, the same HTML page might have the following SCRIPT tag:

<SCRIPT language="JavaScript">
function startJSFractal() {
        document.Form1.Fractal.startFractal()
}
</SCRIPT>

In this example, the 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 document form name , then the applet name (OBJECT ID), then the method name itself, as follows:

document.Form1.Fractal.startFractal()