Skip to content Skip to sidebar Skip to footer

Sessionstorage Is Not Empty When Link Opened In New Tab In Internet Explorer

I need to have some unique id on every opened browser tab (in a javascript object). The Id must be saved through requests and i decided to use sessionStorage for it. When i opens

Solution 1:

I know this is an ancient question but I just today struggled with this myself. I was opening a new tab with a link with target="_blank" expecting the sessionStorage to be empty. It was not.

NOTE: All of this is untested code but you should get the jist. Also the browser I was experiencing this problem with was Firefox 44.0.2.

My code was something along these lines:

Problematic code:

$(window).ready(function(){
    //Get saved session datavar persistedObject = null;

    try{
        persistedObject = JSON.parse(sessionStorage.getItem('tabData'));
    }catch(e){}
});

functionupdateSessionStorage(){
    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 picked up data from earlier tabs that were closed.

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

Working code:

$(window).ready(function(){
    //Get saved session datavar persistedObject = null;

    try{
        persistedObject = JSON.parse(window.name);
    }catch(e){}
});

functionupdateSessionStorage(){
    var objectToPersist = {};

    //Get some data to persist
    objectToPersist.value1 = "some data";

    //Save the data in the session storagewindow.name = JSON.stringify(objectToPersist);
}

This worked beautifully. The window name is persisted until you close the tab/window, which is exactly what I was expecting sessionStorage to do. New pages is always loaded with window.name = "" if no name was given on the link handling side.

What you might miss out on is the fact that you can't straight out of the box use names such as my sessionStorage.setItem('tabData', 'some data') but you can easily avoid this by doing something like this:

Suggestion:

functionupdateSessionStorage(){
    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 storagewindow.name = JSON.stringify(objectToPersist);
}

I hope someone finds this useful.

Post a Comment for "Sessionstorage Is Not Empty When Link Opened In New Tab In Internet Explorer"