如何在 cookie 中赋值 Javascript
How to assign values in the cookies Javascript
我在 CBLL class 中有以下 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;
在使用 javascript 的 .aspx 中,我正在尝试为 CookieDFKey 分配值。所以以后可以用。
var cookie = new Cookie("<%= this.TDRFilterCookieName%>");
cookie.<%= this.CookieDFKey %> = id;
cookie.store();
alert(cookie.<%= this.CookieDFKey %>);
尝试了上面的代码,但它抛出类似 Cookie() 未定义的错误。请帮助我,因为我是 JS 脚本的新手
请阅读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 为 add/get cookie 提供了非常好的方法,我将在此处 copy/paste(所有功劳都归功于他们):
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";
}
我在 CBLL class 中有以下 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;
在使用 javascript 的 .aspx 中,我正在尝试为 CookieDFKey 分配值。所以以后可以用。
var cookie = new Cookie("<%= this.TDRFilterCookieName%>");
cookie.<%= this.CookieDFKey %> = id;
cookie.store();
alert(cookie.<%= this.CookieDFKey %>);
尝试了上面的代码,但它抛出类似 Cookie() 未定义的错误。请帮助我,因为我是 JS 脚本的新手
请阅读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 为 add/get cookie 提供了非常好的方法,我将在此处 copy/paste(所有功劳都归功于他们):
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";
}