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
我不知道这是否是最好的方式,但它的工作原理。
var constant1=<%=class.CONSTANT_1%>;
你可以在一个隐藏的字段中设置这个静态变量,然后你可以通过javascript使用这个隐藏字段来访问它
<input type="hidden" value="<your static variable>" id="staticVariable" />
<script type="text/javascript">
function getStaticField(){
return document.getElementById("staticVariable").value;
}
</script>
I have faced this problem before. what i did is simply i declared hidden
input field that i can access on the server side and set it's value with whatever i want.
<input type="hidden" runat="server" id="hiddenInput" />
then using the programming language(I use c#):
hiddenInput.Value = ValueOnServerSide;
Then using jQuery i get the value of this input on the client side.
$("[id$='hiddenInput']").val();
链接地址: http://www.djcxy.com/p/69618.html