[英]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.