Access velocity variable with javascript

☆樱花仙子☆ 提交于 2019-12-05 09:25:39
patel.milanb

try this...

    #set ($test = "works")

  <script type="text/javascript">
       var myvar = "${test}";
       alert (myvar);
  </script>

THIS WORKS SURELY!!!!

Try var get = "$test"; instead of var get = "${test}";

I realize the question is old, but this worked for me:

#set ($test = "hi")
<script>
    alert("$test");
</script>

Need to include quotes around the variable in the alert since it is a string.

I had that problem while using tiles, the only way i got it to work was to use the jstl c:out tag, like:

var get = "<c:out value='${test}' />";

that should work, and remember to add the jstl include on the top of the page

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

Just use it as in html:

<script> 
    var get = $test;

    alert(get);
</script> 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!