简体   繁体   English

iframe 中的 sessionStorage

[英]sessionStorage in iframe

I'm going to have several iframes on my page and I'm going to quite intensively use sessionStorage inside them.我将在我的页面上有几个 iframe,并且我将在其中大量使用 sessionStorage。 What I'm curious about is if I will have separate storages or one shared for all iframes?我很好奇的是我是否会有单独的存储或所有 iframe 共享一个? How do the size limits apply?大小限制如何适用?

If sessionStorage is shared depends on the iframe's page and it's origin, which is the domain part of the URL. sessionStorage 是否共享取决于 iframe 的页面及其来源,即 URL 的域部分。 If you have a webpage at http://myserver/test.html and it is including http://thatserver/some.html via an iframe, the iframe's page has the domain thatserver .如果您的网页位于http://myserver/test.html并且它通过 iframe 包含http://thatserver/some.html ,则 iframe 的页面具有域thatserver Thus the origin differs and the sessionStorage won't be shared.因此来源不同并且 sessionStorage 不会被共享。 But if the iframe's page is http://myserver/some.html it has the same origin and therefore will share the same session storage.但是如果 iframe 的页面是http://myserver/some.html它具有相同的来源,因此将共享相同的会话存储。

Now there is an additional trick: The sandbox attribute for the iframe.现在还有一个技巧:iframe 的沙箱属性。 If you write <iframe sandbox> without the value allow-same-origin the content of the iframe gets a unique origin.如果您编写没有值allow-same-origin<iframe sandbox> ,则 iframe 的内容将获得唯一的来源。 That means it would get a different sessionStorage regardless of the real origin that page has.这意味着无论页面的真实来源如何,它都会获得不同的 sessionStorage。 You can write <iframe sandbox="allow-same-origin"> to sandbox the content AND let the content of the iframe to have the same origin (but only if if does have the real same origin).您可以编写<iframe sandbox="allow-same-origin">来沙箱内容并让 iframe 的内容具有相同的来源(但前提是确实具有相同的来源)。

Now special notes: sandboxed iframes won't support localStorage per spec .现在特别注意:沙盒 iframe 将不支持每个规范的localStorage 。 And in webkit-browsers and mozilla firefox an exception will be thrown if the sandboxed iframe content will try to access sessionStorage.在 webkit 浏览器和 mozilla firefox 中,如果沙盒 iframe 内容尝试访问 sessionStorage,则会抛出异常。

OK, I've made a test myself.好的,我自己做了一个测试。 At least in Chrome (44) and Firefox (40) the sessionStorage is shared among page and the iframes included if they are of the same domain and do not if they are of different domains.至少在 Chrome (44) 和 Firefox (40) 中, sessionStorage 在页面和包含的 iframe 之间共享,如果它们属于同一域,则不会在它们属于不同域的情况下共享。

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

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