1

I have a javafx application. Initially it loads a login page using WebView. Login page takes user name and redirects to another page. In this html page I have a function inside javascript. I want to call a java method while executing the script. but I end up getting an error saying

ReferenceError: Can't find variable: OpenDoc[at 17]

This my html

html>

    <body onload="login()">
        <div id="jnlpStart_EN">
            <H2>Welcome to Home Page</H2>
        </div>
    </body>

    <script type="text/javascript">
        function login() {
            OpenDoc.passDocId('q56wre');
        }
    </script>

</html>

This is my java code

public class WebEngineTest1 extends Application {

    @Override
    public void start(Stage primaryStage) {
        WebConsoleListener.setDefaultListener((webView, message, lineNumber, sourceId) -> {
            System.out.println(message + "[at " + lineNumber + "]");
        });

        WebView webView = new WebView();
        WebEngine engine = webView.getEngine();

        engine.load("http://localhost:8001/app/login");
        engine.locationProperty().addListener((obs, oldLocation, newLocation) -> {
            if (newLocation != null && newLocation.endsWith("/home")) {
                JSObject window = (JSObject) engine.executeScript("window");
                window.setMember("OpenDoc", new OpenDoc());
            }
        });
        Scene scene = new Scene(webView, 300, 150);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }

    public class OpenDoc {
        public void passDocId(String id) {
            System.out.println(id);
        }
    }
}
1
  • Have you double checked that your if-statement is running? Commented Aug 23, 2018 at 13:29

2 Answers 2

1

I have found the answer. Since after login it redirects to an URL. I had to add a listener with documentProperty(). Inside I add the code for calling java method from javascript. So while loading the page I don't get ReferenceError: Can't find variable: OpenDoc[at 17] message since I added the reference already. here is the code

engine.documentProperty().addListener((v, o, n) -> {
            String newLocation = webEngine.getLocation();
            if (newLocation != null && newLocation.endsWith("/home")) { 
                JSObject window = (JSObject) engine.executeScript("window");
                        window.setMember("OpenDoc", new OpenDoc());
            }           
        });
Sign up to request clarification or add additional context in comments.

Comments

0

First you have to enable JavaScript on the WebEngine

webEngine.setJavaScriptEnabled(true);

and this line will do the trick

webengine.executeScript("initOpenDoc(\"ID\")");

2 Comments

can't you just call a function that initialize the OpenDoc object for you ? See my edited answer
No luck on that.

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.