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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.