Talk to JavaScript with Java
Communication between JavaScript and Java applets can be made possible using something called LiveConnect. JavaScript can be used to access Java variables, classes and methods directly, and conversely Java can be used to access JavaScript methods and properties.
Enabling LiveConnect
Before Java and JavaScript can communicate at all, LiveConnect must be enabled. This is done by putting a special attribute in the <applet> element:
<applet
code="Test"
width="100"
height="100"
codebase="./"
mayscript="mayscript">
</applet>
You must include a mayscript attribute which will enable communication to take place.
JavaScript to Java communication is pretty simple, but for Java to be able to talk to JavaScript it is a bit more complicated. First of all lets look at JavaScript to Java communication.
JavaScript to Java
This is quite simple to do. All the applets in a page can be referred to by their name attribute. They can also be access from an array. Consider the following applet:
<applet
code="Test"
width="100"
height="100"
name="myApplet"
mayscript="mayscript">
</applet>
Here we have set the name of the applet to "myApplet". We can refer to this applet using any of the following methods in JavaScript:
document.myApplet
document.applets["myApplet"]
document.applets[0]
The number of applets in a page is indicated by document.applets.length
Example: myApplet
Consider an applet which has the following Java source code:
import java.applet.Applet;
import java.awt.*;
public class MyApplet extends Applet {
String message="Hello Universe!";
public void paint(Graphics g) {
g.drawString(message, 20, 20);
}
public void setMessage(String message) {
this.message = message;
repaint();
}
}
The HTML file might look like this:
<html>
<body>
<applet
code="MyApplet"
width="200"
height="100"
mayscript="mayscript"
name="myApplet">
</applet>
<script language="javascript">
<!--
myApplet.setMessage("Goodbye World!");
//-->
</script>
</body>
</html>
This would call the setMessage() method in the Java applet and change the message. You can also call other functions which return values...you get the idea.
Java to JavaScript Communication
To access JavaScript methods and properties from a Java applet, we need to import classes from Netscape's netscape.javascript package. The
Java classes for this package can be downloaded here: netscape.jar.
import netscape.javascript.*;
This package contains the JSObject and JSException classes. Before we can access JavaScript, we need to get a reference to the browser weption classes. Before we can access JavaScript, we need to get a reference to the browser window. This is done like this:
JSObject win = JSObject.getWindow(this);
Now we can access JavaScript methods and properties. The getMember() method allows access to each JavaScript object:
JSObject win = JSObject.getWindow(this);
JSObject doc = (JSObject) win.getMember("document");
JSObject myForm = (JSObject) doc.getMember("myform");
JSObject check = (JSObject) myForm.getMember("username");
String userName = (String) check.getMember("checked");
This is how we access JavaScript properties and objects, but how do we access JavaScript methods?
The easiest way is to use the eval() method which lets you evaluate a JavaScript expression:
JSObject.getWindow().eval("expression");
Another way is to use the call() method of JSObject:
JSObject.call(methodName,argArray);
Despite these classes being in Netscape packages, Internet Explorer also supports them. There are a few annoying differences which I have found in Explorer, such as the fact that it won't always return values from functions correctly. I will cover this another time.