Vinod CG Vinod CG - 1 year ago 144
Javascript Question

JSexception in webview of java fx?

My java Fx webview throws JS exception for following code. Can anyone explain me why?

WebView webview = new WebView();
WebEngine webengine=webview.getEngine();
newtab.setText("Start Page");
webengine.load("file:///E:/myproj/src/novuseditor/resources/html/clike.html");/*works till here*/
/*exception:netscape.javascript.JSException: TypeError: 'null' is not an object */

Answer Source

webengine.load() call is asynchronous. You need to wait until document is loaded before applying javascript. E.g.:

    WebView webview = new WebView();
    final WebEngine webengine = webview.getEngine();
    new ChangeListener<State>() {
        public void changed(ObservableValue ov, State oldState, State newState) {
            if (newState == Worker.State.SUCCEEDED) {
                webengine.executeScript("document.getElementById('header').innerHTML = '<p>Hi</p>';");
    primaryStage.setScene(new Scene(webview, 400, 400));

There is an example from the WebEngine javadoc.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download