javascript设置带有过期时间的cookie [英] javascript set cookie with expire time

查看:41
本文介绍了javascript设置带有过期时间的cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 Javascript 设置一个 cookie,它工作正常,但没有占用我给出的过期时间.无论我提供什么,它都会继续获取会话值,以下是我从 此处

I am setting a cookie by Javascript and it is working fine but it is not taking the expire time I am giving. It keeps on taking session value regardless of what I give, below is the code which I took from 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 开发工具中显示为会话过期

I tried giving hard-coded value but still it is showing expire as session in chrome dev tool

var tempExp = 'Wed, 31 Oct 2012 08:50:17 GMT';
document.cookie = aaa+'='+sStr+';expires='+tempExp+';path=/';

知道我做错了什么吗?

推荐答案

我已经把时间设置为 1000*36000.

I've set the time to 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆