本文介绍了java fx的webview中的JSexception?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的java Fx webview抛出了以下代码的JS异常。任何人都可以解释我为什么?

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");
newtab.setContent(webview);
editTabset.getTabs().add(newtab);
webengine.load("file:///E:/myproj/src/novuseditor/resources/html/clike.html");/*works till here*/
webengine.executeScript("document.getElementById('code').value=\"Hi\"");
/*exception:netscape.javascript.JSException: TypeError: 'null' is not an object */


推荐答案

webengine.load()调用是异步的。在应用javascript之前,您需要等到文档加载完毕。例如:

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();
    webengine.getLoadWorker().stateProperty().addListener(
    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>';");
            }
        }
    });
    webengine.load("http://stackoverflow.com");
    primaryStage.setScene(new Scene(webview, 400, 400));

WebEngine javadoc有一个例子。

There is an example from the WebEngine javadoc.

这篇关于java fx的webview中的JSexception?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 23:38