10

From Window.sessionStorage docs:

Opening multiple tabs or Windows on the same URL creates sessionStorage for each tab or Window

https://stackblitz.com/edit/session-storage?file=index.js

Enter your name and click "store"

Your name is now stored in sessionStorage and page view is updated.

sessionStorage.setItem('name', nameInput.value);
nameSpan.innerHTML = nameInput.value;

Now click "open in new tab"

This will open page in new tab by creating link element to the current page, and calling click() on it

const link = document.createElement('a');
link.target = '_blank';
link.href = '/';
link.setAttribute('visibility', 'hidden');
document.body.appendChild(link);
link.click();
link.remove();

As you can see, your name is still there

Why is this happening and is there a way to make new tab open with empty sessionStorage (without clearing current tab sessionStorage)?

Tested this on Chrome 75 and Firefox 66

No, it has nothing to do with stackblitz, behaves the same way on localhost

displayName
  • 986
  • 16
  • 40
  • Please note that session is only kept if you click on the link. If you open a new tab by your own means the session is empty. – Álvaro González Aug 02 '19 at 18:43
  • 1
    Seems like on Chrome >=89 if you click on a link to open in new tab the session is now cleared on opening new tab. Can anyone else verify this? – Steven Mark Ford Mar 08 '21 at 03:38
  • @StevenMarkFord yes this behaviour is confirmed on the Chrome 89 platform status: https://www.chromestatus.com/feature/5679997870145536 – Audwin Oyong Mar 11 '21 at 13:39

3 Answers3

4

Stop cloning sessionStorage for windows opened with noopener after release 89 https://www.chromestatus.com/feature/5679997870145536#details

WDQ
  • 41
  • 2
  • 4
    A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](/help/deleted-answers) – STA Mar 08 '21 at 06:24
3

Reason:

The HTML standard changed to specify that anchors that target _blank should behave as if |rel="noopener"| is set. https://www.chromestatus.com/feature/6140064063029248

Recently, Chrome 89 behaviour has been changed to match the HTML standard specification:

Stop cloning sessionStorage for windows opened with noopener https://developer.chrome.com/blog/deps-rems-89/#stop-cloning-sessionstorage-for-windows-opened-with-noopener

Solution:

Add attribute rel="opener" to the a tag.

This will duplicate the sessionStorage to the new tab opened by clicking on the link.

<a href="http://..." target="_blank" rel="opener">Link</a>
Audwin Oyong
  • 2,247
  • 3
  • 15
  • 32
0

The session has to work that way to maintain state if you navigate to another link within the same application. If you want to open a tab without that session try using incognito mode. Incognito mode will open window with fresh session.

sessionStorage on new window isn't empty, when following a link with target="_blank"

Stradosphere
  • 1,285
  • 3
  • 14
  • 40
  • I think you are confusing sessionStorage with localStorage. I know I can open incognito tab, actually any kind of tab which is opened manually will have empty sessionStorage, but my application must have a feature of opening new tabs on button click. – displayName Aug 02 '19 at 16:33
  • 1
    I think I understand what you are saying but, Opening a page in a new tab or window causes a new session to be initiated with the value of the top-level browsing context per: https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage That is why the value is coming over again in session. – Stradosphere Aug 02 '19 at 16:50
  • 1
    https://stackoverflow.com/questions/17297287/sessionstorage-on-new-window-isnt-empty-when-following-a-link-with-target-bl – Stradosphere Aug 02 '19 at 17:16
  • thanks. add link to your answer so people don't have to read comments :) – displayName Aug 02 '19 at 17:30
  • 2
    Seems like on Chrome >=89 session is now cleared on opening new tab. Can anyone else verify this? – Steven Mark Ford Mar 08 '21 at 03:38
  • @StevenMarkFord Correct, see answer here: https://stackoverflow.com/a/66583860/10158227 – Audwin Oyong Aug 01 '21 at 13:59