Spec-Zone .ru
спецификации, руководства, описания, API
|
|
LiveConnect Support
|
Contents
The
LiveConnect support has been fortified with backward compatibility in mind. Formerly Mozilla-specific LiveConnect functionality, such as the ability to call static Java methods, instantiate new Java objects and reference third-party packages from JavaScript, is now available in all browsers.
The Netscape.javascript.JSObject
class enables communication between the Java code in the applet and JavaScript in the HTML page. This class is in the plugin.jar
file along with the other jars in jre/lib
. To access it, add the plugin.jar
file to the compilation class path.
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. Browsers allow communication between Java and JavaScript through the Java wrapper class netscape.javascript.JSObject
.
Because the JavaScript rendering engine is implemented differently between browsers, Java Plug-in provides different degrees of support for JSObject
in Internet Explorer and Navigator. This document specifies how JSObject
support works in different browser environments.
JSObject
WorksJSObjec
t provides an easy way to access the DOM of an HTML page. But because different browsers implement the DOM differently, using JSObject
in a Java applet may yield different behaviors in Java Plug-in. For details about the DOM implementation in a particular browser, consult the developer guide for that 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 } } |
The starting point is the static
method
public static JSObject getWindow(Applet a)
which returns a JSObject
representing the Window
that contains the given applet. Since this method takes only java.applet.Applet
as parameter, JSObject
can be accessed from an applet, but not from a bean unless the bean is also an applet.
Once the Window
object is obtained, the applet can navigate the DOM of the HTML page 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()
public boolean equals(Object obj)
We recommend using only getWindow()
, call()
, eval()
, setMember()
and getMember()
in Java Plug-in. The implementations of getSlot()
, setSlot()
, removeMember()
and toString()
are browser-dependent; i.e., the result of execution may vary depending on the version and platform of the browser in which Java Plug-in is running.
To compile Java code to take advantage of JSObject
, you must have the package netscape.javascript in the CLASSPATH
. Since 1.4.2 Java Plug-In ships netscape.javascript in a JAR file called plugin.jar
. To compile an applet which uses JSObject
, add plugin.jar
in the CLASSPATH
before compilation.
Note that although JSObject
is supported in Java Plug-in, it is not supported in AppletViewer in the Java SE platform. As a result, applets using JSObject
may not run in AppletViewer or result in exceptions.
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:
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> |
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> |
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, see Using OBJECT
, EMBED
and APPLET
Tags in Java Plug-in.
JavaScript and LiveConnect are technologies from Netscape Communications Corporation. Due to the differences in the Javascript engine implemented by Mozilla and Microsoft, data marshalling between Java and JavaScript via LiveConnect may result in different datatypes for different browsers. The main differences in JavaScript implementations from vendors are in JavaScript datatype support, mapping of datatypes between Java and JavaScript, and DOM element implementation.
For more information on Java to Javascript communication including JSObject and datatype conversions, see:
You need to include the following in your applet's HTML page:
These tags are explained in the following sections.
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:
Now, you can use the name Fractal within scripts to refer to the Fractal applet.ID="Fractal"
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>
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 JavaScript.
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()
Copyright
©2008 |
|