【发布时间】:2015-11-08 01:02:10
【问题描述】:
我正在编写一个 JavaFX 应用程序,它与 JavaScript 交互,使用 WebView 和 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 对象由于某种原因仅在句柄方法内部可见,而在其他任何地方都看不到,即使它是在外部定义的。
【问题讨论】:
标签: javascript exception javafx