Running JavaScript on a page

You can execute an arbitrary JavaScript by calling WebEngine.executeScript(). The return type is not defined; it depends on the type of JavaScript which has been executed, for example, String, Number, JSObject, and so on. You can find a full list in the JavaDoc.

JavaScript numbers from the same function can be matched to different Java objects on each call—either Double or Integer, depending on JavaScript values. To avoid instanceof checks, you can use java.lang.Number for all numerical values. See the following example.

In the next example, we will use JavaScript commands to dynamically show an overlay over a WebView that will show the bounds of the HTML element under the cursor and its tag name.

For the overlay, ...

Get Mastering JavaFX 10 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.