admin管理员组

文章数量:1318573

I want my parent window to have button that opens child window. Child window should have an input text box and a button(Named Copy). Whatever i enter text in child window textbox and click Copy button, the child window should close and entered name should appear on parent window.
I am trying below approach but can't understand how to retrieve enter value on parent.

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

I want my parent window to have button that opens child window. Child window should have an input text box and a button(Named Copy). Whatever i enter text in child window textbox and click Copy button, the child window should close and entered name should appear on parent window.
I am trying below approach but can't understand how to retrieve enter value on parent.

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");
}
Share Improve this question edited Oct 18, 2016 at 6:45 inaya asked Oct 17, 2016 at 7:27 inayainaya 971 gold badge2 silver badges8 bronze badges 3
  • Not sure but you can try window.parent or window.top – Rajesh Commented Oct 17, 2016 at 7:35
  • 1 Possible duplicate of Communication between tabs or windows – Pablo Lozano Commented Oct 17, 2016 at 7:55
  • I am not sure with syntax. can u please provide additional details – inaya Commented Oct 17, 2016 at 9:35
Add a ment  | 

1 Answer 1

Reset to default 6

Initially my approach was incorrect is i was trying to access child window textBox from parent window whereas i should be doing reverse. Below is the working code.

Parent Window

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

Child Window

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

本文标签: