我正在編寫一個JavaFX應用程序,它與JavaScript交互,使用WebView和WebEngine(.executeScript()方法)。webEngine.executeScript();拋出異常
在這裏,我有從Medow.java,它加載map.html(包含JavaScript代碼)這部分代碼,而這種代碼工作得很好:
add_button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent ea5) {
// webEngine.executeScript("document.fun();"); // For Drawing Shapes
if (add == false) {
webEngine.executeScript("document.fun();"); // For Drawing Shapes
add = true;
}
// }
else {
webEngine.executeScript("document.reSet();"); // To remove Drawing Shapes
add = false;
}
}
});
在這裏
webEngine.executeScript();
正在調用相應的JavaScript函數的
,但現在,我想我的Java代碼來調用一些JS功能,當程序啓動,所以我直接寫:
webEngine.executeScript("document.draw();");
權在/在加載map.html文件的代碼之後。
所以,現在作爲這兩者 webEngine.execute("document.fun();");
和webEngine.executeScript("document.draw();");
幾乎是相似的,我不明白呢,它使什麼區別是<button>.setOnAction
塊內,並在外面吧,因爲這兩個WebEngine和webView的聲明爲全局變量。
無法使用HTML的onLoad選項調用document.draw()函數,因爲我需要傳遞一些值來從java中繪製函數。
產生的例外是:
netscape.javascript.JSException: TypeError: undefined is not a function (evaluating 'document.draw()')
我怎樣才能使這項工作?謝謝
雖然連續試圖弄清楚原因究竟是什麼,我發現使用webEngine.load()
創建的HTMLDocument對象由於某種原因只能在句柄方法內部看到,而在其他任何地方,即使它已被定義在外部。