Raj
Raj

Reputation: 204

Can we use the value of localStorage from other domains?

I am storing a value in local storage in one domain. Can I retrieve that value from another domain if I am accessing both domain from same browser?

Upvotes: 5

Views: 22525

Answers (3)

sihao
sihao

Reputation: 431

You may want to take a look at this blog post. It seem to suggest that you can attempt to use an iframe as a workaround to access local storage from another domain.

Disclaimer: I haven't tried it before but it seems interesting. Let me know if it works!

Upvotes: 0

spurushottam13
spurushottam13

Reputation: 116

Yes, you can use this javascript library. Bifrost-cors size: <2KB

It's very simple to use, you just need to invoke method.

// In www.exampleA.com site:
var bifrostCors = new bifrostCors("http://exampleB.com/", false)

// In www.exampleB.com site
var bifrostCors = new bifrostCors("http://exampleA.com/", false)
bifrostCors.getLocalStorage("local-storage-key-of-what-you-want")

Actually this lib render the iframe (hidden) and uses window.postMessage to communicate with two different contexts (domain). You can also implement by yourself but this lib is very very light < 2Kb. Also not only you can access localStorage you have feature also like.

  1. Get/Set Cookie
  2. Bi-directional message thread
  3. Run JS expression from one domain to other
  4. DOM Manipulation from one domain to other domain ( Iframe )

Upvotes: 2

nikki
nikki

Reputation: 81

No, you can't use the local storage of one domain to other domain. Local Storage is domain based. You can’t read or write from localstorage that’s on different domain even on it's subdomain. you can use it via Iframe on your subdomain. Please go through this article Cross-Domain LocalStorage for detailed explanation.

Hope it'll help. :)

Upvotes: 6

Related Questions