JavaScript Cookie绝对拒绝删除 [英] JavaScript Cookie absolutely refuses to be deleted
问题描述
这与我的其他问题有关,我在问一个新问题缩小问题的范围,原来的问题变得相当复杂。
This is related to my other question, i'm asking a new question as i've narrowed down the problem and the original question was getting rather convoluted.
总而言之,我有一个拒绝删除的cookie!
In a nutshell, i have a cookie that refuses to be deleted!
我的服务器将Cookie设置为 session = abc; domain = example.com; path = /;
My server sets the cookie "session=abc; domain=example.com; path=/;"
我想在客户端编辑此cookie,所以可以说我想将abc更改为xyz,然后运行以下代码
I want to edit this cookie client-side, so lets say i want to change abc to xyz then i run the following code
document.cookie = "session=xyz; domain=example.com; path=/;"
现在,当我检查Cookie时,我实际上现在拥有以下两个Cookie(前导点):
Now, when i inspect the cookies i actually now have the following two cookies (note the leading dot):
"session=abc; domain=example.com; path=/;"
"session=xyz; domain=.example.com; path=/;"
我很快放弃了尝试强制我的新cookie删除点,而是选择删除了
I quickly gave up trying to force my new cookie to drop the dot, and instead opted to delete the existing cookie before setting a new cookie.
但是,这:
document.cookie = 'session=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
这:
document.cookie = 'session=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/; domain=example.com;';
两者都不做
怎么做当我什至无法使域名匹配时,我应该删除cookie吗?
How on earth am i supposed to delete a cookie, when i can't even get the domains to match up?
NB
在写这个问题时,事情变得更加复杂!我刚刚注意到,以上行为仅在特定页面上发生,如果我尝试从另一个页面删除服务器的cookie,它就会消失而不会大惊小怪
Whilst writing this question, things have gotten more complicated! I have just noticed that the above behaviour only happens on a specific page, if i attempt to delete the server's cookie from another page it disappears without a fuss
加侮辱性的伤害
这实际上在IE / Edge中按预期工作
To add insult to injury This actually works as expected in IE/Edge
推荐答案
我知道你没有除非有必要,否则不要使用依赖项,但是JS-Cookie是一个极其轻量级,经过广泛测试的库,具有交叉兼容性。
I know you don't want to use dependencies unless necessary, but JS-Cookie is an extremely lightweight, extensively tested library that is cross compatible; it shouldn't affect performance and could be exactly what you're looking for
这是到 GitHub存储库。
干杯!
这篇关于JavaScript Cookie绝对拒绝删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!