如何使用javascript从子窗口返回数据到父窗口,而不使用sessionStorage
How to return data from child to parent window using javascript without using sessionStorage
我想让我的父窗口有打开子窗口的按钮。子窗口应该有一个输入文本框和一个按钮(名为Copy)。无论我在子窗口文本框中输入什么文本,然后点击复制按钮,子窗口应该关闭,输入的名称应该出现在父窗口。
我正在尝试下面的方法,但无法理解如何检索父上的输入值。
p>Click the button to create a window and then display the entered name on parent window.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var myWindow = window.open("", "MsgWindow", "width=200,height=200");
myWindow.document.write("<p>This window's name is: " + myWindow.name + "</p>");
myWindow.document.write("<br/>");
myWindow.document.write("<input type='text' id='txtId' />");
myWindow.document.write("<input type='button' value='Copy'/>");
var x = localStorage.getItem(Name);
}
myWindow.opener.document.write("Landed to prent");
}
最初我的方法是不正确的,我试图从父窗口访问子窗口文本框,而我应该做反向。下面是工作代码:
父窗口
<!DOCTYPE html>
<html>
<body>
<input type="text" id="txtName" readonly="readonly" />
<button onclick="myFunction()">Open</button>
<script>
function myFunction() {
var win = window.open("ChildWin.htm", "_blank", "width=200,height=200");
}
</script>
</body>
</html>
子窗口
<!DOCTYPE html>
<html>
<body>
<p>Enter name </p>
<input type="text" id="txtbx" />
<br/><br/>
<button onclick="copyFunc()">Copy</button>
<script>
function copyFunc() {
if (window.opener != null && !window.opener.closed) {
var txtName = window.opener.document.getElementById("txtName");
txtName.value = document.getElementById("txtbx").value;
}
window.close();
}
</script>
</body>
</html>
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 防止Iframe窗体在新窗口中打开
- 内部分区字体大小获胜'调整浏览器窗口大小时不会随媒体查询而更改
- 调整窗口大小时,可拖动的对象会出现在容器外部
- Javascript排序的图像弹出窗口..可以't单独弹出
- 窗口大小html css
- 弹出窗口出现,然后退出
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- Javascript更新孙窗口中的表单元素
- 如何在选项卡上定义属性'的主窗口对象
- CSS-若窗口太小,滚动条会出现在“表格”单元格上
- 召回窗口加载事件 - javascript
- 为什么不显示警报窗口
- 如何使用modalDialog来显示可以在所有浏览器中工作的弹出窗口
- Chrome应用程序调整窗口大小保持纵横比
- 新选项卡被弹出窗口阻止程序阻止
- 如何访问UIWebView'的子窗口上下文
- 刷新父窗口后无法关闭窗口
- 自动关闭弹出窗口的Javascript
- 如何使用特定大小的浏览器窗口打开我的页面