2016-10-17 3 views
1

親ウィンドウに子ウィンドウを開くボタンがあるようにします。子ウィンドウには、入力テキストボックスとボタン(名前付きコピー)が必要です。子ウィンドウのテキストボックスにテキストを入力して[コピー]ボタンをクリックすると、子ウィンドウは閉じられ、入力された名前は親ウィンドウに表示されます。
私は以下のアプローチを試していますが、親の入力値を取り出す方法を理解できません。sessionStorageを使用せずにjavascriptを使用して子ウィンドウから親ウィンドウにデータを返す方法

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

わからないが、あなたは、 'window.parent'を試すか、[タブやウィンドウ間の通信](HTTPの – Rajesh

+1

可能な複製をwindow.topことができます。 //stackoverflow.com/questions/28230845/communication-between-tabs-or-windows) –

+0

構文がわかりません。追加の詳細をお知らせください。 – inaya

答えて

0

は、最初は私のアプローチは間違っていた私は逆にやるべきことに対し、親ウィンドウから子ウィンドウテキストボックスにアクセスしようとしていたです。以下は作業コードです。

親ウィンドウ

<!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> 
関連する問題