Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I see different behaviors of the HTML5 sessionStorage when following Weblinks. Sometimes the sessionStorage of the first page is being copied to the TargetPage, sometimes I get an empty sessionStorage on the TargetPage.

Can anyone explain me, why this is the case?

Given the following link:

<a href="TargetPage.html" target="_blank">link</a>

When I open the link with a right-click -> "open link in new Tab", the sessionStorage is empty on the TargetPage. (Expected) But when I open the link with a normal left-click, the sessionStorage gets copied over to the TargetPage. (Unexpected)

Note: I've seen this behavior in Chrome and Firefox. Internet Explorer 9 provides an empty sessionStorage in both cases. This is my expected behavior.

I've created a jsFiddle to demonstrate the behavior: http://jsfiddle.net/P9nUv/3/

Can anyone think of a cross-browser solution to ensure an empty sessionStorage for new Browser Tabs/Windows, no matter how they've been opened?

Any help is appreciated!

like image 256
it's me Avatar asked Jun 25 '13 12:06

it's me


People also ask

Does session storage persist on new tab?

The benefit of the sessionStorage is that it'll persist across different pages and browser refreshes. Hence the user may navigate to different pages and/or refresh the page and still remain logged-in. Good.

Is session storage same across tabs?

Opening multiple tabs/windows with the same URL creates sessionStorage for each tab/window. Duplicating a tab copies the tab's sessionStorage into the new tab. Closing a tab/window ends the session and clears objects in sessionStorage .

Does sessionStorage persist after refresh?

Web storage objects localStorage and sessionStorage allow to save key/value pairs in the browser. What's interesting about them is that the data survives a page refresh (for sessionStorage ) and even a full browser restart (for localStorage ).

What is the difference between sessionStorage and session cookie?

If you have a small amount of data you can use instead of sessionStorage a session cookie which remains active until the user closes their browser or clears their cookies. And it also preserves its value among multiple tabs. By omitting expires we set a session cookie.

How to share session storage between tabs in a browser?

Using sessionStorage for this is not possible. Opening a page in a new tab or window will cause a new session to be initiated. That means that you can't share between tabs, for this you should use localStorage Ok, i got it, but how i can clear localStorage when all browser tabs is closed?

What happens when I Left-Click on a blank link?

When left-clicking any link containing a "target="_blank"" attribute in an IE mode-enabled page within our intranet, the destination is successfully opened in a new tab. However, the state of my original tab (as well as the new tab) is effectively logged out and back in, and my session is reset.

How to prevent session shearing between browser tabs in JSP?

Here is a solution to prevent session shearing between browser tabs for a java application. This will work for IE (JSP/Servlet) In your first JSP page, onload event call a servlet (ajex call) to setup a "window.title" and event tracker in the session (just a integer variable to be set as 0 for first time)


2 Answers

IMHO:

I think the problem is in different browsing context. In W3C webStorage specification there is a paragraph:

When a new Document is created in a browsing context which has a top-level browsing context, the user agent must check to see if that top-level browsing context has a session storage area for that document's origin

Document in this quote means new page. So it means if you open in new tab the same page it use the same browsing context and the same sessionStorage, but if you open in new window (ie target="_blank") the browsing context is different.

OR

The problem can be of separate CPU threads, so separate browsing context and separate sessionStorages.

like image 199
Pinal Avatar answered Sep 19 '22 15:09

Pinal


Once again an ancient question but I didn't find any answers to this problem on this site and I just had this same problem with Firefox 44. I found a workaround and you can find it here: https://stackoverflow.com/a/35596134/3751509

Hope this helps.

like image 20
Tero Heiskanen Avatar answered Sep 19 '22 15:09

Tero Heiskanen