在浏览器关闭之前存储数据的方法

问题描述:

是否有任何方法(使用 JavaScript)如何在浏览器关闭之前存储会话?我想使用会话 cookie - 当 expires 未设置时,cookie 应该在浏览器关闭时过期.但是对于任何实际目的,这都不可用,因为带有显示我上次的窗口和标签 选项的 Firefox 即使在浏览器关闭后也不会清除会话 cookie.

is there any method (using JavaScript) how to store session until browser is closed? I wanted to use session cookie - when expires is not set the cookie is supposed to expire when browser is closed. But for any practical purpose this is not usable because Firefox with Show my windows and tabs from last time option does not clear session cookies even after browser is closed.

我也不能使用 onunloadonbeforeunload 因为我需要记住数据直到浏览器关闭,即使用户重新加载或转到其他页面然后又回来.

I also cannot use onunload or onbeforeunload because I need to remember the data until the browser is closed even when users reloads or goes to some other page and then comes back.

您可能对 sessionStoragelocalStorage 鲜为人知的表亲.它应该完全符合您的需要——在会话中存储变量并在会话结束时清除它们.

You might be interested in sessionStorage, localStorage's little-known cousin. It should do exactly what you need--store variables in a session and clear them when the session is ended.