A Handout
Lem: "I got fired from my job as a bank guard."
Clem: "That's awful. What happened?"
Lem: "Well a thief came in to rob a bank. I drew my gun. I told him that if he
took one more step, I'd let him have it."
Clem: "What did thief do then?"
Lem: "He took one more step so I let him have it. I didn't want that stupid gun
anyhow!"
Passing Data from Parent to Child Form
Passing information back and forth from the child window to the parent window is
rather easy once you learn how to reference the window object. This will allow you
to use a pop up window to grab information from a database, calendar, etc.
You most likely know to open a pop up window you need to use window.open. If you
have trouble making pop up windows, you can always use my generator here. When
you open the window you need to set it to a variable so the object is stored. For
example:
<html>
<head>
<title>Test</title>
<script type="text/javascript">
function OpenWindow(){
var winPop = window.open("testPop.html","winPop");
}
</script>
</head>
<body>
<form name="form1">
<input type="text" name="text1">
<input type="button" name="button" value="Grab Info"
onclick="OpenWindow()">
</form>
</body>
</html>
You will need this when you want to pass the information to the child (pop up)
window. In the example above note that I have also added a form with a text field
and a button. The text field will be filled in via the pop up window.
To do this you need to write some code in the pop up window. You can reference the parent window (the one that opened the pop up) with the window object reference window.opener. Then you just reference the document form filed like you normally do.
<html>
<head>
<title>Test</title>
<script type="text/javascript">
function SendInfo(){
var txtVal = document.formPop.textPop.value;
window.opener.document.form1.text1.value = txtVal;
window.close();
}
</script>
</head>
<body>
<form name="formPop">
<input type="text" name="textPop">
<input type="button" name="button" value="Send Info"
onclick="SendInfo()">
</form>
</body>
</html>
As you can see I also added window.close to close the pop up window. FYI: You can
not do window.opener.close() without a security warning. That is there so people
do not transfer you away to a site you do not want to go to. There are some ways
to get around that with some browsers, but I am talking about that here.
Now you can pass data from the parent to the child by using the window object reference along with the form name. For example:
winPop.document.formPop.textPop.value = variableData;Now if you want to pass the information as soon as the window opens, you have a slight problem. If you do it right after the window.open statement, you get an error. Reason, the page has not rendered the form elements yet. Therefore you need to add a pause, you can do that with a simple setTimeout.
<html>
<head>
<title>Test</title>
<script type="text/javascript">
var winPop;
function OpenWindow(){
winPop = window.open("testPop.html","winPop");
var theDate = new Date();
setTimeout("SendToChild('" + theDate.toLocaleString() + "')",10);
}
function SendToChild(data){
winPop.document.formPop.textPop.value = data;
}
</script>
</head>
<body>
<form name="form1">
<input type="text" name="text1">
<input type="button" name="button" value="Grab Info"
onclick="OpenWindow()">
</form>
</body>
</html>
Now you may say, I want to pass data from the parent to the child so the child
page's server side code can use it. If this is the case, you would need to pass it
with a query string.
Eric Pascarello
Moderator of HTML/JavaScript at www.JavaRanch.com
Author of: JavaScript: Your Visual Blueprint for Dynamic Web Pages
<html> <head> <title>Test</title> <script type="text/javascript"> function OpenWindow(){ var winPop = window.open("testPop.htm","winPop"); } </script> </head> <body> <form name="form1"> <input type="text" name="text1"> <input type="button" name="button" value="Grab Info" onclick="OpenWindow()"> <table border="1" width="15%" id="table1"> <tr> <td> <FONT FACE="sans-serif"> <SELECT NAME="PICKED" SIZE="9""> <option>Item1</option> <option>Item3</option> <option>Item2</option> </SELECT></FONT></td> </tr> </table> </form> </body> </html> and my pop-up window code is like this: <html> <head> <title>Test</title> <script type="text/javascript"> function SendInfo(){ var txtVal = document.formPop.textPop.value; window.opener.document.form1.PICKED.value = txtVal; } </script> </head> <body> <form name="formPop"> <input type="text" name="textPop"> <input type="button" name="button" value="Send Info" onclick="SendInfo()"> </form> </body> </html> any help would be highly appreciated. tnx!
var sel = window.opener.document.form1.PICKED;
sel.options[sel.options.length] = new Option("text","value");
Eric
You can than close the child window with window.close(); Eric
<u>Main Page:</u>
<html>
<head>
<script type="text/javascript">
var objActiveElem;
var winPop;
function GetImage(xElem){
objActiveElem = xElem;
winPop = window.open("imagePicker.html");
winPop.focus();
}
</script>
</head>
<body>
<form name="Form1">
<input type="text" name="txt1">
<input type="button" name="btn1" value="Get Value" onclick="GetImage(document.Form1.txt1)">
</form>
</body>
</html>
<u>PopUp Page:</u>
<html>
<head>
<script type="text/javascript">
function SetImage(xValue){
window.opener.objActiveElem.value = xValue;
window.opener.objActiveElem = "";
self.close();
}
window.onunload = function(){
window.opener.objActiveElem = "";
}
</script>
</head>
<body>
<form name="Form1">
<img id="img1" src="man.gif" onclick="SetImage(document.getElementById('spanImg1').innerHTML)"/><span
id="spanImg1">Pass Back Span Text</span><br/>
<img id="img2" src="man2.gif" alt="Passed alt text" onclick="SetImage(this.alt)"/>
</form>
</body>
</html>
Eric
onclick="GetImage(document.getElementById('imgId')"
and
onclick="SetImage(this.src)"
and
window.opener.objActiveElem.src = xValue;
Eric
JavaScript can not talk across domains for security reasons. EricI glad I found this answer you wrote b4
I drove myself crazy trying to open a page
on another domain and pass a value to it's
lookup engine. the other is a frame based
layout so the whole page fails to load only
the results page loads. oh well back to the
old code.
Hi, Eric!
Thanks so far, for what you've given me! ;)
I kind of have the same problem as Mr. Anonymus got. And, I as well, are one hell of a newbee in this javascript-world.
So, My problem is; I have several images in a db, and these are printed as a thumb together with a radio-button. The user should now check the radio belonging to the wanted image. This worked fine when I only had 1 image in my db, but when I added som more, the script seems to be unable to collect the id-info from the selected radio-button.
The result is; "undefined". And that kind of sucks.. ;)
Thanks again!!
window.onload = function(){
document.forms[0].YourTextboxName.value = window.opener.location.href;
}
Eric