JavaScript: The Definitive GuideJavaScript: The Definitive GuideSearch this book

14.9. Applets

The applets[] array of the Document object contains objects that represent the applets embedded in the document with the <applet> or <object> tag. An applet is a portable, secure Java program that is loaded over the Internet and executed by the web browser; both Netscape and Internet Explorer support Java (although IE 6 no longer includes Java support by default).

As of Netscape 3 and Internet Explorer 3, both browsers allow JavaScript to invoke public methods and read and write the public properties of Java applets. (As we'll see in Chapter 22, Netscape also supports much richer bidirectional interactions between JavaScript and Java.) All applets have a few standard public methods that they inherit from their superclasses, but the most interesting methods and properties vary on a case-by-case basis. If you are the author of the applet that you want to control from JavaScript, you already know what public methods and properties it defines. If you are not the author, you should consult the applet's documentation to determine what you can do with it.

Here's how you might embed a Java applet in a web page with the <applet> tag and then invoke the start( ) and stop( ) methods of that applet from JavaScript event handlers:

<applet name="animation" code="Animation.class" width="500" height="200">
</applet>
<form>
<input type="button" value="Start" onclick="document.animation.start( );">
<input type="button" value="Stop" onclick="document.animation.stop( );">
</form>

All applets define start( ) and stop( ) methods. In this hypothetical example, the methods cause an animation to start and stop; by defining the HTML form, we've given the user control over starting and stopping the applet. Note that we've used the name attribute of the <applet> tag, so we can refer to the applet by name, rather than as a numbered element of the applets[] array.

This example does not fully demonstrate the power of JavaScript to script Java applets: the Java methods invoked from the JavaScript event handlers are passed no arguments and return no values. In fact, JavaScript can pass numbers, strings, and boolean values as arguments to Java methods and can accept numbers, strings, and boolean return values from those functions. (As we'll see in Chapter 22, Netscape can also pass and return JavaScript and Java objects to and from Java methods.) The automatic conversion of data between JavaScript and Java allows for rich interactions between the two programming environments. For example, an applet might implement a method that returns a string of JavaScript code. JavaScript could then use the eval( ) method to evaluate that code.

Applets can also implement methods that don't operate on the applet itself, but instead simply serve as conduits between JavaScript and the Java environment. For instance, an applet might define a method that invokes the System.getProperty( ) method for a given string argument. This applet would allow JavaScript to look up the value of Java system properties and determine, for example, the version of Java that is supported by the browser.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.