有没有办法隔离不同选项卡之间的"localStorage"?

问题描述:

我当前正在使用window.localStorage在浏览器上存储一些客户端数据.但我想将此存储隔离到各个选项卡.

I am currently using window.localStorage to store some client side data on browser. But I'd like to isolate this storage to individual tabs.

有没有办法做到这一点?

Is there a way to achieve this?

您应该考虑使用

You should consider using sessionStorage instead:

sessionStorage与localStorage类似;唯一的区别是,存储在localStorage中的数据没有到期时间,而存储在sessionStorage中的数据在页面会话结束时被清除.页面会话的持续时间只要浏览器处于打开状态,并且在页面重新加载和还原后仍然存在.在新标签页或窗口中打开页面将导致使用顶级浏览上下文[...]

sessionStorage is similar to localStorage; the only difference is while data stored in localStorage has no expiration time, data stored in sessionStorage gets cleared when the page session ends. A page session lasts for as long as the browser is open and survives over page reloads and restores. Opening a page in a new tab or window will cause a new session to be initiated with the value of the top-level browsing context [...]