SessionStorage is not empty when the link is open in a new tab in Internet Explorer

I need to have a unique identifier on every open browser tab (in a javascript object). The identifier should be saved through requests, and I decided to use sessionStorage for it.

When I open a new page in the browser, it works well.

But when I right-click the link and select "Open link in a new tab" in IE 11 - sessionStorage is not empty. Therefore, my expectations regarding the new identifier failed.

Chrome works differently, sessionStorage is empty.

Does anyone know how to solve this problem for IE?

+3
javascript internet-explorer session-storage
Jun 24 '14 at 8:54
source share
1 answer

I know this is an ancient question, but I just struggled with it myself today. I opened a new tab with a link with target="_blank" , expecting sessionStorage to be empty. This is not true.

NOTE. All this is untested code, but you should get jist. Also the browser I came across was Firefox 44.0.2.

My code was something like this:

Problem code:

 $(window).ready(function(){ //Get saved session data var persistedObject = null; try{ persistedObject = JSON.parse(sessionStorage.getItem('tabData')); }catch(e){} }); function updateSessionStorage(){ var objectToPersist = {}; //Get some data to persist objectToPersist.value1 = "some data"; //Save the data in the session storage sessionStorage.setItem('tabData', JSON.stringify(objectToPersist)); } 

This selected data from previous tabs that were closed.

I solved this by changing my code to something like this:

Work code:

 $(window).ready(function(){ //Get saved session data var persistedObject = null; try{ persistedObject = JSON.parse(window.name); }catch(e){} }); function updateSessionStorage(){ var objectToPersist = {}; //Get some data to persist objectToPersist.value1 = "some data"; //Save the data in the session storage window.name = JSON.stringify(objectToPersist); } 

It worked beautifully. The window name is saved until you close the tab / window, as I expected from sessionStorage. New pages are always loaded using window.name = "" if the name is not indicated on the link processing side.

What you might miss is the fact that you cannot directly use names like my sessionStorage.setItem('tabData', 'some data') , but you can easily avoid this by doing something like of this:

Sentence:

 function updateSessionStorage(){ var objectToPersist = {}; try{ objectToPersist = JSON.parse(window.name); }catch(e){} if(objectToPersist[tabData] == undefined){ objectToPersist.tabData = {}; } //Get some data to persist objectToPersist.tabData.value1 = "some data"; //Save the data in the session storage window.name = JSON.stringify(objectToPersist); } 

I hope someone finds this helpful.

+5
Feb 24 '16 at 7:45
source share



All Articles