简体   繁体   English

通过 iframe 访问域的 localStorage

[英]Accessing localStorage for a domain via iframe

On example.com I have an iframe which loads anotherexample.com .example.com上,我有一个anotherexample.com加载了另一个 example.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 .我遇到的问题是,如果我直接(在 iframe 之外)加载anotherexample.com ,localStorage 不会与 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.通过访问https://eskimo.dev/localstorage/可以看到该问题的示例,然后访问https://eskimo.ooo/localstorage/iframe iframe 第一个链接。 The iframe isn't using the localStorage from the previous. iframe 没有使用之前的 localStorage。

Actually I tried, and it worked for me (I accessed first /iframe but it worked for me both ways):实际上我试过了,它对我有用(我先访问/iframe但它对我两种方式都有效):

Image from https://eskimo.ooo/localstorage/iframe图片来自 https://eskimo.ooo/localstorage/iframe

Image from https://eskimo.dev/localstorage/图片来自 https://eskimo.dev/localstorage/

Image from DevTools图片来自 DevTools

Of course, when I disabled all third party cookies it didn't work.当然,当我禁用所有第三方 cookies 时,它没有用。 Might that be the reason?这可能是原因吗?

I'm using chrome 107.0 by the way.顺便说一下,我使用的是 chrome 107.0。

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.但是通过 OP 中的上述链接示例。 The reason its not working there is:它在那里不起作用的原因是:

  1. There are 2 domains: eskimo.dev & eskimo.ooo (Both of these are different).有 2 个域:eskimo.dev 和 eskimo.ooo(两者不同)。 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".当第一个域 eskimo.dev 打开时,本地存储在浏览器中设置为参考 eskimo.dev 并且这将可访问来源为“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.但是当打开eskimo.ooo(将iframe src设置为eskimo.dev的域)时,localStorage不会被共享,因为原始域是eskimo.ooo,因此无法访问eskimo.dev的localStorage数据。

What will work?什么会起作用? : :

  1. If you were to have eskimo.dev/localStorage (where the localStorage is set in the browser) and如果你有eskimo.dev/localStorage(在浏览器中设置了localStorage)和
  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?现在如果你打开eskimo.dev/localStorage/iframe(其中iframe src指向eskimo.dev/localStorage,这个页面将可以访问localStorage数据。),为什么?

Because, the domain origin is not changing, hence it can access the same localStorage.因为,域源没有改变,因此它可以访问相同的 localStorage。

Thanks谢谢

It is a complete other domain, so there is no way access localStorage of the other domain.它是一个完整的其他域,因此无法访问其他域的localStorage。

But if you have control of both domains, you could use javascript events to share the localStorage through domains.但是,如果您可以控制这两个域,则可以使用 javascript 事件通过域共享 localStorage。

You can see more here: cross domain localstorage with javascript你可以在这里看到更多: cross domain localstorage with javascript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM