1

I am trying to call javascript function in javaFx webview Control ,I have tried every possible combination but i didn't solve it

my js file is

function getHTMLOfSelection () {
   var range;
   if (document.selection && document.selection.createRange) {
   range = document.selection.createRange();
   alert("executed 1");
   return range.htmlText;
   }
   else if (window.getSelection) {
     var selection = window.getSelection();
     if (selection.rangeCount > 0) {
     range = selection.getRangeAt(0);
     var clonedSelection = range.cloneContents();
     var div = document.createElement('div');
     div.appendChild(clonedSelection);
     alert(div.innerHTML)    ;
     return div.innerHTML;  
   }
    else {
     alert("executed 3");
     return '';
    }
  }
  else {
    return '';
  }
}

And my java file is

  package test;

  import javafx.application.Application;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.scene.layout.HBox;
  import javafx.stage.Stage;


  public class JavaFX_GoogleMaps extends Application {
      public Scene scene;
      MyBrowser myBrowser;
      public JavaFX_GoogleMaps () {  }
      public static void initJavaFX_GoogleMaps () {
         launch("hello");
      }
      @Override
      public void start(Stage primaryStage) {
      primaryStage.setTitle("My Application");
      myBrowser = new MyBrowser();
      scene = new Scene(myBrowser, 800, 600);
      primaryStage.setScene(scene);
      primaryStage.show();
  }

  public static void main(String[] args) {
     //launch(args); 
     JavaFX_GoogleMaps.initJavaFX_GoogleMaps();
  }


  class MyBrowser extends Region {
     HBox toolbar;
     static final WebView webView = new WebView();
     static final WebEngine webEngine = webView.getEngine();
     final Button button = new Button("Add Marker");
     String str;

     public MyBrowser () {

       try {
          webEngine.load(new  File("F:/ce/ceacts/ceacts44_2.htm").toURI().toURL().toString());
      } catch(Exception e) {
          System.out.println("Input error "+e);
      }

     toolbar = new HBox();
     toolbar.getChildren().addAll(button);
     getChildren().add(webView);
     button.setOnAction(new EventHandler<ActionEvent>() {
     @Override public void handle(ActionEvent t) {

         Platform.runLater(new Runnable() {
            @Override
             public void run() {
             try {

                     str = webEngine.executeScript(
                            "function getSelectedText() {"
                     + " var range;"
                     + "if (document.selection && document.selection.createRange) {"
                     + "range = document.selection.createRange();"
                     + "alert(range.htmlText);"
                     + "return range.htmlText;"
                     + "}"
                     + " else if (window.getSelection) {"
                     + " var selection = window.getSelection();"
                     + "if (selection.rangeCount > 0) {"
                     + " range = selection.getRangeAt(0);"
                     + "var clonedSelection = range.cloneContents();"
                     + " var div = document.createElement('div');"
                     + "div.appendChild(clonedSelection);"
                     + " return div.innerHTML;"
                     + "}"
                     + " else {"
                     + "alert('else 1');"  
                     + "return '';"
                     + "}"
                     + " }"
                     + "else {"
                     + "alert('else 2');"
                             + "document.write('In Else2');"
                             + "document.write('else 2');"
                     + "return '';"
                     + "}"
                     + "}").toString();


             } catch(Exception e ) {
                    System.out.println("Exception in executing script " +e);
                }

                System.out.println(" Value of str  " +str);
               }
        });

     }});
  getChildren().add(toolbar); 

  } 

 }

When i call this js file in google chrome browser, it work fine but it's not working when we called from javafx webview browser like above code. It throw throws
"Exception in reading js file javax.script.scriptException: sun.org.mozilla.javascript internal.EcmaError:ReferenceError :"document" is not defined"

Thanks in Advance

6
  • Your code does not even reference WebView. Oracle provide instructions on interacting between WebView and Java and the look nothing like your code. Commented May 24, 2013 at 11:06
  • I have edited my question, please check it Commented Jun 3, 2013 at 13:27
  • You are not trying to call js fom javafx webview. You have loaded the html file to webEngine but definitely not calling js from that webEngine. Look this as example. Commented Jun 5, 2013 at 11:57
  • I have edited my question but i get undefined value of script. I want load a html file in webview and execute a javascript method on click of button which is added on webview. My javascript method is working on google chrome its returns selected text with htmltag. Commented Jun 6, 2013 at 12:54
  • @KrishnaRoy, See this tutorial https://blogs.oracle.com/javafx/entry/communicating_between_javascript_and_javafx. As a name implies webEngine.executeScript will execute a js function, not definition of it. Put that function body into html file. Commented Jun 6, 2013 at 13:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.