1

I have 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 client side 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

3
  • I think things stored in the session are serialized, so it would be returned as a string. You could simplify your javascript code var bob = '<%=Session("myArray")%>'.split(","); Commented Mar 5, 2011 at 0:52
  • I liked this idea but it didn't work :( Commented Mar 5, 2011 at 1:42
  • 1
    @Pre classic ASP session store the full object as-is, only Request and Cookies values are stored as pure string. Commented Mar 6, 2011 at 8:28

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

This should work just fine:

var bob = [<%=Join(Session("myArray"), ", ")%>];

1 Comment

This worked great when passing in an array which saved me converting the array to a string. Thanks.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.