socratic_singh
socratic_singh

Reputation: 183

Why is my localStorage data not accessible for different URLs under the same domain?

I am making a mobile web application in backbone under: http://example.com/mobile/index/

After the user logs in I'm storing a token in localStorage and redirecting to a search route within backbone: http://example.com/mobile/index/search

I was under the impression the localStorage information can be shared across the entire domain but the localStorage token is only accessible from the latter URL. Why is this not the case? How can I share localStorage for the entire example.com domain?

A side-point if anyone know - will information for m.example.com, example.com and www.example.com be shared with a similar approach?

Many thanks in advance

Upvotes: 2

Views: 2233

Answers (1)

apsillers
apsillers

Reputation: 115950

That should not happen.

  1. Are you absolutely sure they have the same origin? They must have the same:

    • Subdomains: http://example.com and http://www.example.com are different origins

    • Protocols: http://www.example.com and https://www.example.com are different origins

  2. Alternatively, it's possible that localStoarge might be full, so new data is getting silently ignored.

  3. If you are doing development locally, make sure you do not intermix localhost and 127.0.0.1 (those a different origins that happen to refer to the same physical machine).

  4. If you are doing development locally and loading your page as a file: resource (i.e., the URL actually starts with file://), browsers may treat each file: URL as a distinct origin, or at least treat different directories as different origins.

Upvotes: 4

Related Questions