Friday, July 31, 2009

Passing value from child window to parent window in asp or javascript?

i have a parent window that opens a child window when a button is clicked. How do i pass the value from child window to parent window when the child window is closed? the value will be passed to a drop down menu in the parent window.the language i use is asp. thanks

Passing value from child window to parent window in asp or javascript?
Inter-window communication is best accomplished via JavaScript. Here's a sample that goes either way, i.e., parent-to-child or child-to-parent. Put the two files in the same folder and open the parent.html.





parent.html


~~~~~~~~~~~~~


%26lt;html%26gt;





%26lt;head%26gt;


%26lt;style%26gt;


input.msgInput { width: 40em; }


%26lt;/style%26gt;


%26lt;script%26gt;


var childWin;





function sendToChild() {


var msg = document.getElementById('msgText').value...


var target = childWin.document.getElementById("msgTar...


target.innerHTML = msg;


}





function openChildWin() {


childWin = window.open('child.html');


}


%26lt;/script%26gt;


%26lt;/head%26gt;





%26lt;body%26gt;


%26lt;form%26gt;


%26lt;input class="msgInput" type="text"


value="type msg to child window here"


id="msgText" /%26gt;







%26lt;input type="button"


value="click to send message"


onclick="sendToChild();" /%26gt;







%26lt;input type="button"


value="click to open child window"


onclick="openChildWin();" /%26gt;









%26lt;div id="msgTarget"%26gt;


messages from child will go here


%26lt;/div%26gt;


%26lt;/form%26gt;


%26lt;/body%26gt;





%26lt;/html%26gt;





---------------------


child.html


~~~~~~~~~~~~~~~


%26lt;html%26gt;





%26lt;head%26gt;


%26lt;style%26gt;


input.msgInput { width: 40em; }


%26lt;/style%26gt;





%26lt;script%26gt;


var parentWin = window.opener;





function sendToParent() {


var msg = document.getElementById('msgText').value...


var target = parentWin.document.getElementById("msgTa...


target.innerHTML = msg;


}


%26lt;/script%26gt;


%26lt;/head%26gt;





%26lt;body%26gt;


%26lt;form%26gt;


%26lt;input class="msgInput" type="text"


value="type msg to parent window here"


id="msgText" /%26gt;







%26lt;input type="button"


value="click to send message"


onclick="sendToParent();" /%26gt;







%26lt;div id="msgTarget"%26gt;


messages from parent will go here


%26lt;/div%26gt;


%26lt;/form%26gt;


%26lt;/body%26gt;





%26lt;/html%26gt;





--------------------


To pass from child to parent when the child is closed, register the passing function as the handler for the child's onclose event...





%26lt;body onclose="sendToParent();"%26gt;





Tailor the sendToParent() for your case.
Reply:try the following


window.opener.document





from the child windo that gets you access to the parent


No comments:

Post a Comment