What's New
Dev Tools
Site Map

Java Interacting with JavaScript/Webpage

Using what Netscape calls "LiveConnect", Java can directly invoke JavaScript functions and manipulate webpage elements.  The code is more involved than the related interaction where JavaScript calls Java but it is not overly complex. The topics covered are as follows:

Note: The code below will work for Netscape or Internet Explorer (despite reference to Netscape throughout).

Getting Netscape's Java Classes

The first step is to setup some Java classes from Netscape so the Java compiler can see the JSObject and JSException classes. The classes are required before Java can access JavaScript or the webpage. If you have Navigator 4.x installed, search under the "Program Files\Netscape" directory for java40.jar (java40.jar works for Netscape 4.x and IE 4+).

Place the JAR file where the compiler can "see" it in the ClassPath. If using Sun's JDK 1.2, the simplest solution is to place java40.jar into jdk1.2.2\jre\lib\ext directory.

Webpage Permission to Java

Before the Java applet can access the webpage, the HTML applet definition needs to given "permission". This is done by specifying the "MAYSCRIPT" attribute in the applet tag.

<applet code="JavaTellPage.class" name="Applet1"
  width=320 height=220 MAYSCRIPT>

Import the Java Classes

At the top of the Java applet, include the following import statement:

import netscape.javascript.*;

This will let you refer to the JSObject and JSException classes without using the prefix "netscape.javascript" in your applet.

Defining Class-level Variables for Webpage Objects

To make the Java coding more straightforward, it is a good idea to declare and initialize Java variables that refer to the webpage objects that you will need to reference.

Declare class-level variables for the window, document, form and a particular form element:

private JSObject mainWindow;
private JSObject pageDoc;
private JSObject pageForm;
private JSObject pageFormElement;

Initialize those variables in the applet's init method:

public void init() {
  // get a handle to parent HTML window, document, form & element
  mainWindow = JSObject.getWindow(this);
  pageDoc 	= (JSObject) mainWindow.getMember("document");
  pageForm 	= (JSObject) pageDoc.getMember("form1");
  pageFormElement = (JSObject) pageForm.getMember("myElement");

Calling a JavaScript Function

In the appropriate place in the Java applet, you can call a JavaScript function. The function parameters are passed as an array. In the example, a JavaScript function called "setMsg" is called with two parameters.

String [] stringArgs = new String[2];
stringArgs[0] = "first arg from Java";
stringArgs[1] = "second arg from Java";
mainWindow.call("setMsg", stringArgs);

Read/update a Form Element

Using the variable which refers to myElement on form1 (defined above), the example first reads and then updates that form element. JSObject's getMember and setMember class methods are used.

String elementVal;
// get the value of the form element
elementVal = (String) pageFormElement.getMember("value");
// now update that form element's value
pageFormElement.setMember("value","Java value");

Directly Execute a JavaScript Statement

A JavaScript statement can be executed directly using the JSObject's eval method. The example shows Java using alert, the JavaScript window-level function.

mainWindow.eval("alert('Java says hello')");

... back to Java Topics

Copyright 1997-2019, Woodger Computing Inc.