0

On example.com I have an iframe which loads anotherexample.com. The problem I am having though is if I load anotherexample.com directly (outside of the iframe) the localStorage isn't shared with the iframed anotherexample.com.

Is this possible to achieve?

An example of the issue can be seen by visiting https://eskimo.dev/localstorage/, then visiting https://eskimo.ooo/localstorage/iframe which iframes the first link. The iframe isn't using the localStorage from the previous.

eskimo
  • 304
  • 1
  • 3
  • 15
  • Without seeing actual code, all I can offer is that I doubt that's possibly true. – Pointy Nov 10 '22 at 14:23
  • Does this answer your question? [How to use localStorage in iFrame](https://stackoverflow.com/questions/61554531/how-to-use-localstorage-in-iframe) – angel.bonev Nov 10 '22 at 14:24
  • @angel.bonev I don't believe so, almost everything I'm finding that's already asked is about accessing data on a different domain. In this case I want to access data on the same domain but iframed. – eskimo Nov 10 '22 at 14:27
  • @eskimo You're saing "On example.com I have an iframe which loads anotherexample.com." . Those are different domains – angel.bonev Nov 10 '22 at 14:36
  • @angel.bonev I'm not trying to access the storage from `anotherexample.com` on `example.com`. I'm just trying to have the same data for `anotherexample.com` both inside an iframe and also on `anotherexample.com` directly. – eskimo Nov 10 '22 at 14:42
  • @eskimo the problem is with the origin, you can try something like ` – angel.bonev Nov 10 '22 at 14:48
  • 2
    @angel.bonev what the OP wants is that "anotherexample.com" code loaded in his iframe and "anotherexample.com" loaded in another browser tab should both share the localstorage content. – Pointy Nov 10 '22 at 14:56
  • ... and I strongly suspect that they do, but because no code has been posted, who knows what's really happening – Pointy Nov 10 '22 at 14:57
  • @Pointy You can check https://eskimo.dev/localstorage and then https://eskimo.ooo/localstorage/iframe - if you access the /iframe link 2nd you'll see that it generates a new random number and saves that instead of using the value that already exists on the non /iframe link. – eskimo Nov 10 '22 at 15:00
  • You must send postMessage to domain which localStorage you wish to change. And then site on this domain must listen this postMessage and change localStorage – Андрей Беспалов Nov 10 '22 at 15:07
  • 2
    @АндрейБеспалов You are misunderstanding the question. The OP does not want to communicate between domains. – Pointy Nov 10 '22 at 16:34
  • @eskimo It's `existed already`, – angel.bonev Nov 11 '22 at 10:49
  • Looking in the dev tools _Storage_ panel, it seems pretty obvious that the framed and direct domains are using different storage buckets. – Phil Nov 23 '22 at 01:24
  • @Phil yeah, just unsure of why this is the case and if there's a workaround? – eskimo Nov 23 '22 at 01:39
  • Probably related: https://stackoverflow.com/questions/63922558/safari-localstorage-not-shared-between-iframes-hosted-on-same-domain – Quentin Nov 27 '22 at 17:48

3 Answers3

-2

Actually I tried, and it worked for me (I accessed first /iframe but it worked for me both ways):

Image from https://eskimo.ooo/localstorage/iframe

Image from https://eskimo.dev/localstorage/

Image from DevTools

Of course, when I disabled all third party cookies it didn't work. Might that be the reason?

I'm using chrome 107.0 by the way.

L.Raudel
  • 52
  • 3
-2

Not sure why its not behaving as it should. May be code snippets might be really helpful here. But by the above linked example in the OP. The reason its not working there is :

  1. There are 2 domains : eskimo.dev & eskimo.ooo (Both of these are different). When the first domain eskimo.dev is opened, the local storage is set in the browser with reference to eskimo.dev and this will accessible to page with origin "eskimo.dev".
  2. But when eskimo.ooo is opened (domain that has the iframe src set to eskimo.dev) here the localStorage will not be shared as the origin domain is eskimo.ooo, and therefore cannot access localStorage data of eskimo.dev .

What will work ? :

  1. If you were to have eskimo.dev/localStorage (where the localStorage is set in the browser) and
  2. now if you open eskimo.dev/localStorage/iframe (in which the iframe src points to eskimo.dev/localStorage, this page will have access to localStorage data.), why ?

Because, the domain origin is not changing, hence it can access the same localStorage.

Thanks

-3

It is a complete other domain, so there is no way access localStorage of the other domain.

But if you have control of both domains, you could use javascript events to share the localStorage through domains.

You can see more here: cross domain localstorage with javascript

Raúl C. Rivero
  • 307
  • 1
  • 4
  • 20