简体   繁体   English

在VB.NET中编写cookie,如何在Javascript中提取?

[英]Wrote cookie in VB.NET, how to extract in Javascript?

I'v scoured the net for hours, but haven't found anything that can help. 我已经在网上搜寻了几个小时,但是没有找到任何可以帮助的东西。 Probably doesn't help that I'm a total noob with javascript. 可能对我对javascript毫无帮助,这无济于事。

In my VB.NET code behind, I've created a multiple name/value pair cookie like this: 在后面的VB.NET代码中,我创建了一个多个名称/值对cookie,如下所示:

        Dim UserCookie As HttpCookie = New HttpCookie("UserInfo")
        UserCookie.Values.Add("UserNumber", Me.UserNumber.Text)
        UserCookie.Values.Add("Password", Me.Password.Text)
        UserCookie.Values.Add("UserName", Me.UserName.ToString)
        Response.Cookies.Add(UserCookie)

Now, what I need to do (somehow) is read that 'UserInfo' cookie in javascript and be able to extract those values at will. 现在,我需要做的(以某种方式)是阅读javascript中的“ UserInfo” cookie,并能够随意提取这些值。 Most of what I'm finding out there doesn't quite do what I need to do. 我在那里发现的大多数内容并没有完全完成我需要做的事情。

I'm trying to do something like this: 我试图做这样的事情:

var MyValue = ReadCookie("UserInfo", "UserName")

...And MyValue would equal whatever the user had typed into the textbox. ...而且MyValue等于用户在文本框中键入的内容。 I can't find anything like this and my javascript... well... I'm a noob. 我找不到这样的东西和我的javascript ...好吧...我是菜鸟。

Anybody know of any good resources? 有人知道有什么好的资源吗?

Thanks, 谢谢,

Jason 杰森

I would highly recommend using jQuery Cookie plugin here . 我强烈建议在这里使用jQuery Cookie插件。 jQuery is a library that makes using JavaScript easier. jQuery是一个使使用JavaScript更容易的库。 jQuery is here jQuery在这里

To add a value to a cookie using jQuery, you would use: 要使用jQuery向Cookie添加值,您可以使用:

$.cookie("myCookieItem", "true");

To read the value, use: 要读取该值,请使用:

$.cookie("myCookieItem")

Taken from Quirks Mode's cookie page : 取自Quirks模式的Cookie页面

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM