localStorage NULL in new tab

Abhilash Nagar
Abhilash Nagar used Ask the Experts™
on
Hello,
I am setting

localStorage.setItem("userName",userStrName);

which is fine if it is a single tab. But as soon as new tab opens and i call

localStorage.getItem("userName");

it is null

Not sure why it is null in new tab.

Please suggest.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Brian TaoSenior Business Solutions Consultant
Top Expert 2014

Commented:
Are the pages from the same web server?
Abhilash Nagar.NET Developer

Author

Commented:
Thanks for the response Brian.
The page which i am setting is in local file system but the page which opens in new tab is hosted in IIS web server.
Ryan ChongSoftware Team Lead

Commented:
retrieval of localStorage from another tab should just working well.

what you could have missed are:

1. the sequence of page loading, make sure you loaded the page that calling localStorage.setItem before localStorage.getItem
2. make sure both pages are refer to the same item, in this case: "userName"
3. make sure in the process of showing userName using localStorage.getItem, value of "userName" is not being overwritten or cleared.
4. make sure you got a modern web browser that support localStorage
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Senior Business Solutions Consultant
Top Expert 2014
Commented:
The page which i am setting is in local file system but the page which opens in new tab is hosted in IIS web server.
That's why.  A localStorage item is isolated per domain.  Otherwise, you'll have to somehow add a namespace to avoid key conflicting.  Also, imagining that when your site stores a password in the localStorage (which you shouldn't of course, but somebody will), and a vicious site just tries to scan the entire localStorage and find some auth data.............
Abhilash Nagar.NET Developer

Author

Commented:
Thank you ,really thanks for the response.
Ok sure, so I will then host all the files on IIS and will retry again , I think ,should work after that.
Abhilash Nagar.NET Developer

Author

Commented:
Thanks Brian, as you said i hosted both WEbAPI service and Webapplication , with the same IP address, it worked. Even if there is something like localhost in webapplication URL  and in webapi URL it is IPaddress, it was not working. I changed the webapplication to IPaddress rather then localhost so both are same, it started working then.
So you are right, really thanks for your help.
Abhilash Nagar.NET Developer

Author

Commented:
really thanks for your help.
Brian TaoSenior Business Solutions Consultant
Top Expert 2014

Commented:
No problem.  Glad to help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial