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

Krishna Roy
  • 193
  • 1
  • 3
  • 20
  • Your code does not even reference [WebView](http://docs.oracle.com/javafx/2/api/javafx/scene/web/WebView.html). Oracle provide [instructions on interacting between WebView and Java](http://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm) and the look nothing like your code. – jewelsea May 24 '13 at 11:06
  • I have edited my question, please check it – Krishna Roy Jun 03 '13 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](http://stackoverflow.com/q/14029964). – Uluk Biy Jun 05 '13 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. – Krishna Roy Jun 06 '13 at 12:54
  • @KrishnaRoy, See this tutorial [https://blogs.oracle.com/javafx/entry/communicating_between_javascript_and_javafx](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. – Uluk Biy Jun 06 '13 at 13:06
  • that's working fine but I don't want to put javascript method inside html file, I want to execute js file which is outside of html file – Krishna Roy Jun 06 '13 at 13:15

0 Answers0