JavaScript: The Definitive Guide

Previous Chapter 14
Documents and Their Contents
Next
 

14.4 The JavaObject Object

The JavaObject object is a JavaScript object that serves as a wrapper around Java objects. It allows JavaScript programs to read and write the public fields of a Java object, and also to invoke the public methods of a Java object. Chapter 19, LiveConnect: JavaScript and Java, covers the "LiveConnect" mechanism for communication between Java and JavaScript, and will explain JavaObjects in detail, as well as the JavaArray, JavaClass, and JavaPackage objects.

The reason that JavaObjects are being discussed here is that in Navigator 3.0, the applets[] and embeds[] properties of the Document object are arrays that contain JavaObjects. Elements of the applets[] array are created when a Java applet is included in the document with the <APPLET> tag. Each JavaObject in this array represents the Java Applet object. Similarly, elements of the embeds[] array are created when embedded data are included in the document with the <EMBED> tag. In Navigator, the <EMBED> tag specifies data to be displayed through a plug-in, and the JavaObject objects in the embeds[] array are Java objects provided by the plug-in that allow it to be controlled through a Java-based interface. If a plug-in does not support Java--and many plug-ins currently do not--then the entry in the embeds[] array will be a dummy object with no functionality.

Both the <APPLET> and the <EMBED> tags have optional NAME attributes. If you specify a name for either of these tags, then a property with that specified name will be created in the Document object. The value of this property will be a reference to the JavaObject for the applet or embedded data. Using the NAME attribute in HTML can make your JavaScript code more readable--you can use expressions like document.myapp instead of document.applets[0].

Example 14.5 shows 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.

Example 14.5: Invoking Methods of a Java Applet from JavaScript

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

The topic of interacting with Java from JavaScript is a broad one, and deserves a chapter on its own. We'll learn more about the JavaObject object and the applets[] and embeds[] arrays in Chapter 19, LiveConnect: JavaScript and Java.


Previous Home Next
The Anchor Object Book Index Saving State with Cookies

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell
Hosted by uCoz