How Java to Javascript Communication Works in Java Plug-in
Introduction
Java applets may need to perform Java to JavaScript communication to access the Document Object Model (DOM) or to call JavaScript functions on an HTML page. Internet Explorer and Navigator allow communication between Java to JavaScript through the Java wrapper class netscape.javascript.JSObject. For more details, please see this document on Java packages for LiveConnect. Each JSObject encapulates an entity in the DOM in the JavaScript world.
Because of differences in DOM implementations between browsers, Java Plug-in 1.3 provides different degrees of support for JSObject in Internet Explorer and Navigator. This document specifies how JSObject support works in different browser environments.
How JSObject works
Java Plug-in supports Internet Explorer and Netscape Navigator on various Win32 platforms and Solaris platforms. JSObject provides an easy way to access the DOM of a HTML page. Because different browsers implement DOM differently, using JSObject in a Java applet may yield different behaviors in Java Plug-in, depending on the underlying DOM implementation in the browser. For more details about the DOM implementation in your browser, please consult the development guide for your browser.
In general, applets access JSObject as follows:
import netscape.javascript.*; import java.applet.*; import java.awt.*; class MyApplet extends Applet { public void init() { JSObject win = JSObject.getWindow(this); JSObject doc = (JSObject) win.getMember("document"); JSObject loc = (JSObject) doc.getMember("location"); String s = (String) loc.getMember("href"); // document.location.href win.call("f", null); // Call f() in HTML page } } |
Everything in JSObject starts with the static method
public static JSObject getWindow(Applet a)
which returns a JSObject representing the Window object in the JavaScript for the window containing the given applet. Since this method takes only java.awt.Applet as parameter, JSObject can be accessed from an applet, but not from a bean unless the bean is also an applet.
Once this Window JSObject is obtained, the applet can navigate the DOM of the HTML page by using the following methods:
- public Object call(String methodName, Object args[])
- public Object eval(String s)
- public Object getMember(String name)
- public Object getSlot(int index)
- public void removeMember(String name)
- public void setMember(String name, Object value)
- public void setSlot(int index, Object value)
- public String toString()
We recommend using only getWindow(), call(), eval(), setMember() and getMember() in Java Plug-in. The implementation of getSlot(), setSlot(), removeMember() and toString() is browser-dependent, so the result of the execution may vary depending on the version and platform of the browser in which Java Plug-in is running.
For more information about using JSObject, please read this document on LiveConnect
To compile Java code to take advantage of JSObject, you must have the package netscape.javascript in the CLASSPATH. Currently, Java Plug-in 1.3 ships netscape.javascript in a JAR file called JAWS.JAR. To compile an applet which uses JSObject, please add JAWS.JAR in the CLASSPATH before compilation.
Notice that although JSObject is supported in Java Plug-in 1.3, it is not supported in AppletViewer in the Java 2 platform, Standard Edition v 1.3. As a result, applets using JSObject may not run in AppletViewer, or result in exceptions.
Degree of JSObject support in Java Plug-in
Internet Explorer: Java Plug-in provides full support of JSObject in IE 3/4 by accessing the DOM through COM.
Netscape Navigator: Java Plug-in provides limited support of JSObject in Navigator 3/4 by accessing the DOM through Netscape's Plug-in API. Currently, in Navigator 3, the following JavaScript objects can be accessed through JSObject:
- Anchor
- Document
- Element
- Form
- Frame
- History
- Image
- Link
- Location
- Navigator
- Option
- URL
- Window
In Navigator 4, all the JavaScript objects mentioned above are supported. In addition, it supports the following:
- Layer
- UIBar
All other JavaScript objects not mentioned above are not supported, and accessing them through JSObject will result in exceptions in Java.
Notice that even though different browsers may support the same JavaScript object, the methods and properties the JavaScript object support may be different. Please check the JavaScript developer guide of your browser for more details.
Enabling JSObject support in Java Plug-in
Due to security reasons, JSObject support is not enabled in Java Plug-in by default. To enable JSObject support in Java Plug-in, a new attribute called MAYSCRIPT needs to be present in the EMBED/OBJECT tag as follows:
Original APPLET tag:
<APPLET code="XYZApp.class" codebase="html/" align="baseline" width="200" height="200" MAYSCRIPT> <PARAM NAME="model" VALUE="models/HyaluronicAcid.xyz"> No JDK 1.3 support for APPLET!! </APPLET> |
New OBJECT tag:
<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" width="200" height="200" align="baseline" codebase="http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,0,0"> <PARAM NAME="code" VALUE="XYZApp.class"> <PARAM NAME="codebase" VALUE="html/"> <PARAM NAME="type" VALUE="application/x-java-applet;version=1.3"> <PARAM NAME="MAYSCRIPT" VALUE="true"> <PARAM NAME="model" VALUE="models/HyaluronicAcid.xyz"> No JDK 1.3 support for APPLET!! </OBJECT> |
New EMBED tag:
<EMBED type="application/x-java-applet;version=1.3" width="200" height="200" align="baseline" code="XYZApp.class" codebase="html/" model="models/HyaluronicAcid.xyz" MAYSCRIPT=true pluginspage="http://java.sun.com/products/plugin/1.3/plugin-install.html"> <NOEMBED> No JDK 1.3 support for APPLET!! </NOEMBED> </EMBED> |
If MAYSCRIPT is specified as false, or if MAYSCRIPT is absent, JSObject is disabled. For more information about the MAYSCRIPT attribute in the EMBED/OBJECT tag, please read Java Plug-in 1.3 HTML Specification for more details.
Conclusion
Java Plug-in 1.3 provides support for Java to JavaScript communication through JSObject. It enables applets running inside Java Plug-in to access the document object model and to increase the degree of interaction and integration with the HTML page.