如何从 Java 调用 Java 脚本

How to call JavaScript from Java

我正在尝试从 Java 调用 Java 脚本函数 testCheckMate,但出现错误:

Exception in thread "JavaFX Application Thread" netscape.javascript.JSException: SyntaxError: Unexpected EOF

WebView 正在持有 FullCalendar

如何从 Java 调用 JQuery/Java脚本?提前谢谢大家。

<!DOCTYPE html>
<html>
    <head>
        <link href='../fullcalendar/fullcalendar.css' rel='stylesheet' />
        <link href='../fullcalendar/fullcalendar.print.css' rel='stylesheet' media='print' />
        <script src='../lib/jquery.min.js'></script>
        <script src='../lib/jquery-ui.custom.min.js'></script>
        <script src='../fullcalendar/fullcalendar.min.js'></script>
        <script>

            $(document).ready(function() {

                var date = new Date();
                var d = date.getDate();
                var m = date.getMonth();
                var y = date.getFullYear();

                $('#calendar').fullCalendar({
                        editable: true,

                    testCheckMate: function() {
                        alert("Check-Mate");
                    },

                    events: [
                        {
                            title: 'All Day Event',
                            start: new Date(y, m, 1)
                        },
                        {
                            id: 999,
                            title: 'Repeating Event',
                            start: new Date(y, m, d + 4, 16, 0),
                            allDay: false
                        }
                    ]
                });

            });

        </script>
    </head>
    <body>
        <div id='calendar'></div>
    </body>
</html>

如果你想在 html 文件上添加一个函数,你可以使用另一个 <script> 标签,或者只使用那里的那个,但是在 $(document).ready() jQuery块:

<script>

    function testCheckMate() {
        alert("Check-Mate");
    };

    $(document).ready(function() {
        $('#calendar').fullCalendar({
           ...
        });
    }); 
</script>

或者像这样answer,需要预先声明函数名:

<script>

    var testCheckMate;

    $(document).ready(function() {

        testCheckMate=function () {
            alert("Check-Mate");
        };

        $('#calendar').fullCalendar({
           ...
        });
    }); 
</script>

或者,如本 所示,您可以在 Java 端创建您的函数,而无需修改 html 文件:

private WebView webView;
private WebEngine engine;

private void createCalendar() {
    webView = new WebView();
    engine = webView.getEngine();
    engine.load("file:D:/standAloneDev//src/fc/fullcalendarwebview/fullcalendar-1.6.4/demos/selectable.html");

    engine.getLoadWorker().stateProperty().addListener((ov,oldState, newState)->{
        if(newState==State.SUCCEEDED){

            // JS to Java
            JSObject script = (JSObject) engine.executeScript("window");
            script.setMember("myClick", new JavaApp());

            // Java to JS
            engine.executeScript("function testCheckMate() {\n" +
                        "            alert(\"Check-Mate\");\n" +
                        "        };");
        }
    });

    engine.setOnAlert(e->System.out.println("Alert: "+e.getData()));
}

并在您的应用中调用它:

 @Override
public void start(Stage primaryStage) {
    Button btn=new Button("Call function");
    btn.setOnAction(e->{
         engine.executeScript("testCheckMate();");
    });
    Scene scene = new Scene(new VBox(webView,btn), 600, 400);

    primaryStage.setScene(scene);
    primaryStage.show();
}

无论如何,请注意 alert window 不会显示在您的日历中,因为它被 webView 困住了。 setOnAlert() 方法用于获取通知。