我在我的网站上有一些值,我想在浏览器关闭时清除。我选择sessionStorage来存储这些值。当tab被关闭时,它们确实被清除,如果用户按f5则保留;但是如果用户在不同的标签中打开一些链接,这些值是不可用的。

我如何与我的应用程序共享所有浏览器选项卡之间的sessionStorage值?

用例:将一个值放在某个存储空间中,使该值在所有浏览器选项卡中都可访问,并在所有选项卡关闭时将其清除。

if (!sessionStorage.getItem(key)) {
    sessionStorage.setItem(key, defaultValue)
}

当前回答

You can just use localStorage and remember the date it was first created in session cookie. When localStorage "session" is older than the value of cookie then you may clear the localStorage Cons of this is that someone can still read the data after the browser is closed so it's not a good solution if your data is private and confidental. You can store your data to localStorage for a couple of seconds and add event listener for a storage event. This way you will know when any of the tabs wrote something to the localStorage and you can copy its content to the sessionStorage, then just clear the localStorage

其他回答

You can just use localStorage and remember the date it was first created in session cookie. When localStorage "session" is older than the value of cookie then you may clear the localStorage Cons of this is that someone can still read the data after the browser is closed so it's not a good solution if your data is private and confidental. You can store your data to localStorage for a couple of seconds and add event listener for a storage event. This way you will know when any of the tabs wrote something to the localStorage and you can copy its content to the sessionStorage, then just clear the localStorage

我发现在选项卡之间共享sessionStorage的唯一方法是window.open:

Window.open ('./page2.html', ")用一个新标签打开page2 window.open('./page2.html','height=100, width=100')在新窗口中用新标签打开page2。

Page2可以从page1获得一个sessionStorage对象的副本,但是这两个sessionStorage对象是相互独立的。

为此使用sessionStorage是不可能的。

来自MDN文档

在一个新的选项卡或窗口中打开一个页面将导致一个新的会话 启动。

这意味着你不能在标签间共享,为此你应该使用localStorage

你可以使用localStorage和它的"storage" eventListener将sessionStorage数据从一个选项卡传输到另一个选项卡。

这段代码需要存在于ALL选项卡上。它应该先于其他脚本执行。

// transfers sessionStorage from one tab to another
var sessionStorage_transfer = function(event) {
  if(!event) { event = window.event; } // ie suq
  if(!event.newValue) return;          // do nothing if no value to work with
  if (event.key == 'getSessionStorage') {
    // another tab asked for the sessionStorage -> send it
    localStorage.setItem('sessionStorage', JSON.stringify(sessionStorage));
    // the other tab should now have it, so we're done with it.
    localStorage.removeItem('sessionStorage'); // <- could do short timeout as well.
  } else if (event.key == 'sessionStorage' && !sessionStorage.length) {
    // another tab sent data <- get it
    var data = JSON.parse(event.newValue);
    for (var key in data) {
      sessionStorage.setItem(key, data[key]);
    }
  }
};

// listen for changes to localStorage
if(window.addEventListener) {
  window.addEventListener("storage", sessionStorage_transfer, false);
} else {
  window.attachEvent("onstorage", sessionStorage_transfer);
};


// Ask other tabs for session storage (this is ONLY to trigger event)
if (!sessionStorage.length) {
  localStorage.setItem('getSessionStorage', 'foobar');
  localStorage.removeItem('getSessionStorage', 'foobar');
};

我在chrome, ff, safari, ie 11, ie 10, ie9中测试了这个

这个方法“应该在IE8中工作”,但我不能测试它,因为我的IE崩溃,每次我打开一个选项卡....任何选项卡……在任何网站上。(很好的IE) PS:如果你想要IE8支持,你显然需要包括一个JSON垫片。:)

文章全文如下: http://blog.guya.net/2015/06/12/sharing-sessionstorage-between-tabs-for-secure-multi-tab-authentication/

实际上看看其他区域,如果你用_blank打开,它会保留sessionStorage,只要你在打开父选项卡时打开:

在这个链接中,有一个很好的jsfiddle来测试它。 当跟随target="_blank"的链接时,新窗口上的sessionStorage不是空的