0

In the beginning, I set a cookie value like this:

Response.Cookies("UserInfo")("UserName") = "Bob"

And it was good.

In the end, I need to read that cookie in javascript. But I have no idea how to do that. The documentation for javascript and cookies (especially those set like I have above) is poor at best. Most cookie-reading functions out there seem to only accept 1 argument (cookie name). But as we clearly see here, I have to pass two arguments to get the value for "Bob".

Any help is greatly appreciated!

Thanks,

Jason

1 Answers1

0

alert(document.cookie); will show you everything available to JavaScript.

Inserting Bob from the VB side gives something like UserInfo=Username=Bob.

Let's add something else, like Bob's favorite color:

Response.Cookies("UserInfo")("FavoriteColor") = "Chartreuse"

Now the cookie is UserInfo=Username=Bob&FavoriteColor=Chartreuse

One way to get at the cookie values is to take the whole collection and split it on the semi-colon:

var cookieArray = document.cookie.split(";")

In your case, you'll also need to split UserInfo cookie using the ampersand, and then you'll have access to each of your UserInfo key/value pairs.

There are plenty of examples here on StackOverflow about handling cookies in Javascript; here's one that I've found useful

Community
  • 1
  • 1
twip
  • 638
  • 2
  • 9
  • 20