title: Why doesn't the JavaScript method execute when inserted into the DOM from JavaFX?
body: <p>I have a JavaFX application that makes heavy use of a WebView. I am trying to insert an object into the DOM that the JavaScript code can use, and I need these objects to be available as new pages are loaded.</p>
  
  <p>However, when I run the program, FirebugLite shows the object in the DOM, but the functions do not execute.</p>
  
  <p>According to <a href="http://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm" rel="nofollow noreferrer">some Oracle documentation</a>, this seems to be the appropriate way to provide upcalls from JavaScript to Java. I've also seen a few <a href="https://stackoverflow.com/questions/28687640/javafx-8-webengine-how-to-get-console-log-from-javascript-to-system-out-in-ja">StackOverflow posts</a> explaining the same thing.</p>
  
  <p>What am I missing? I'm using Java 8, Update 51, 64-bit on Windows 7.</p>
  
  <p>Java:</p>
  
  <pre><code>public class DemoApplication extends Application {
  
      Debug debug;
  
      @Override
      public void start(final Stage stage) throws Exception {
          debug = new Debug();
  
          WebView browser = new WebView();
          WebEngine webEngine = browser.getEngine();
          webEngine.getLoadWorker().stateProperty().addListener(
                  new ChangeListener&lt;Worker.State&gt;() {
                      @Override
                      public void changed(ObservableValue&lt;? extends Worker.State&gt; observable, Worker.State oldValue, Worker.State newValue) {
                          if (newValue == Worker.State.SUCCEEDED) {
                              JSObject windowObject = (JSObject) webEngine.executeScript("window");
                              windowObject.setMember("Debug", debug);
                          }
                      }
                  }
          );
          webEngine.load("http://localhost:8080/page1.html");
  
          stage.setScene(new Scene(browser));
          stage.show();
      }
  
  }
  
  public class Debug {
      public void print(final Object text) {
          System.err.println(text);
      }
  }
  </code></pre>
  
  <p>HTML/JavaScript:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;&lt;/title&gt;
    &lt;script type="text/javascript" src="https://getfirebug.com/firebug-lite.js"&gt;&lt;/script&gt;
    &lt;script&gt;
      Debug.print("Hello");
    &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    Page 1
    &lt;a href="page2.html"&gt;Page 2&lt;/a&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Firebug Screenshot:</p>
  
  <p><a href="https://i.stack.imgur.com/zbREl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zbREl.png" alt="enter image description here"></a></p>