Javascript sessionStorage 何时真正清除?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36604747/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:16:25  来源:igfitidea点击:

When is sessionStorage actually cleared?

javascriptjqueryhtmlsession-storage

提问by northernMonkey

I have some javascript that checks for an object in sessionStorage, and uses it to refill an input field. I use this to help users on my site if they leave the form unfinished and either navigate away or try to submit the form after their session has expired.

我有一些 javascript 可以检查 sessionStorage 中的对象,并使用它来重新填充输入字段。我使用它来帮助我网站上的用户,如果他们未完成表单并在会话过期后离开或尝试提交表单。

My understanding is that sessionStorage is NOT linked to a server session, it is linked to the browser, so whether I have a new session on the server or not is irrelevent.

我的理解是 sessionStorage 没有链接到服务器会话,它链接到浏览器,所以我是否在服务器上有一个新的会话是无关紧要的。

This was supported when I was testing this initially a few months ago. However, it seems ot no longer be the case, and when I clear my session cookie and reload my page, my sessionStorage is also cleared out. This is using both Chrome and Firefox.

几个月前,当我最初测试它时,这是支持的。但是,情况似乎不再如此,当我清除会话 cookie 并重新加载页面时,我的 sessionStorage 也被清除了。这是同时使用 Chrome 和 Firefox。

I don't want to use localStorage as that could cause issues with shared computers, whereas sessionStorage will be wiped out when the browser windows is closed.

我不想使用 localStorage,因为这可能会导致共享计算机出现问题,而 sessionStorage 将在浏览器窗口关闭时被清除。

JS to get the value of my stored object

JS获取我存储对象的值

JSON.parse(sessionStorage.getItem("draftPost") || null);

JS to save the value

JS保存值

$("#wallText").on("change",function(){
            sessionStorage.setItem("draftPost",JSON.stringify(draftPost));
        });

回答by timolawl

Session storage is cleared when the browser closes. It persists over page reloads and restores. See: https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage

浏览器关闭时会清除会话存储。它在页面重新加载和恢复时持续存在。请参阅:https: //developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage

回答by rockmo

Something to bear in mind with sessionstorage on mobile safari.

在移动 safari 上使用 sessionStorage 需要记住一些事情。

As stated by the mozila.org doc referred to by timolawl:

正如 timolawl 引用的 mozila.org 文档所述:

Note: since iOS 5.1, Safari Mobile stores sessionStorage data in the cache folder, which is subject to occasional clean up, at the behest of the OS, typically if space is short.

注意:从 iOS 5.1 开始,Safari Mobile 将 sessionStorage 数据存储在缓存文件夹中,该文件夹会根据操作系统的要求偶尔清理,通常是在空间不足的情况下。

This clean-up takes place all too frequently on mobile safari, and all sessionstorage variables are destroyed for all open tabs.

这种清理在移动 safari 上发生得非常频繁,并且所有打开的选项卡的所有 sessionstorage 变量都会被销毁。

For example, having say 5 tabs open, and then loading a new page which has lots of javascript and css will cause the clean-up, destroying all sessionstorage variables.

例如,假设打开 5 个选项卡,然后加载一个包含大量 javascript 和 css 的新页面将导致清理,破坏所有 sessionstorage 变量。

However, localstorage variables are preserved.

但是,本地存储变量被保留。

Also, on safari, (i believe both desktop and mobile), in-private browsing will prevent using either localstorage or sessionstorage.

此外,在 safari 上(我相信桌面和移动设备),私密浏览将阻止使用 localstorage 或 sessiontorage。