Actually updating a cookie is slightly different from just replacing a cookie in that the new value we want to place in the cookie is dependent in some way on whether the cookie already exists and if so on what it contains. This means that we need to read the existing cookie before we can write a replacement for it.
实际上,更新cookie与仅替换cookie稍有不同,因为我们要放置在cookie中的新值在某种程度上取决于cookie是否已存在以及它所包含的内容。 这意味着我们需要先读取现有的cookie,然后才能为其编写替代文件。
One thing to note is that when we read a cookie we have no way of telling when the existing cookie is due to expire or whether the cookie is restricted to a specific folder or available across the entire domain. You need to set a new retention period when you replace the cookie and need to keep track of what scope you want the cookie to have within your pages so as to apply the same domain or path option each time. The only thing that you are actually able to read when updating rather than just replacing a cookie is the actual value of the data stored in the cookie.
需要注意的一件事是,当我们读取Cookie时,我们无法得知现有Cookie何时到期,或者该Cookie是否仅限于特定文件夹或在整个域中都可用。 替换cookie时,您需要设置一个新的保留期限,并需要跟踪cookie在页面中的作用域,以便每次都应用相同的域或路径选项。 在更新而不是仅替换cookie时,您真正能够读取的唯一内容是存储在cookie中的数据的实际值。
In this example, we are going to use a cookie named 'accesscount' to count the number of times that our visitor has accessed our page where no more than seven days has elapsed between visits. Should more than seven days elapse between visits then the cookie will expire and the next visit will restart counting from zero. We are using the allCookies() and writeCookie() functions from the prior examples so the only piece of new code we need in order to actually do the update is in the last two lines.
在此示例中,我们将使用名为“ accesscount”的cookie来计算访问者访问我们的页面的次数,两次访问之间的时间间隔不超过7天。 如果两次访问之间的间隔时间超过7天,则Cookie将会过期,下一次访问将从零开始重新计数。 我们正在使用先前示例中的allCookies()和writeCookie() 函数 ,因此,实际上需要进行更新的唯一代码就是最后两行。
var cookie;
allCookies = function() {
var cr, ck, cv;
cr = []; if (document.cookie != '') {
ck = document.cookie.split('; ');
for (var i=ck.length - 1; i>= 0; i--) {
cv = ck.split('=');
cr[ck[0]]=ck[1];
}
}
return cr;
};
writeCookie = function(cname, cvalue, days,opt) {
var dt, expires, option;
if (days) {
dt = new Date();
dt.setTime(dt.getTime()+(days*24*60*60*1000));
expires = "; expires="+dt.toGMTString();
} else expires = '';
if (opt) {
if ('/' = substr(opt,0,1)) option = "; path="+opt;
else option = "; domain="+opt;
} else option = '';
document.cookie = cname+"="+cvalue+expires+option;
}
cookie = allCookies(); if (cookie.accesscount != null) writeCookie('mycookie', cookie.accesscount + 1,7);
else writeCookie('mycookie', 1,7);
var cookie;
allCookies = function(){
var cr,ck,cv;
cr = []; 如果(document.cookie!=''){
ck = document.cookie.split(';');
对于(var i = ck.length-1; i> = 0; i--){
cv = ck.split('=');
cr [ck [0]] = ck [1];
}
}
返回cr
};
writeCookie = function(cname,cvalue,days,opt){
var dt,过期,选项;
如果(天){
dt = new Date();
dt.setTime(dt.getTime()+(days * 24 * 60 * 60 * 1000));
expires =“; expires =” + dt.toGMTString();
} else expires =”;
如果(选择){
if('/'= substr(opt,0,1))option =“; path =” + opt;
else option =“; domain =” + opt;
} else option ='';
document.cookie = cname +“ =” + cvalue + expires + option;
}
cookie = allCookies(); 如果(cookie.accesscount!= null)writeCookie('mycookie',cookie.accesscount + 1,7); 否则writeCookie('mycookie',1,7);
翻译自: https://www.thoughtco.com/javascript-by-example-updating-cookies-2037276