Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Cookie过期日期始终设置为7天_Javascript_Cookies - Fatal编程技术网

Javascript Cookie过期日期始终设置为7天

Javascript Cookie过期日期始终设置为7天,javascript,cookies,Javascript,Cookies,我有一个设置cookies的函数。它使用下面的函数为cookie设置一个Expires值 如果我将30更改为从1到7的任何值,则Expires值将按预期工作。如果我超过了7,则Expires值始终为7天后。为什么呢 function setCookie(cname, cvalue) { const d = new Date(); d.setTime(d.getTime() + (30 * 24 * 60 * 60 * 1000)); const expires = `ex

我有一个设置cookies的函数。它使用下面的函数为cookie设置一个
Expires

如果我将
30
更改为从
1
7
的任何值,则
Expires
值将按预期工作。如果我超过了
7
,则
Expires
值始终为7天后。为什么呢

function setCookie(cname, cvalue) {
    const d = new Date();
    d.setTime(d.getTime() + (30 * 24 * 60 * 60 * 1000));
    const expires = `expires=${d.toUTCString()}`;
    document.cookie = `${cname}=${cvalue};${expires};path=/;SameSite=Strict`; // Using the new SameSite attribute: https://web.dev/samesite-cookie-recipes/
    document.cookie = `${cname}=${cvalue};${expires};path=/;Secure`; // Fallback for legacy browsers: https://web.dev/samesite-cookie-recipes/
}

Javascript本身没有任何可能导致这种情况的限制


但是你的浏览器很可能有。常规浏览器设置或某些已安装的附加组件可能会限制Cookie的生存时间,7天或30天,或者用户认为合适的任何限制。请参见,例如:

很可能是您的浏览器有这样的限制:谢谢,从未意识到这一点!只是在控制台中尝试了一下,并添加了cookie,并按照代码中的预期将过期时间设置为30天。没问题。