为什么关闭浏览器后我的cookie丢失了?(由javascript设置的过期时间) [英] Why my cookie is lost when the browser is closed? (Expires set by javascript)
本文介绍了为什么关闭浏览器后我的cookie丢失了?(由javascript设置的过期时间)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过JavaScript设置永久cookie.我有类似的东西:
I am trying to set a permanent cookie by JavaScript. I have something like:
document.cookie = name+'='+value+";expires="+(new Date(2019, 1, 1)).toUTCString();
它不起作用.关闭浏览器后,cookie消失.这是为什么?:(
It does not work. The cookie disappears when the browser is closed. Why is that? :(
推荐答案
尝试一下
function setCookie(key, value) {
var expires = new Date();
expires.setTime(expires.getTime() + 31536000000); //1 year
document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();
}
我刚刚设置为1年,您可以根据自己的需要进行更改.
I have just set to 1 Year and you can change it to your convinience.
这篇关于为什么关闭浏览器后我的cookie丢失了?(由javascript设置的过期时间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文