trx
trx

Reputation: 2167

how to use document and Request.Cookies

I have two variables which needs to be created as a cookie. Can I give them just as without giving any expiration date but just as a key value pair,

 document.cookie = "<%= this.CookieDFKey %> = id";
 alert (document.cookie);
 document.cookie = "<%= this.CookieDateCompleteEnd %> = lastRunDate"; 
 window.location = '<%= ResolveUrl("~/GUI/DRNEW.aspx") %>';

When I gave the alert statement to check what value it is having it shows me

enter image description here

I need to have both the values id and lastRunDate avaiable in the called page. Can I be just using Request.Cookie[the name of cookie where the value store]?

Upvotes: 0

Views: 2951

Answers (2)

vi_gne_sh
vi_gne_sh

Reputation: 59

document.cookie = "id=<%= this.CookieDFKey %>";

document.cookie = "lastRunDate=<%= this.CookieDateCompleteEnd %> ";

To retrieve cookie use the following code

function getCookie(cname) {
    var name = cname + "=";
    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);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length,c.length);
        }
    }
    return "";
}

Upvotes: 1

Krishna Maheswara
Krishna Maheswara

Reputation: 241

First cookies are key value pairs, you will get all cookies in Request.Cookies

If i'm not wrong in C#

 if (Request.Cookies["UserSettings"] != null)
    {
        string userSettings;
        if (Request.Cookies["UserSettings"]["Font"] != null)
        { userSettings = Request.Cookies["UserSettings"]["Font"]; }
    }

Read the below url to set multiple cookies in document.cookie

Setting multiple cookies in Javascript

Upvotes: 2

Related Questions