如何具有对同一产地iframe的独立sessionStorages [英] How to have separate sessionStorages for iframes on same origin

查看:347
本文介绍了如何具有对同一产地iframe的独立sessionStorages的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准的W3C标准说,大约localStorages:

The standard W3C standard says about localStorages:

不同的作者共享一个主机名,例如用户对托管 geocities.com 的内容,都有一个本地存储对象。没有任何功能限制由路径名访问。因此,在共享主机的作者们应避免使用这些功能,因为这将是微不足道的其他作者来读取数据,并覆盖它。

Different authors sharing one host name, for example users hosting content on geocities.com, all share one local storage object. There is no feature to restrict the access by pathname. Authors on shared hosts are therefore urged to avoid using these features, as it would be trivial for other authors to read the data and overwrite it.

但sessionStorages它说,它为标签和窗口从同一来源提供单独的sessionStorages均匀。

But for sessionStorages it's said that it does provide separate sessionStorages for tabs and windows even from same origin.

但似乎没有内部框架共享的sessionStorage。

But it seems iframes does share the sessionStorage.

有没有办法来对通过对同一产地iframe的独立sessionStorages。

Is there a way to have separate sessionStorages on via iframes on the same origin.

编辑:
由于当时的混乱,如果标签/窗口也有独立的sessionStorages,这里是一个示例页面。保存code在文件中,有两个不同的选项卡中打开它。再刷新一个选项卡5次,刷新了其他选项卡1次。你会看到,编号不同。

Since there was confusion if tabs/windows do have separate sessionStorages, here is a sample page. Save the code in a file and open it with two different tabs. Then refresh one tab 5 times and refresh the other tab 1 time. You'll see that the numbers differ.

<!DOCTYPE html>
<html>
<body>
    <div id="result"></div>
    <script>
        sessionStorage.setItem("counter", (parseInt(sessionStorage.getItem("counter"), 10) || 0 ) + 1);
        document.getElementById("result").innerHTML = sessionStorage.getItem("counter");
    </script>
</body>
</html>

EDIT2:我到目前为止尝试是使用的iframe 沙盒属性。但后来我得到了iframe中的错误和我上无法使用的sessionStorage的。我不得不添加沙盒=允许-同源。但随后的sessionStorage在所有内部框架一样了。

What I have tried so far is to use the iframe sandbox attribute. But then I got an error within the iframe and I cann't use sessionStorage at all. I had to add sandbox="allow-same-origin". But then the sessionStorage is the same in all iframes again.

先谢谢了。

推荐答案

<一个href=\"http://stackoverflow.com/questions/35065157/how-to-have-separate-sessionstorages-for-iframes-on-same-origin?noredirect=1#comment57867853_35065157\">@adeneo当说明是正确的。

储存容器被设置为原点的域,并从内部框架
  同一原点共享同一个对象,即使iFrame中打开一个新的
  会议

the storage container is set to the origin domain, and iframes from the same origin share the same object, even if the iFrame opens a new session

一个解决方法似乎并不当前存在的

A workaround does not appear to currently exist

<!DOCTYPE html>
<html>
<body>
    <iframe name="one" src="sessionStorageTest.html"></iframe>
    <iframe name="two" src="sessionStorageTest1.html"></iframe>
</body>
</html>


sessionStorageTest.html


sessionStorageTest.html

<!DOCTYPE html>
<html>
<body>
    <div id="result"></div>
    <script>
        sessionStorage.setItem("counter", (parseInt(sessionStorage.getItem("counter"), 10) || 0 ) + 1);
        document.getElementById("result").innerHTML = sessionStorage.getItem("counter");
    </script>
</body>
</html>


sessionStorageTest1.html


sessionStorageTest1.html

<!DOCTYPE html>
<html>
<body>
    <div id="result"></div>
    <script>
        var i = 0;
          while (++i < 10) {
            sessionStorage.setItem("counter", (parseInt(sessionStorage.getItem("counter"), 10) || 0 ) + 1);
            document.getElementById("result").innerHTML = sessionStorage.getItem("counter");
          }
    </script>
</body>
</html>

在plnkr http://plnkr.co/edit/Etel4ToABM6gWOw6qAE5?p=证明preVIEW

这篇关于如何具有对同一产地iframe的独立sessionStorages的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆