如何从Freemarker调用JavaScript函数?

SP5*_*RFD 5 javascript freemarker

我有一些基本的JavaScript函数:

<script type="text/javascript">
    function someTestFunction(param1, param2) {
        //do something
    }
</script>
Run Code Online (Sandbox Code Playgroud)

和Freemarker代码:

<#if something==somethingElse>
    // call: someTestFunction(something, 123)
<#else>
    // call: someTestFunction(somethingElse, 345)
</#if>
Run Code Online (Sandbox Code Playgroud)

我的问题是:是否可能,如果可以,如何从freemarker标签内部调用someTestFunction()?

And*_*Ray 6

Freemarker是一种Java模板语言,意味着它在服务器上执行。javascript是在客户端(用户的浏览器)上执行的。您不能以这种方式从Java服务器调用javascript函数。

您可以执行以下操作:

<script>
<#if something==somethingElse>
    someTestFunction(something, 123);
<#else>
     someTestFunction(somethingElse, 345);
</#if>
</script>
Run Code Online (Sandbox Code Playgroud)

这意味着将根据设置的服务器变量在客户端执行javascript。