jQuery cookie 是否可能像会话变量一样过期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19118000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible for jQuery cookies to expire like session variables?
提问by Adam K Dean
I know this question has been asked a thousand times but none of the answers really give me what I'm looking for. I am using jQuery cookieto store some information, but I want them to expire when the browser closes. window.unload
is not a viable option (read: doesn't work.)
我知道这个问题已经被问了一千次,但没有一个答案真正给我我正在寻找的东西。我正在使用jQuery cookie来存储一些信息,但我希望它们在浏览器关闭时过期。window.unload
不是一个可行的选择(阅读:不起作用。)
My question is this: is it actually possibleto have the cookies set to expire when the browser closes, like a session? If so, does anyone know how?
我的问题是:实际上是否可以将 cookie 设置为在浏览器关闭时过期,就像会话一样?如果是这样,有人知道怎么做吗?
Update:Turns out it's quite simple, instead of passing expiry: 0 (which didn't work), just don't pass any expiry at all. Well, this explains why there wasn't many questions.
更新:事实证明它很简单,而不是传递到期:0(不起作用),只是根本不传递任何到期。嗯,这就解释了为什么没有很多问题。
Thanks all.
谢谢大家。
回答by Adam K Dean
Turns out it's quite simple, instead of passing expiry: 0 (which didn't work), just don't pass any expiry at all. Well, this explains why there wasn't many questions.
事实证明它很简单,而不是传递到期:0(这不起作用),只是根本不传递任何到期。嗯,这就解释了为什么没有很多问题。
So instead of passing expiry with the flags like so:
因此,不要使用像这样的标志传递到期:
$.cookie(key, value, { expiry: 0, domain: '', path: '' });
You just omit the expiry value:
您只需省略到期值:
$.cookie(key, value, { domain: '', path: '' });
And it works. It's such a simple thing, that it confused me.
它有效。这么简单的事情,让我很困惑。
But thanks anyway!
不过还是谢谢啦!
回答by schlingel
It's possible in the RFC 2965 specification. That said it would be possible if you set the "Discard" property in your cookie handling code andif the browser supports it.
在RFC 2965 规范中是可能的。也就是说,如果您在 cookie 处理代码中设置了“Discard”属性并且浏览器支持它,那将是可能的。
That said: jQuery cookie doesn't support the Discard-property. You would have to extend the jQuery-Cookiecode like so (it's about line 57 in the original file):
也就是说:jQuery cookie 不支持丢弃属性。您必须像这样扩展jQuery-Cookie代码(大约在原始文件的第 57 行):
return (document.cookie = [
config.raw ? key : encodeURIComponent(key),
'=',
config.raw ? value : encodeURIComponent(value),
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.discard ? '; Discard=' + options.discard : '',
options.secure ? '; secure' : ''
].join(''));
回答by Otto Kanellis
if you use a default setup like this
如果您使用这样的默认设置
$.cookie.defaults = { path: '/', expires: 365 };
then you need to use the following attribute for each cookie value
那么您需要为每个 cookie 值使用以下属性
$.cookie(key, value, { expires: null });