grevory / angular-local-storage

An AngularJS module that gives you access to the browsers local storage with cookie fallback
Other
2.83k stars 585 forks source link

localstorage not sharing data between tabs #252

Closed ashiefk closed 9 years ago

ashiefk commented 9 years ago

I have implemented localstorage in my web app configuration given below,

app.config(function (localStorageServiceProvider) { localStorageServiceProvider .setPrefix('appName') .setStorageType('sessionStorage') .setNotify(true, true) });

and i set some data inside localstorage but when i checked in different tab (browser tabs) its returning null. Is there anything which i missed in configuration (do i need to change sessionStorage to cookie?). Is there anything i need to specify inorder to share the data between tabs? Please help !!! I really need the data between tabs just like server session.

juliendangers commented 9 years ago

you can't share data between multiple tabs using sessionStorage, because a new page is a new session

see MDN

Opening a page in a new tab or window will cause a new session to be initiated, which differs from how session cookies work.

You need to replace . setStorageType('sessionStorage') by . setStorageType('localStorage') // which is default

ashiefk commented 9 years ago

Thank you so much, really helpful info.

aplocher commented 7 years ago

Is there a way to get the best of both worlds? When the browser is closed the session no longer exists (as is the case with "sessionStorage") but while actively using the browser the user will stay logged in across tabs?

Correct me if I'm wrong, but I think this is the type of behavior you would see using something like PHP to manage the sessions.