繁体   English   中英

如何在不使用 sessionStorage 的情况下使用 javascript 将数据从子窗口返回到父窗口

[英]How to return data from child to parent window using javascript without using sessionStorage

我希望我的父窗口具有打开子窗口的按钮。 子窗口应该有一个输入文本框和一个按钮(命名副本)。 无论我在子窗口文本框中输入文本并单击“复制”按钮,子窗口都应该关闭并且输入的名称应该出现在父窗口上。
我正在尝试以下方法,但无法理解如何检索父级的输入值。

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");
}

最初我的方法不正确是我试图从父窗口访问子窗口 textBox 而我应该做相反的事情。 下面是工作代码。

父窗口

<!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>

暂无
暂无

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

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