|
Accessing JavaScript from Java
|
|---|
| Java Programming Resources home.
|
| Core Web Programming home.
|
|
These examples illustrate accessing JavaScript from Java applets using
Netscape's "LiveConnect" JavaScript extensions. They only work in
Netscape 3 and 4 and the Windows 95/NT versions of Internet Explorer
4. The process is discussed in detail in Chapter 19 of
Core Web Programming, but the main examples from
that chapter are summarized below. For information on the reverse
process (accessing Java from JavaScript), see the
Java from JavaScript
thumbnail guide.
Building HTML dynamically using the
javascript: URL
In Netscape 3 (and only Netscape 3), calling
showDocument on a
URL of the form "javascript:'[entire HTML document]'"
will cause the browser to display that page. The idea is sketched below:
String page = "'<HEAD><TITLE>...</TITLE></HEAD><BODY>...</BODY>'";
try {
URL pageURL = new URL("javascript:" + page);
getAppletContext().showDocument(pageURL);
} catch(MalformedURLException mue) {
...
}
To try an on-line example of this approach in action, see
TestHTML.html, which uses
TestHTML.java, an applet that gathers input via a
LabeledTextField
and then uses
ShowHTML.java to display the results. The TestHTML.html file also
uses
TestHTMLTop.html and
TestHTMLBottom.html as the contents of the top and bottom frame
cells.
Using JSObject
The netscape.javascript.JSObject class allows applets on
Netscape 3 and 4 to reliably access JavaScript objects, properties, and
functions. Using it involves the following steps:
- Obtain and install the JSObject class. Get it by
unzipping the java_30 or java_301 zip file or uncompressing
the java40 JAR file that comes with the Netscape installation.
Alternatively, just point your CLASSPATH at the archive file.
- Import it in your applet. Use:
import netscape.javascript.JSObject;
- From the applet, obtain a JavaScript reference to the current
window. For this, do something like:
JSObject window =
JSObject.getWindow(this); // this=applet
- Read the JavaScript properties of interest. Use
getMember to access them. E.g.
JSObject document =
(JSObject)window.getMember("document");
String cookies =
(String)document.cookie;
JSObject someForm =
(JSObject)document.getMember("someFormName");
JSObject someElement =
(JSObject)someForm.getMember("someElementName");
You can also use getSlot with an index to access elements
of an array.
- Set the JavaScript properties of interest.
Use the
setMember method for this. E.g.
document.setMember("bgColor", "red");
someElement.setMember("value", "textfield value");
Note that the second argument to setMember must be
a Java Object, so primitive objects must be converted to their
corresponding wrapper type before being passed.
- Call the JavaScript methods of interest.
JavaScript methods are called using either the
call
method or by constructing a JavaScript expression containing the
method calls and passing it to eval. The
call method takes the method name and an array of
arguments; eval takes a single string. For
instance,
String[] message = { "An alert message" };
window.call("alert", message);
window.eval("alert('An alert message')");
- Give the applet permission to access its Web page.
This is done via the
MAYSCRIPT attribute of
the APPLET element. E.g.
<APPLET CODE=... WIDTH=... HEIGHT=... MAYSCRIPT>
...
</APPLET>
For a very simple example, see
MatchColor.java, and applet that looks up the background color
of the enclosing Web page
(MatchColor.html
in this case) via the JavaScript document.bgColor
property.
For a more complex example, see
Everest.html. Here, mouse movement and slider operations in the
applet
(Everest.java)
result in changes in the CGI form entries in the
enclosing Web page. The applet makes use of the
LabeledCostSlider,
CostSlider, and
Slider classes.
More Info
For more Java and JavaScript examples, see my
Java Programming Resources page or
the source code archive for
Core Web Programming. For more info on
Core Web Programming, see the
reader reviews, the
table of contents, and a
sample chapter.