Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to share constants between Java and Javascript

I have in my java classes static variables CONSTANT_1, CONSTANT_2...
What is the best way to share these constants with my javascript functions or if there is a JQuery plugin for this.

Till now the only solution I can think of, is an ajax call in the beginning, to send these static variables to the client.

Thanks

like image 685
Dany Y Avatar asked Feb 26 '13 12:02

Dany Y


2 Answers

I dont know whether this the best way or not, but it works.

var constant1=<%=class.CONSTANT_1%>;
like image 100
sainath reddy Avatar answered Oct 19 '22 14:10

sainath reddy


you can set this static variable in a hidden field, then you can access it by javascript using this hidden field

<input type="hidden" value="<your static variable>" id="staticVariable" />
<script type="text/javascript">
    function getStaticField(){
        return document.getElementById("staticVariable").value;
    }
</script>
like image 28
Georgian Citizen Avatar answered Oct 19 '22 14:10

Georgian Citizen