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 = {}; }
I hope someone finds this helpful.
Tero Heiskanen Feb 24 '16 at 7:45 2016-02-24 07:45
source share