localStorage NULL in new tab

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.
Abhilash Nagar.NET DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian TaoSenior Business Solutions ConsultantCommented:
Are the pages from the same web server?
Abhilash Nagar.NET DeveloperAuthor 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 LeadCommented:
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
Get Blueprints for Increased Customer Retention

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

Brian TaoSenior Business Solutions ConsultantCommented:
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.............

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Abhilash Nagar.NET DeveloperAuthor 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 DeveloperAuthor 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 DeveloperAuthor Commented:
really thanks for your help.
Brian TaoSenior Business Solutions ConsultantCommented:
No problem.  Glad to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.