我的java Fx webview抛出了以下代码的JS异常。谁能解释我为什么?
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 */
答案 0 :(得分:10)
webengine.load()
调用是异步的。在应用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有一个例子。