I 开发者_如何学JAVAhave a Session variable which is an array of integers, for example:
myArray{1,4,3,5,6,7,9,2,...n}
What I am trying to do is pass this in a session var <%=Session("myArray")%> to my clientside javascript. I can do it if I take the session var and convert it to a string like this:
var b = '<%=Session("myArray")%>';
var bob = new Array();
bob = b.split(',');
I am just wondering if there is a more direct way of passing the array possibly cutting out the need to convert it to a string before passing so I can just pass it as an array?
Thanks
you could parse the integers into something that is already an array, so you bob array woul be like that:
var bob = [<%=GetIntegersString(Session("myArray"))%>];
when the page is served to the client it should look like this:
var bob = [1,4,3,5,6,7,9];
the function GetIntegersString
is something you will need to implement
This should work just fine:
var bob = [<%=Join(Session("myArray"), ", ")%>];
JSON is Javascript's way of expressing a literal value, so you could convert your array to json, and then assign it directly:
var bob = '<%=array_to_json(Session("myArray"))%>';
http://www.json.org recommends some ASP utilities to convert data to json:
http://tforster.wik.is/ASP_Classic_Practices_For_The_21st_Century/JSON4ASP
http://code.google.com/p/aspjson/
http://www.webdevbros.net/2007/04/26/generate-json-from-asp-datatypes/
Read more about JSON to know how to express any value as a literal in JS.
精彩评论