Strings, numbers, and Boolean values are converted to String, Float,
and Boolean objects, respectively.
All JavaScript values show up as some kind of java.lang.Object in Java. In
order to make use of them, you will have to cast them to subclasses of java.lang.Object,
using the regular casting technique.
Getting a Handle for the
Browser Window
Before you can access JavaScript, you must get a handle for the browser window. Use the
getWindow() method of the JSObject class to do so.
The following Java class gets a handle for the browser window containing the applet:
import netscape.javascript.JSObject;
import java.applet.Applet;
public class js extends Applet {
JSObject win;
public void init() {
win = JSObject.getWindow(this);
}
}
Accessing JavaScript
Objects and Properties
The getMember() method in the class netscape.javascript.JSObject
enables you to access JavaScript objects and properties. You should invoke this function
as a method of a JavaScript object reflected in Java (the JavaScript object is wrapped
with a JSObject). The getMember() method retrieved a named property of a
JavaScript object, thus returning a value equivalent to this.name in JavaScript.
This method receives the name of the desired property in the form of a string. The
following Java method provides you the value of a check box in an HTML document (provided
that the applet is loaded from the same HTML document):
public void init() {
win = JSObject.getWindow(this);
JSObject doc = (JSObject) win.getMember("document");
JSObject myForm = (JSObject) doc.getMember("myForm");
JSObject check = (JSObject) myForm.getMember("myBox");
Boolean isChecked = (Boolean) check.getMember("checked");
}
Since data type casting is often very confusing, take special precautions when using
such routines.
Don’t worry if you don’t completely understand this topic. As you will see later,
you will probably never need to use it, because it is much easier to hand values from
JavaScript to Java rather than the opposite way.
Calling JavaScript Methods
The eval() method in the class netscape.javascript.JSObject enables
you to execute any valid JavaScript statement within a Java code. Use getWindow()
to get a handle for the JavaScript window (the window containing the desired JavaScript
functions or methods), then use eval() to execute a JavaScript statement. The eval()
method is mostly used to invoke JavaScript methods, using the following syntax:
JSObject.getWindow().eval("expression")
Be aware that if you want to hand an argument to a JavaScript method in the form of a
Java data structure (e.g., a String or an int), the argument should
first be evaluated to its literal value in the Java applet. For example, you can use the
following Java method to invoke the JavaScript’s alert() method:
public void JSalert(String str) {
win.eval("alert(\"" + str + "\")");
}
Example 31-1 shows how to invoke a JavaScript method and a JavaScript function using
Java.
<HTML>
<HEAD>
<TITLE>Invoking a JavaScript method from a Java Applet</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
function myAlert(str) {
window.alert(str)
}
// -->
</SCRIPT>
</HEAD>
<BODY>
<APPLET CODE="callMe" NAME="callMe" MAYSCRIPT>
</APPLET>
</BODY>
</HTML>
Example 31-1 (ex31-1.htm). A simple HTML document with a Java applet
and a deferred JavaScript function.
Here’s the source code for the callMe class in Example 31-1:
import netscape.javascript.JSObject;
import java.applet.Applet;
public class callMe extends Applet {
JSObject win;
public void init() {
win = JSObject.getWindow(this);
makeAlert1();
makeAlert2();
}
private void makeAlert1() {
win.eval("alert(\"LiveConnect is cool.\")");
}
private void makeAlert2() {
win.eval("myAlert(\"LiveConnect is cool.\")");
}
}
Example 31-1a (callMe.java). The source code for the callMe
class.