个人的奋斗还是历史的进程?
by
stack overflow上有个千赞答案,原文如下
localStorage and sessionStorage both extend Storage. There is no difference between them except for the intended “non-persistence” of sessionStorage.
That is, the data stored in localStorage persists until explicitly deleted. Changes made are saved and available for all current and future visits to the site.
For sessionStorage, changes are only available per tab. Changes made are saved and available for the current page in that tab until it is closed. Once it is closed, the stored data is deleted.
主要说了两个区别点:
localStorage 除非用户删除,否则一直存在;sessionStorage 随会话关闭而销毁。
看表象的话,localStorage 浏览器关闭也不会被销毁,sessionStorage 则会在浏览器关闭以后就被销毁(关闭页签不会,重新打开还是存在)(基于chrome浏览器123测试)
与会话超时无关:sessionStorage 的生命周期与服务器端会话的超时无关。即使服务器端的会话没有超时,关闭浏览器也会清除 sessionStorage 中的数据。
localStorage 同域共享;sessionStorage页签之间隔离。
sessionStorage 有个特殊场景会共享,就是window.open或a标签打开了新页签,新页签会复制父页签也就是opener的 sessionStorage 进行初始化,但是后续的 change 不会同步
在MDN中是这么描述的
在新标签或窗口打开一个页面时会复制顶级浏览会话的上下文作为新会话的上下文,这点和 session cookie 的运行方式不同。
顶级浏览会话就是指openner
当window.open或者a标签使用了
noopener的参数以后,这个行为就会失效,依此类推,a标签rel=”noopener”也有一样的效果
Expires 或 Max-Age:指定 Cookie 的有效期。Expires 设置具体日期,而 Max-Age 设置从创建时起的秒数。Domain 和 Path:定义了 Cookie 的作用域,即哪些路径和域名下的请求会携带这个 Cookie 发送给服务器。Secure:指示 Cookie 只能在加密(HTTPS)连接中传输。HttpOnly:指示 Cookie 不能被 JavaScript 访问,增加了安全性。Expires 或 Max-Age 属性,它就是一个会话期 Cookie,这种 Cookie 在用户关闭浏览器时会被自动删除。Expires 或 Max-Age 属性的 Cookie 是持久性的,它们会在指定的时间后过期,或者直到用户手动清除它们。Expires 指定的日期或 Max-Age 指定的时间长度后,它就会过期并从浏览器的 Cookie 存储中删除。Expires 设置为过去日期的 Set-Cookie 头,或调用 Max-Age 设置为 0 的 Set-Cookie 头来显式地删除客户端上的 Cookie。Secure 和 HttpOnly 属性可以提高 Cookie 的安全性。