0

I have the following Cookie Name and Cookie Item in a CBLL class as

    public const string COOKIE_NAME_TDR_FILTER = "jS.TDR.Filter";
    public const string COOKIE_DF_KEY = "DFKey";

In the Page we try to assign the values to the cookies so it can be used in the called pages, .aspx.cs.

   protected string TDRFilterCookieName = CBLL.COOKIE_NAME_TDR_FILTER;
   protected string CookieDFKey = CBLL.COOKIE_DF_KEY;

In the .aspx using the javascript I am trying to assign the values for the CookieDFKey. So it can be used later.

  var cookie = new Cookie("<%= this.TDRFilterCookieName%>");
  cookie.<%= this.CookieDFKey %> = id;
  cookie.store();
  alert(cookie.<%= this.CookieDFKey %>);

Tried the above code but it throws error like Cookie() is not defined. Please help me with this as I am new to JS Script

1 Answer 1

1

Please read documentation about cookies

// To create a cookie
document.cookie = "${key}=${value}"; // optional expiration date, see doc.

// To add a new cookie
document.cookie = "${key}=${value}"; // As you can see, `document.cookie` is not a normal Object holding a string

W3 Schools provides very good methods to add/get cookies, that I will copy/paste here (All the credit goes to them):

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+ d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

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 "";
}

And a function deleteCookie(cname) that I just wrote:

function deleteCookie(cname) {
    document.cookie = cname + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
}
Sign up to request clarification or add additional context in comments.

5 Comments

What is the pair of key : value that you actually need to store? CookieDFKey : TDRFilterCookieName? Or maybe CookieDFKey : id?
Key Should be CookieDFKey and value should be id. How should be we giving without the expiration date.
Import the setCookie function above, and then just call it by setCookie(<%= this.CookieDFKey %>, id, 30). There has to be an expiration date. The cookie is deleted by default when the browser is closed when you don't specify any. By putting 30, the cookie will expire after a month. You can put more days if you need them.
But is it possible to have just the key and value without expiration
No. A cookie has to have an expiration. If you want to store data permanently, using a server and database would be a good choice.

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.