Calling Javascript from an Applet

An applet can invoke Javascript methods by means of an object called JSObject. Access to this object can be obtained either by:

  • Explicit access to the static method JSObject.getWindow to get a reference to the applet window. Requires import of the package netscape.javascript, which is available in a jar file that must be added to the classpath for compilation purpose.

  • Reflection. In this case there is no need to explicitly import the package, even the JSObject may not be referred to directly. However, the code is a bit more complex.

We will stick to the first approach above mentioned. Plenty of information regarding both approaches can be found on the Internet.

The package of JSObject must be made available to the applet development environment. The classpath must point to an external jar file that, depending on the JDK version, can have a different name. The name of the file can be jaws.jar or plugin.jar, they can be found in the JRE lib folder.

Basically, the communication between an applet and Javascript code follows the steps below:

  • Get a reference to the object that represents the applet window. The object is an instance of JSObject:

    JSObject window = JSObject.getWindow(this); 'this' refer to the applet.

  • Call wrapper methods on the window object to access Javascript functions, evaluate Javascript expressions, and access Javascript DOM members directly to get/set their properties. Here is a partial list of these methods:

    • JSObject.call(String jsmethodname,Object[] arguments) - call a Javascript function, given its name, passing arguments values.
    • JSObject.eval(String jsexpression) - evaluates a Javascript expression.
    • JSObject.setMember(String membername,Object[] value) - Sets the value of a member of the Javascript DOM tree.
    • JSObject.getMember(String membername) - Gets a reference to a member of the Javascript DOM tree.

    Here is a few examples of how the methods above work from within an applet:

    1. First of all, the package netscape.javascript must be imported:
      import nestcape.javascript.*; or just import netscape.javascript.JSObject;

    2. Then the next thing to be done is to get a reference to the JSObject that represents the applet window:
      JSObject window = JSObject.getWindow(this);

      the following code can now be used:

      // A Javascript function called 'func' is invoked and is passed an integer and a string.
      window.call("func",new Object[]{new Integer(10),new String("hello")});

      // A Javascript expression is evaluated.
      window.eval("alert('Hello');");

      // Sets the value of a member called 'text' in the Javascript DOM tree. Similar to setting the value of a text field using Javascript:
      // this.document.form.text.value='hello'

      JSObject doc = (JSObject)window.getMember("document");
      JSObject form = (JSObject)doc.getMember("form");
      JSObject text = (JSObject)form.getMember("text");
      text.setMember("value","hello");


    There is one more thing to be done, place the MAYSCRIPT attribute in the applet tag to allow the applet to execute Javascript code in the same page it is loaded from. This attribute works fine with most browsers, please refer to the following link for more information regarding configuration with the OBJECT and EMBED tags: Java to Javascript communication

    The applets below consist of an example of interaction between Java and Javascript, aimed at the usage of JetChart for this specific purpose. The left graph generated by JetChart is event-driven, user can mouse-interact with data points by clicking, moving or dragging. A click on a data point causes a Javascript alert box to be displayed, showing the value of the clicked point. When mouse is moved on a data point, respective value is shown inside the text box. If a data point is dragged, respective index and value are passed to a Javascript function, which in turn passes these values to the applet to the right, where its chart is updated to show the same information of the left chart.
    The right applet implements a public method that receives the index and value from a Javascript function called 'dragged', implemented in this HTML page.

    Get this applet code here. Get this applet code here

Copyright © 2007 Jinsight Informática Ltda. All rights reserved.