JavaFX WebView - Communicating between Java and Javascript after page redirection -


i'm developing javafx webview application let users navigate through existing third-party website.

the pages on site rely on $(document).ready() style functions in order work , java bridge object should visible javascript side then.

while code works expected first page loaded, other page user navigates bridge object doesn't seem registered until way after javascript of page has finished executing, throwing bunch of 'undefined' errors.

here's quick project put demonstrating i'm trying do:

webviewtest.java

import javafx.application.application; import javafx.scene.scene; import javafx.scene.layout.stackpane; import javafx.scene.web.webengine; import javafx.scene.web.webview; import javafx.stage.stage; import netscape.javascript.jsobject;  public class webviewtest extends application {      @override     public void start(stage stage) {         try {             webview web = new webview();             webengine engine = web.getengine();             bridge bridge = new bridge();              engine.getloadworker().stateproperty().addlistener((observable, oldstate, newstate) -> {                 switch (newstate) {                     //doing in state other succeeded doesn't seem anything.                     case succeeded:                         jsobject jsobj = (jsobject) engine.executescript("window");                         jsobj.setmember("javabridge", bridge);                         break;                 }             });              stackpane root = new stackpane();             root.getchildren().add(web);              scene scene = new scene(root, 800, 600);              stage.setscene(scene);             stage.show();              //adding these 2 lines gets first page loaded work expected.             jsobject jsobj = (jsobject) engine.executescript("window");             jsobj.setmember("javabridge", bridge);              engine.load("http://localhost/javafxtest/first.html");         } catch (exception e) {             e.printstacktrace();         }     }      public static void main(string[] args) {         launch(args);     } } 

bridge.java

public class bridge {     public void print(string msg) {         system.out.println("invoked javascript: " + msg);     } } 

first.html

<html>     <head>         <script src="jquery-1.12.4.min.js"></script>     </head>     <body>         <script>             javabridge.print('first');              $(document).ready(function(){                 javabridge.print('first ready');             });         </script>          <h1>first</h1>         <a href="second.html">second</a>          <button onclick="javabridge.print('first click');">click!</button>     </body> </html> 

second.html

<html>     <head>         <script src="jquery-1.12.4.min.js"></script>     </head>     <body>         <script>             javabridge.print('second');              $(document).ready(function(){                 javabridge.print('second ready');             });         </script>          <h1>second</h1>         <a href="first.html">first</a>          <button onclick="javabridge.print('second click');">click!</button>     </body> </html> 

running application outputs, desired:

invoked javascript: first invoked javascript: first ready 

however, after clicking link go second page, neither of lines printed, yet clicking on button works intended:

invoked javascript: second click 

and if click first link go first page, lines not printed either, clicking works.

the workaround i've been able come far discarding current webview object , creating new 1 anytime location listener fires, subsequent calls engine.load() don't seem either. nevertheless, doing kinda breaks things if there sessions or post parameters involved...

i have been struggling few days , i'm pretty stuck now, if can point out if i'm missing something, i'd appreciate it.