Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

session cookie 在浏览器软件关闭后未被删除 #99

Closed
Dream4ever opened this issue Oct 15, 2020 · 0 comments
Closed

session cookie 在浏览器软件关闭后未被删除 #99

Dream4ever opened this issue Oct 15, 2020 · 0 comments
Labels
Browser All about browsers

Comments

@Dream4ever
Copy link
Owner

Dream4ever commented Oct 15, 2020

问题描述

由于业务需要,通过 JS 为一个页面添加 cookie 时,特意没有设置 expires 和 max-age 属性,这样可以保证 cookie 只在当前会话期间(session)有效。用户一旦关闭浏览器,该 cookie 就会被清除,之后再打开浏览器并访问该页面的话,就没有这个 cookie 了。

可是在用 Chrome 测试的时候,发现彻底关闭 Chrome 之后再重新打开,该页面的 cookie 依然存在,测试了几次始终如此。

上网 Google 了一下,才发现这是浏览器的刻意设置。如果设置了浏览器在启动时,继续浏览上次打开的网页,则浏览器在每次关闭时,不会删除已打开网页中有效期为 session 的 cookie。

解决方案

不要用 session cookie,用 sessionStorage 就行,经过实际测试,的确能够满足需求。

参考资料

@Dream4ever Dream4ever added the Browser All about browsers label Oct 15, 2020
@Dream4ever Dream4ever changed the title session cookie 在浏览器窗口关闭后未被删除 session cookie 在浏览器软件关闭后未被删除 Oct 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Browser All about browsers
Projects
None yet
Development

No branches or pull requests

1 participant