Reputation: 2157
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
Upvotes: 0
Views: 49
Reputation: 3227
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";
}
Upvotes: 1