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:

1.
Obtain and install the JSObject class.
2.
Import the class in your applet.
3.
From the applet, obtain a JavaScript reference to the current window.
4.
Read the JavaScript properties of interest.
5.
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.