displayName
displayName

Reputation: 1106

Why is sessionStorage preserved across multiple tabs?

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


Update:

This issue is fixed with Chrome 89

Upvotes: 11

Views: 12908

Answers (3)

Audwin Oyong
Audwin Oyong

Reputation: 2531

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>

Upvotes: 3

WDQ
WDQ

Reputation: 41

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

Upvotes: 4

Stradosphere
Stradosphere

Reputation: 1285

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"

Upvotes: 0

Related Questions