2 Replies Latest reply on Jan 14, 2013 8:46 AM by 984052

    [WebView] executeScript not working...? Whats wrong?


      I'm working on a client solution, using an HTML-markup as the UI, loaded into a WebView. It works great at all. But one point is totally freaking me out.

      According to this tutorial: http://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm

      It is of course possible to execute Javascript via Java code. Its pretty amazing and I want to use that function, but it does not work at all. Example 5 shows the implementation for hide and show buttons, which purpose is to execute Javascript code, directly embedded in the markup of the forums page. I completely copy-pasted that example in a class, execution was no deal, everything works fine. So I started an own example to get familiar with the executeScript() function first. Thereby I wrote my own classes, Main and CommBrowser:
      class CommBrowser extends Region {
           final WebView browser = new WebView();
           final WebEngine browserEngine = browser.getEngine();
           public CommBrowser(){
                URL markupURL = getClass().getResource("../mockup.html");
                      // here we go...
           private Node createSpacer(){
                Region spacer = new Region();
                HBox.setHgrow(spacer,  Priority.ALWAYS);
                return spacer;
           @Override protected void layoutChildren(){
                double width = getWidth();
                double height = getHeight();
                layoutInArea(browser, 0, 0, width, height, 0, HPos.LEFT, VPos.TOP);
           @Override protected double computePrefWidth(double height){
                return 800;
           @Override protected double computePrefHeight(double width){
                return 600;
      and Main:
      public class Main extends Application {
           private Scene CommScene;
           public void start(Stage primaryStage) {
                primaryStage.setTitle("Cedavis CommPort");
                CommScene = new Scene(new CommBrowser(),800,600,Color.web("#666970"));
           public static void main(String[] args) {
      So as you can see, I just separate the classes and modfied them a bit. It is as simple as it can be. The Javascript is just a small text-function, based on JQuery. Its directly written in the markup, and loaded from an external script file. It does:
      function Hello(){
           $("#import_content").text("Hello Javascript!");
      So just a little Hello-World functionality. Now lets hit the problem.

      When I want to execute my version of this script-example, I get an exception, telling me that "the variable Hello can not be found":
      Exception in Application start method
      Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
           at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
           at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
           at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
           at java.lang.Thread.run(Unknown Source)
      Caused by: netscape.javascript.JSException: ReferenceError: Can't find variable: hideAll
           at com.sun.webpane.platform.WebPage.twkExecuteScript(Native Method)
           at com.sun.webpane.platform.WebPage.executeScript(WebPage.java:1438)
           at javafx.scene.web.WebEngine.executeScript(WebEngine.java:811)
           at view.CommBrowser.<init>(CommBrowser.java:28)
           at view.Main.start(Main.java:14)
           at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
           at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206)
           at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173)
           at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
           at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
           at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
           ... 1 more
      Of course I can execute the function in all browsers, I can also execute it manually in the JS console in Chrome. It just does not work via Java.

      But it works fine in the example based on the tutorial. So what am I doing wrong? Especially I would like to know, how I can execute local scripts, not via absolute paths, but per relative paths. It is all structured in an eclipse JavaFX project. I am using Java 1.7 and eclipse Juno. Using Javascript would be great just to manipulate the DOM. It would save a lot of Java-souce code and would be much easier for me.

      Please help me!