Entry
How to pass data back and forth between two windows?
How to pass data back and forth between forms in two different windows?
Feb 13th, 2000 04:49
Martin Honnen,
You often popup a window to allow the user some (further) data
input/selection which you want to pass back to the opening window. A
window opened with
var w = window.open('url', 'windowName', 'featureString')
can refer back to the opening window with the
opener
object which then allows to exchange data. The following example sets
up a form field as the argument for a popup which then passes data back
to the field:
Main window:
<HTML>
<HEAD>
<SCRIPT>
var dialogArguments;
function getValue (field) {
dialogArguments = field;
open ('aDialog.html', 'popup',
'width=250,height=100,scrollbars=1');
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="aForm">
<INPUT TYPE="text" NAME="aText">
<INPUT TYPE="button" VALUE="select text"
ONCLICK="getValue(this.form.aText)"
>
</FORM>
</BODY>
</HTML>
aDialog.html:
<HTML>
<HEAD>
</HEAD>
<BODY>
<TABLE WIDTH="100%" HEIGHT="100%">
<TR>
<TD ALIGN="center" VALIGN="middle">
<FORM NAME="aForm">
<SELECT NAME="gods"
ONCHANGE="window.opener.dialogArguments.value = this.options
[this.selectedIndex].value; window.close()">
<SCRIPT>
var html = '';
html += '<OPTION SELECTED>Please select text for field ' +
window.opener.dialogArguments.name;
document.write(html);
</SCRIPT>
<OPTION VALUE="Kibology">Kibology
<OPTION VALUE="Scriptology">Scriptology
</SELECT>
</FORM></TD>
</TR>
</TABLE>
</BODY>
</HTML>