I want to a...">

Use freemarker variable in javascript / Jquery

I have declared a variable in freemarker as

<#assign myvariable= "value">

      

I want to access it in my javascript function like below.

function myfunction(){

    alert(myvariable);

}

      

+3


source to share


2 answers


I assume that you should first output this variable into your HTML / JavaScript code, for example:



<script type="text/javascript">
var myvariable = "${myvariable}";
function myfunction(){
    alert(myvariable);
}
</script>

      

+6


source


You can immediately use the FreeMarker JavaScript code. Here's a sample code where FreeMarker supplies Morris.js chart data. I think you will understand this idea.



new Morris.Area({
    element: 'searchTrend',
    resize: true,
    data: [
    <#list searchCount as sc>
    {day: '${sc.date!?string("yyyy-MM-dd")}', count: ${sc.searches}} <#sep>,
    </#list>
    ],
    xkey: 'day',
    ykeys: ['count'],
    labels: ['Count'],
    xLabelFormat: function (x) { return x.getDate(); }
});

      

0


source







All Articles