简体   繁体   English

如果存储的数据来自iframe,localStorage或sessionStorage是否会在打开的标签中保留

[英]does localStorage either or sessionStorage persist in open tab if data stored comes from within an iframe

If I load a iFrame into a child element of a tab the user is in, for use by the user as a means of selecting something from an external location, and I were to store that data from the selection using local or session storage methods would I be able to access that data once I close out the iFrame to continue on? 如果我将iFrame加载到用户所在标签的子元素中,以供用户用作从外部位置选择内容的方式,那么我将使用本地或会话存储方法从选择内容中存储该数据关闭iFrame以继续操作后,是否可以访问该数据? Or even at all for that matter? 甚至根本没有这件事?

If not, is there still not a clean method of communicating child window to parent? 如果不是,是否还有一种干净的方法将子窗口与父窗口进行通信?

The following is a basic example of using localStorage between a parent window and an iFrame. 以下是在父窗口和iFrame之间使用localStorage的基本示例。

To demo, enter a value into the main page's textbox and click 'Set Data'. 要进行演示,请在主页的文本框中输入一个值,然后单击“设置数据”。 This will set the value in localStorage. 这将在localStorage中设置值。 Click 'Read Data' to output the value to the div. 单击“读取数据”以将值输出到div。

Then enter another value in the iFrame's textbox, and click 'Set Data'. 然后在iFrame的文本框中输入另一个值,然后单击“设置数据”。

Finally, click on 'Read Data' again and you will see the value set from the iFrame displayed in the main page's div. 最后,再次单击“读取数据”,您将看到iFrame设置的值显示在主页的div中。

main.html: main.html:

     <div id="divOne">
        <label>Enter a Value: </label><input id="txtOne" />
        <input type="button" id="btnOne" value="Set Data" onclick="btn1Click(this);" />
        <input type="button" id="btnTwo" value="Read Data" onclick="btn2Click(this);" />
    </div>
    <div id="divTwo"></div>
    <iframe id="ifOne" src="iframe.html"></iframe>

    <script type="text/javascript">
        var div2 = document.getElementById('divTwo');
        var btn1 = document.getElementById('btnOne');
        var btn2 = document.getElementById('btnTwo');
        var input1 = document.getElementById('txtOne');

        function btn1Click(e)
        {
            localStorage.dataItem = input1.value;
        }

        function btn2Click(e) {
            div2.innerHTML = localStorage.dataItem;
        }
    </script>

iframe.html: iframe.html:

    <div>
        <label>Enter a different Value: </label><input id="txtOne" />
        <input type="button" id="btnOne" value="Set Data" onclick="btn1Click(this);" />
    </div>

    <script type="text/javascript">
        var btn1 = document.getElementById('btnOne');
        var input1 = document.getElementById('txtOne');

        function btn1Click(e)
        {
            localStorage.dataItem = input1.value;
        }
    </script>

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

相关问题 本地存储或会话存储无法在跨浏览器上保留数据 - localstorage or sessionstorage can't persist data on cross browser localStorage不会在刷新时持久保存数据 - localStorage does not persist data on refresh 使用 Javascript,我试图将数据保存在浏览器的 SessionStorage 和 localStorage 中,但刷新页面后数据丢失 - Using Javascript, I am trying to persist the data in SessionStorage and localStorage of browser but after refreshing the page the data is lost sessionStorage数据存储在哪里? - Where is sessionStorage data stored? LocalStorage 不持久 - LocalStorage does not persist 在同一选项卡中打开 iframe 中的链接 - open a link within an iframe in the same tab 无法扩充 SessionStorage 数据; 根据通货膨胀方法获得“不正确的标头检查”或“无效的存储块长度” - Unable to inflate SessionStorage data; getting either 'incorrect header check' or 'invalid stored block lengths' depending on the inflation method iframe窗口中的链接在新选项卡中打开 - links within an iframe window to open in new tab localStorage或sessionStorage是否会影响网站的可访问性? - Does localStorage or sessionStorage affect Website accessibility? 测试localStorage / sessionStorage最大数据量 - Testing localStorage/sessionStorage max data amount
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM