如何从 Freemarker 调用 JavaScript 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11327826/
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 JavaScript function from Freemarker?
提问by SP5RFD
I have some basic JavaScript function:
我有一些基本的 JavaScript 函数:
<script type="text/javascript">
function someTestFunction(param1, param2) {
//do something
}
</script>
and Freemarker code:
和 Freemarker 代码:
<#if something==somethingElse>
// call: someTestFunction(something, 123)
<#else>
// call: someTestFunction(somethingElse, 345)
</#if>
my question is: Is it possible, and if so, how to call someTestFunction() from inside freemarker tags?
我的问题是:是否有可能,如果有,如何从 freemarker 标签内部调用 someTestFunction()?
回答by Andy Ray
Freemarker is a java templating language, meaning it is executed on the server. javascript is executed on the client (user's browser). You cannot call a javascript function from the java server in this manner.
Freemarker 是一种 Java 模板语言,这意味着它在服务器上执行。javascript 在客户端(用户的浏览器)上执行。您不能以这种方式从 Java 服务器调用 javascript 函数。
You could do something like:
你可以这样做:
<script>
<#if something==somethingElse>
someTestFunction(something, 123);
<#else>
someTestFunction(somethingElse, 345);
</#if>
</script>
which means the javascript wll be executed on the client side depending on what server variable is set.
这意味着 javascript 将根据设置的服务器变量在客户端执行。