[英]Referencing cookies set in VB.NET with javascript
In the beginning, I set a cookie value like this:一开始,我设置了一个这样的 cookie 值:
Response.Cookies("UserInfo")("UserName") = "Bob"
And it was good.这很好。
In the end, I need to read that cookie in javascript.最后,我需要读取 javascript 中的那个 cookie。 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.
javascript 和 cookies 的文档(尤其是我上面设置的那些)充其量是很差的。 Most cookie-reading functions out there seem to only accept 1 argument (cookie name).
大多数 cookie 读取函数似乎只接受 1 个参数(cookie 名称)。 But as we clearly see here, I have to pass two arguments to get the value for "Bob".
但正如我们在这里清楚地看到的那样,我必须通过两个 arguments 才能获得“Bob”的值。
Any help is greatly appreciated!任何帮助是极大的赞赏!
Thanks,谢谢,
Jason杰森
alert(document.cookie);
will show you everything available to JavaScript.将向您展示 JavaScript 可用的所有内容。
Inserting Bob from the VB side gives something like UserInfo=Username=Bob
.从 VB 端插入 Bob 会得到类似
UserInfo=Username=Bob
的东西。
Let's add something else, like Bob's favorite color:让我们添加一些其他的东西,比如 Bob 最喜欢的颜色:
Response.Cookies("UserInfo")("FavoriteColor") = "Chartreuse"
Now the cookie is UserInfo=Username=Bob&FavoriteColor=Chartreuse
现在 cookie 是
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:获取 cookie 值的一种方法是获取整个集合并将其拆分为分号:
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.在您的情况下,您还需要使用 & 符拆分 UserInfo cookie,然后您将可以访问每个 UserInfo 键/值对。
There are plenty of examples here on StackOverflow about handling cookies in Javascript; StackOverflow 上有很多关于在 Javascript 中处理 cookies 的示例; here's one that I've found useful
这是我发现有用的一个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.