我在CBLL类中有以下Cookie名称和Cookie项目
public const string COOKIE_NAME_TDR_FILTER = "jS.TDR.Filter";
public const string COOKIE_DF_KEY = "DFKey";
在页面中,我们尝试将值分配给Cookie,以便可以在被调用的页面.aspx.cs
中使用。
protected string TDRFilterCookieName = CBLL.COOKIE_NAME_TDR_FILTER;
protected string CookieDFKey = CBLL.COOKIE_DF_KEY;
在.aspx中使用javascript我试图为CookieDFKey分配值。所以它可以在以后使用。
var cookie = new Cookie("<%= this.TDRFilterCookieName%>");
cookie.<%= this.CookieDFKey %> = id;
cookie.store();
alert(cookie.<%= this.CookieDFKey %>);
尝试了上面的代码,但它抛出错误,如Cookie()未定义。请帮助我,因为我是JS Script
的新手答案 0 :(得分:1)
请阅读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学校提供了非常好的方法来添加/获取cookie,我将在这里复制/粘贴(所有功劳归于他们):
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 "";
}
我刚刚写的函数deleteCookie(cname)
:
function deleteCookie(cname) {
document.cookie = cname + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
}