Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

session storage does not working on different tab

I want to pass some data between different tabs using sessionStorage on my local server. I have tried some ways but could not succeed. Anyone please help me.

This is my one file where I am setting my data.

if (typeof(Storage) !== "undefined") {
    var loggedInUserId = loggedInId;
    console.log(loggedInUserId);
    sessionStorage.clear();
    sessionStorage.setItem("loggedin_id", loggedInUserId);
    console.log(sessionStorage);
}

and I am trying to fetch the data from other file of another project of my local drive

var companyId = sessionStorage.getItem("loggedin_id");

The problem is, data is setting on the browser but when I check on my other project tab the storage remain empty.

note: my two projects are in two drives, dont know its makes difference or not. Please anyone give me some way to solve it. thank you .

like image 806
Rajesh Dey Avatar asked Jan 20 '26 19:01

Rajesh Dey


1 Answers

Session storage is not the appropriate tool for your task you should be using local storage For sessionStorage, changes are only available per window (or tab in browsers like Chrome and Firefox)

in your case you want the data to be shared throughout different tabs so you can do something like this :

if (typeof(Storage) !== "undefined") {
    localStorage.setItem("firstname", "fady");
} else {
    // Sorry! No Web Storage support..
}

and then you can retrieve it just the same you did with your session storage

var firstname = localStorage.getItem("firstname");
like image 73
Fady Sadek Avatar answered Jan 22 '26 07:01

Fady Sadek