如何在单击按钮时从 JavaFX WebView 调用 JavaScript 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19420753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call a JavaScript function from a JavaFX WebView on Button click?
提问by Sandeep Sehrawat
I am trying to call a JavaScript function from a JavaFX WebViewon a JavaFX button click event.
我试图在 JavaFX 按钮单击事件上从 JavaFX WebView调用 JavaScript 函数。
I am using the following code, but it is not working:
我正在使用以下代码,但它不起作用:
try {
File file = new File("F:\inputfile\hello.htm");
WebEngine webengine = webview.getEngine();
webengine.load(file.toURI().toURL().toString());
} catch(Exception ex) {
ex.printStackTrace();
}
On any button click I want to execute the test()
JavaScript method in the html file:
在任何按钮上单击我想test()
在 html 文件中执行JavaScript 方法:
webengine.executeScript("test()");
And the JavaScript method in html file is:
而 html 文件中的 JavaScript 方法是:
<script language="javascript">
function test()
{
window.scrollBy(0, 20);
}
</script>
回答by Maher Abuthraa
I used your code as following and it works
我使用你的代码如下,它的工作原理
package org.im.oor;
import java.io.File;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
/**
*
* @author maher
*/
public class main extends Application {
@Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("fire JS");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if (webengine != null)
{
webengine.executeScript("test()");
}
}
});
publishServices();
StackPane root = new StackPane();
// root.getChildren().add(btn);
HBox hh = new HBox();
hh.getChildren().add(btn);
hh.getChildren().add(webview);
root.getChildren().add(hh);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
private WebEngine webengine;
private static WebView webview;
private void publishServices() {
try {
webview = new WebView();
webview.setVisible(true);
webengine = webview.getEngine();
webengine.setJavaScriptEnabled(true);
File file = new File("c:\hello.html");
System.out.println(file.exists() + " file exitence");
webengine.load(file.toURI().toURL().toString());
} catch (Exception ex) {
System.err.print("error " + ex.getMessage());
ex.printStackTrace();
}
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
content of c:\hello.html
c:\hello.html 的内容
<html>
<header>
<script language="javascript">
function test()
{
//window.scrollBy(0,20);
document.body.style.backgroundColor="#00f3f3";
alert('test');
}
</script>
</header>
<body>test
<a href='#' onclick="test();">fire for test</a>
</body>
</html>
check this code and let me know .. just some points on that :
检查此代码并让我知道 .. 只是一些要点:
check if you can really access that file ..
make you program simple to test if its working JS<==>JavaFX, then you can go further and use more advanced JS functions
检查您是否真的可以访问该文件..
让你的程序变得简单以测试它是否工作 JS<==>JavaFX,然后你可以走得更远,使用更高级的 JS 函数