admin管理员组文章数量:1323367
I have next code where I do:
Check if my cookie exist if it not exist then I create the cookie called value inside else I read the cookie again and check if value is 1, here i would like to set a new value but not change the expire date and time of this cookie only update the value
if( readCookie("value")==null)
{
createCookie("value",1,1);
}
else
{
if(readCookie("value")==1)
{
/*here I would like to set to cookie called value the new value to 2 */
}
else
{
alert("no es uno");
}
}
function createCookie(name, value, days) {
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
} else {
expires = "";
}
document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) + expires + "; path=/";
}
function readCookie(name) {
var nameEQ = encodeURIComponent(name) + "=";
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, c.length);
if (c.indexOf(nameEQ) === 0) return decodeURIComponent(c.substring(nameEQ.length, c.length));
}
return null;
}
function eraseCookie(name) {
createCookie(name, "", -1);
}
I have next code where I do:
Check if my cookie exist if it not exist then I create the cookie called value inside else I read the cookie again and check if value is 1, here i would like to set a new value but not change the expire date and time of this cookie only update the value
if( readCookie("value")==null)
{
createCookie("value",1,1);
}
else
{
if(readCookie("value")==1)
{
/*here I would like to set to cookie called value the new value to 2 */
}
else
{
alert("no es uno");
}
}
function createCookie(name, value, days) {
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
} else {
expires = "";
}
document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) + expires + "; path=/";
}
function readCookie(name) {
var nameEQ = encodeURIComponent(name) + "=";
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, c.length);
if (c.indexOf(nameEQ) === 0) return decodeURIComponent(c.substring(nameEQ.length, c.length));
}
return null;
}
function eraseCookie(name) {
createCookie(name, "", -1);
}
Share
Improve this question
asked Mar 9, 2016 at 18:57
angelangel
4,64213 gold badges62 silver badges92 bronze badges
2
- So whats' the problem? It looks like you are doing just what you want to do. – Robert Moskal Commented Mar 9, 2016 at 18:58
- I have ReadCookie, CreateCookie, and eraseCookie, now I need an UpdateCookie (but not afected the expires date assigned in creeateCookie) – angel Commented Mar 9, 2016 at 19:02
2 Answers
Reset to default 2While you can't directly check the expiry date, when you set the cookie up you can store the expiry date as part of the cookie. Then whenever you want to check the expiry date, you can look at that part of the cookie. And to change the cookie without affecting the expiry date, just recreate the cookie, replacing the expiry date.
document.cookie="username=John Doe Thu, 18 Dec 2013 12:00:00 UTC; expires=Thu, 18 Dec 2013 12:00:00 UTC";
You can always change the expiry date of the cookie when it is being created.
There is no way to get
expiration date of setted cookie in JavaScript, you can just get the value of cookie, so, I guess, without this knowledge you cant set cookie without replacing previous value of expiration.
本文标签: How to update a cookie value (and not change the expire date) javascriptStack Overflow
版权声明:本文标题:How to update a cookie value (and not change the expire date)? javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742124521a2421877.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论