JavaFX WebView不执行JavaScript [英] JavaFX WebView doesn't execute JavaScript

查看:1703
本文介绍了JavaFX WebView不执行JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows 8上。我有一个JavaFX应用程序,它创建一个带有webview控件的简单窗口并加载本地文件test.html。 test.html包含javascript(一个简单的alert(hello);<(script>)。
但是,javascript被忽略。
JavaFX中的webview使用什么渲染引擎?
我安装了Firefox和IE。只有当用户接受时,后者才会执行包含在本地文件中的JS。所以我的假设是webview使用IE,因为我的操作系统有一些配置。这可能吗?
感谢您的帮助。

I am on Windows 8. I have a JavaFX app that creates a simple window with a webview control and loads the local file "test.html". "test.html" contains javascript (a simple "alert("hello");<(script>"). However, the javascript is ignored. What rendering engine does webview in JavaFX use? I have Firefox and IE installed. The latter executes JS contained in local files only if the users accepts to do so. So my assumption is that webview uses IE due to some configuration of my OS. Is this possible? Thanks for your help.

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import netscape.javascript.JSObject;

public class Hello extends Application {
    @Override
    public void start(final Stage stage) {
        stage.setWidth(400);
        stage.setHeight(500);
        Scene scene = new Scene(new Group());

        WebView browser = new WebView();
        WebEngine webEngine = browser.getEngine();

        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setContent(browser);

        String filepath = this.getClass().getResource("test.html").toExternalForm();

        webEngine.load(filepath);


        scene.setRoot(scrollPane);
        stage.setScene(scene);
        stage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}


推荐答案

当您在javascript中使用alert()函数时,JavaFX WebView不会直观地显示弹出窗口。相反,它会在java中引发一个你可以这样处理的事件:

The JavaFX WebView does not visually show a popup when you use the alert() function in javascript. Instead it raises an event in java that you can handle like this:

myWebView.getEngine().setOnAlert((WebEvent<String> wEvent) -> {
  System.out.println("JS alert() message: " + wEvent.getData() );
});

这篇关于JavaFX WebView不执行JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆