[Java] [JavaScript]

Java with JavaScript

Important points:
Java to JavaScript, JSObject, getWindow(), getMember(), JSException
JavaScript to Java, document.applets[]

Java applets may access the browser, other applets and plugins through JavaScript. Netscape 3.0 implements the Java Class JSObject to represent JavaScript Objects and JSException to represent JavaScript exceptions. Similarly, JavaScript can access the public methods etc. of Java applets.

Java to JavaScript

Class netscape.javascript.JSObject

public final class JSObject extends Object
 { public Object call(String methodName, Object args[]);
   public Object eval(String s);
   public Object getMember(String name); // == this.name in JavaScript
   public Object getSlot(int index);   // == this[index] in JavaScript
   public void removeMember(String name);
   public void setMember(String name, Object value);//== this.name=value
   public void setSlot(int index, Object value);
   public String toString();  // JSObject-->String
   protected void finalize();

When compiling a Java applet that uses JSObject the CLASSPATH must include ....wherever.../moz3_0.zip
On the Department's own machines (7/'97) this happens to require at least:
setenv CLASSPATH :/usr/monash/contrib/lib/moz3_0.zip

Below are four instances of the Talker Applet which communicate through JavaScript. Click on one of the applets to have it communicate with the others.
You need N3 or later with Java and JavaScript on
[Talker.java] source code.

JavaScript to Java

JavaScript can also access the public methods etc. of Java applets directly. For example, the first applet above (#0, top left) has an HTML FORM associated with it below:

<INPUT TYPE="button" VALUE="talk"
which invokes the talk() method of the applet. The value of the ONCLICK attribute of the button is a piece of JavaScript code to be executed on such an event. The applet as accessed by its NAME (Talker0) but could also be accessed by number as ...applets[0]... . Note the two kinds of quote - " and '. HTML uses " and JavaScript can use either " or ', but must use ' here to avoid an error.

Copyright © L.Allison / 1997