0

For example. I'm on facebook in console(chrome) and write this:

var txt = "some text"; 
localStorage.setItem("storage",txt);

Then i go on Twitter in console and write this:

var text = localStorage.getItem("storage");
alert(text);

Why it gives me null?

Mimoid
  • 241
  • 2
  • 15
  • what does this mean? _i go on Twitter in console_ – brk Apr 29 '17 at 13:03
  • 3
    Because `localStorage` is domain specific. See [that post](http://stackoverflow.com/questions/4201239/in-html5-is-the-localstorage-object-isolated-per-page-domain). – hhh Apr 29 '17 at 13:04

1 Answers1

4

Because LocalStorage context is per domain.

You can't save an item on one domain, i.e http://foo.com, then go to another domain http://bar.com and access it there.

If you setItem() within http://foo.com you can only getItem() within http://foo.com.

From MDN - Storage:

The Storage interface of the Web Storage API provides access to the session storage or local storage for a particular domain

nicholaswmin
  • 17,508
  • 12
  • 71
  • 142
  • Thanks for answer. Is there an option to do that on different domains? – Mimoid Apr 29 '17 at 13:10
  • No, you can't do that, at least not in a straightforward way. There's some workarounds detailed [here](http://stackoverflow.com/questions/4026479/use-localstorage-across-subdomains) though. – nicholaswmin Apr 29 '17 at 13:11
  • I don't care that is Storage or something else. I just need to transport some text between two different pages. – Mimoid Apr 29 '17 at 13:15
  • That merits a whole new question on it's own – nicholaswmin Apr 29 '17 at 13:16
  • Okay. Thank you for help. – Mimoid Apr 29 '17 at 13:19
  • @Mimoid In general, different domains are not allowed to affect each other in any way, unless the scripts in the two domains have made special arrangements. – Barmar Apr 29 '17 at 13:19
  • @Mimoid It's a security feature. If I go to your web site, I shouldn't have to worry that you've done something that will interfere with my Twitter experience. – Barmar Apr 29 '17 at 13:20