24.10. Accessing JavaScript from Java

Not only does LiveConnect let you call Java methods and control applets from JavaScript, but it also lets applets access JavaScript. The netscape.javascript.JSObject class lets you use Java syntax to access all JavaScript objects, read and set all available properties, and call any legal method. Furthermore, you can use the eval method to invoke arbitrary JavaScript code when doing so is easier than using Java syntax. This process involves seven steps, as follows:

Obtain and install the JSObject class.
Import the class in your applet.
From the applet, obtain a JavaScript reference to the current window.
Read the JavaScript properties of interest.
Set the JavaScript properties of interest. ...

Get Core Web Programming, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.