JavaScript and Embedded Objects - Accessing Applets in JavaScript (Page 4 of 15 )
The ability to communicate with applets originated with a Netscape technology called LiveConnect that was built into Netscape 3. This technology allows JavaScript, Java, and plug-ins to interact in a coherent manner and automatically handles type conversion of data to a form appropriate to each. Microsoft implemented the same capabilities in IE4, though not under the name LiveConnect. The low-level details of how embedded objects and JavaScript interact are complicated, unique to each browser, and even vary between different versions of the same browser. The important thing is that no matter what it is called, the capability exists in versions of IE4+ (except under Macintosh) and Netscape 3+ (although early versions of Netscape 6 have some problems), and Mozilla-based browsers.
Applets can be accessed through the applets[] array of the Document object or directly through Document using the applet’s name. Consider the following HTML:
<applet code="myhelloworld.class" width="400" height="100"
name="myhelloworld" id="myhelloworld">
<em>Your browser does not support Java!</em>
</applet>
Assuming that this applet is the first to be defined in the document, it can be accessed in all of the following ways, with the last being preferred:
document.applets[0]
// or
document.applets["myhelloworld"]
// or the preferred access method
document.myhelloworld
The JavaScript properties, defined primarily under the browser object model and later by the DOM, of an Applet object are listed in Appendix B and consist of an unsurprising assortment of information reflecting the attributes of the (X)HTML <applet> tag for which it was defined. The relevant aspect to this JavaScript-Java communication discussion is the fact that all properties and methods of the applet’s class that are declared public are also available through the Applet object. Consider the following Java class definition for the previous myhelloworld example. The output (when embedded as before) is shown in Figure 18-1.
import java.applet.Applet;
import java.awt.Graphics;
public class myhelloworld extends Applet
{
String message;
public void init()
{
message = new String("Hello browser world from Java!");
}
public void paint(Graphics myScreen)
{
myScreen.drawString(message, 25, 25);
}
public void setMessage(String newMessage)
{
message = newMessage;
repaint();
}
}

FIGURE 18-1 The output of the myhelloworld applet in Internet Explorer
Now comes the interesting part. Because the setMessage() method of the myhelloworld class is declared public, it is made available in the appropriate Applet object. We can invoke it in JavaScript as
document.myhelloworld.setMessage("Wow. Check out this new message!");
Before proceeding further with this example, it is very important to note that applets often require a significant amount of load time. Not only must the browser download the required code, but it also has to start the Java virtual machine and walk the applet through several initialization phases in preparation for execution. It is for this reason that it is never a good idea to access an applet with JavaScript before making sure that it has begun execution. The best approach is to use an onload handler for the Document object to indicate that the applet has loaded. Because this handler fires only when the document has completed loading, you can use it to set a flag indicating that the applet is ready for interaction. This technique is illustrated in the following example using the previously defined myhelloworld applet:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Applet Interaction Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<script type="text/javascript">
<!--
var appletReady = false;
function changeMessage(newMessage) {
if (!navigator.javaEnabled()) {
alert("Sorry! Java isn't enabled!");
return;
}
if (appletReady)
document.myhelloworld.setMessage(newMessage);
else
alert("Sorry! The applet hasn't finished loading");
}
// -->
</script>
<body onload="appletReady = true;">
<applet code="myhelloworld.class" width="400" height="100"
name="myhelloworld" id="myhelloworld">
<em>Your browser does not support Java!</em>
</applet>
<form action="#" method="get" onsubmit="return false;"
name="inputForm" id="inputForm">
<input type="text" name="message" id="message" />
<input type="button" value="Change Message"
onclick="changeMessage
(document.inputForm.message.value);" />
</form>
</body>
</html>
The output of this script after changing the message is shown in Figure 18-2. There are tremendous possibilities with this capability. If class instance variables are declared public, they can be set or retrieved as you would expect:
document.appletName.variableName
Inherited variables are, of course, also available.
Note Java applets associated with applets defined in <object> tags receive the public properties and methods just as those defined in <applet> tags do. However, using <object> instead of <applet> is potentially less cross-browser compatible because Netscape 4 does not expose this HTML element to scripts.

FIGURE 18-2 JavaScript can call public methods of Java applets.
This chapter is from JavaScript: The Complete Reference, second edition, by Thomas Powell and Fritz Schneider, McGraw-Hill/Osborne, ISBN: 0072253576). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Issues with JavaScript-Driven Applets >>
More JavaScript Articles
More By McGraw-Hill/Osborne