Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access velocity variable with javascript

<head> #set($test = "works")) </head>

<script> 
    var get = "${test}"; // I also tried using '$test' and "$test" also

    alert(get);
</script> 

And it alerts out ${test}, but should print works.

How can I get it to work?

like image 642
Jaanus Avatar asked Jul 02 '12 09:07

Jaanus


3 Answers

try this...

    #set ($test = "works")

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

THIS WORKS SURELY!!!!

like image 152
patel.milanb Avatar answered Nov 03 '22 01:11

patel.milanb


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

like image 38
xdazz Avatar answered Nov 03 '22 00:11

xdazz


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.

like image 29
mwengstrom Avatar answered Nov 03 '22 00:11

mwengstrom