title: How to run javascript code in JavaFx.WebView after the page is loaded?
body: <p>i used this code </p>
  
  <pre><code>public WebMap() {
      webView = new WebView();
      final WebEngine webEngine = webView.getEngine();
      String url = getClass().getResource("map.html").toExternalForm();
      webEngine.load(url);
  
      final Deferred d = new Deferred();
      webEngine.getLoadWorker().stateProperty().addListener(
          new ChangeListener&lt;State&gt;() {
              @Override
              public void changed(ObservableValue ov, State oldState, State newState) {
                  if (newState == State.SUCCEEDED) {
                      d.resolve(webEngine);
                  }
              }
          }
      );
      readyWebEngine = d.promise();
  }
  </code></pre>
  
  <p>I don't call webEngine.executeScript directly but i use</p>
  
  <pre><code>readyWebEngine.then(new PromiseFunc() {
              @Override
      public Promise apply(Object oWebEngine) {
          WebEngine webEngine = (WebEngine)oWebEngine;
          webEngine.executeScript(script); // &lt;=============================
                  // return value doesn't matter
          return (new Deferred()).promise();
      }
  });
  </code></pre>
  
  <p>So that the <code>script</code> string isn't evaluated until the promise is resolved (fulfilled) by the call to <code>d.resolve()</code></p>
  
  <p>Let's assume that the promise library is correct</p>
  
  <p>The code works well for me, but a colleague of mine gets this error which is frustrating </p>
  
  <pre><code>netscape.javascript.JSException: ReferenceError: Can't find variable: gotoLatLng
  [com.sun.webpane.platform.WebPage.twkExecuteScript(Native Method), com.sun.webpane.platform.WebPage.executeScript(WebPage.java:1438), javafx.scene.web.WebEngine.executeScript(WebEngine.java:811), packagename.WebMap.WebMap$1.apply(WebMap.java:36), packagename.WebMap.promise.Deferred$1$1.apply(Deferred.java:23), packagename.WebMap.promise.Deferred.resolve(Deferred.java:40), packagename.WebMap.WebMap$2.changed(WebMap.java:57), packagename.WebMap.WebMap$2.changed(WebMap.java:52), com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:196), com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:100), javafx.beans.property.ReadOnlyObjectWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyObjectWrapper.java:195), javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:161), javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:130), javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:163), javafx.scene.web.WebEngine$LoadWorker.updateState(WebEngine.java:975), javafx.scene.web.WebEngine$LoadWorker.dispatchLoadEvent(WebEngine.java:1086), javafx.scene.web.WebEngine$LoadWorker.access$600(WebEngine.java:968), javafx.scene.web.WebEngine$PageLoadListener.dispatchLoadEvent(WebEngine.java:955), com.sun.webpane.platform.WebPage.fireLoadEvent(WebPage.java:2356), com.sun.webpane.platform.WebPage.fwkFireLoadEvent(WebPage.java:2204), com.sun.webpane.webkit.network.URLLoader.twkDidFinishLoading(Native Method), com.sun.webpane.webkit.network.URLLoader.access$1300(URLLoader.java:44), com.sun.webpane.webkit.network.URLLoader$6.run(URLLoader.java:691), com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76), com.sun.glass.ui.win.WinApplication._runLoop(Native Method), com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17), com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67), java.lang.Thread.run(Thread.java:744)]
  </code></pre>
  
  <p>The html page containing the javascript code is well tested. Let's assume there is no javascript errors there</p>
  
  <p>The error <code>netscape.javascript.JSException: ReferenceError: Can't find variable: gotoLatLng</code> doesn't occure to me because javascript code is executed after the loadWorker's newState == State.SUCCEEDED. </p>
  
  <p>The probleme is that in the stack trace of my coworker's program , we can see that the <code>WebEngine.executeScript(WebEngine.java:811)</code> was called by </p>
  
  <pre><code>packagename.WebMap.promise.Deferred.resolve(Deferred.java:40), packagename.WebMap.WebMap$2.changed(WebMap.java:57), packagename.WebMap.WebMap$2.changed(WebMap.java:52), com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:196), com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:100), 
  </code></pre>
  
  <p>So, executeScript is called by the listener of the <code>load changed event</code> when <code>newState == State.SUCCEEDED</code> which means that the page is completely loaded and javascript inside s is loaded (variables should be defined).</p>
  
  <p>Sorry because i don't have access to my coworker's code, but it's based on mine. Now i am not sure whether this code executes javascript code at the right moment</p>
  
  <pre><code>webEngine.getLoadWorker().stateProperty().addListener(
          new ChangeListener&lt;State&gt;() {
              @Override
              public void changed(ObservableValue ov, State oldState, State newState) {
                  if (newState == State.SUCCEEDED) {
                      webEngine.executeScript(script);
                  }
              }
          }
      );
  </code></pre>
  
  <p>Any help?</p>