我正在使用 JavaScript 设置一个 cookie,它工作正常,但它没有占用我提供的过期时间。无论我提供什么,它都会继续获取 session 值,下面是我从 here 中获取的代码
var now = new Date();
var time = now.getTime();
var expireTime = time + 1000*60;
now.setTime(expireTime);
var tempExp = 'Wed, 31 Oct 2012 08:50:17 GMT';
document.cookie = aaa+'='+sStr+';expires='+now.toGMTString()+';path=/';
我尝试提供硬编码值,但它仍然在 chrome 开发工具中显示为 session 过期
var tempExp = 'Wed, 31 Oct 2012 08:50:17 GMT';
document.cookie = aaa+'='+sStr+';expires='+tempExp+';path=/';
知道我做错了什么吗?
最佳答案
我已将时间设置为 1000*36000。
function display() {
var now = new Date();
var time = now.getTime();
var expireTime = time + 1000*36000;
now.setTime(expireTime);
document.cookie = 'cookie=ok;expires='+now.toUTCString()+';path=/';
//console.log(document.cookie); // 'Wed, 31 Oct 2012 08:50:17 UTC'
}
关于javascript - 如何设置带有过期时间的 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13154552/