Passing values between parent and child windows - Beginner question

E

evrimagha

Hello JS newsgroup


The main window of my Website opens a child window as you can see
below:

<script language="JavaScript">

window.name='Iamparentwindow'

function openwindow()
{
var
child2=window.open("Test.htm","childwin","height=350,width=280");
child2.focus();
child2.document.childform.text1.value =
document.parentform.testtext2.value;

}
</script>


I have some questions

1. the 3rd line of function openwindow is generating errors- Could you
please help me fix that ?

2. In the child window, I have used
opener.document.parentform.testtext1.value =
document.childform.text2.value;

which is working fine. But I would like to use the name of opener
window, "Iamparentwindow" instead.
Could you please help me?

Thank you in advance
Evrim
 
S

Stevo

var child2=window.open("Test.htm","childwin","height=350,width=280");
child2.focus();
child2.document.childform.text1.value = document.parentform.testtext2.value;
I have some questions

1. the 3rd line of function openwindow is generating errors- Could you
please help me fix that ?

You're trying to immediately access the document of the child window
before the browser has probably even got around to opening it. It
certainly won't have loaded Test.htm into it and rendered the page
completely. window.open is NOT a blocking call (i.e. your javascript
doesn't wait for it to have loaded Test.htm into it before it comes back
and executes line 3 of your code.

I think you're going to have to split your function into two parts and
have the second part wait for the page to have loaded. You could poll it
(check every so often to see if that childform exists yet) or the child
could make a call into the parent indicating it's ready and waiting.
2. In the child window, I have used
opener.document.parentform.testtext1.value =
document.childform.text2.value;

which is working fine. But I would like to use the name of opener
window, "Iamparentwindow" instead.
Could you please help me?

How about this?

var Iamparentwindow = opener.document;
Iamparentwindow.parentform.~~~~
 
R

Richard Cornford

The main window of my Website opens a child window as
you can see below:

No. It is a beginner's mistake to assume that attempting to open a new
browser window will result in a new browser window being opened. Your
action only MAY open a new browser window, and you will not achieve a
reliable outcome until you design out any supposed need to open new
browser windows.
<script language="JavaScript">

window.name='Iamparentwindow'

function openwindow()
{
var
child2=window.open("Test.htm","childwin","height=350,width=280");
child2.focus();
child2.document.childform.text1.value =
document.parentform.testtext2.value;

}
</script>


I have some questions

1. the 3rd line of function

The way you have wrapped the code in your posting (and it was you not
your newsreader as the ultimate control is yours) has made it unclear as
to which line is (or was) the third.
openwindow is generating errors-
Could you please help me fix that ?

Yes, the newly opened window (if it opens at all) will request a web
page from the server, but receiving and processing whatever the server
sends back in response to that request will take an unpredictable (but
non-zero) amount of time. Do not attempt to interact with any document
supposed to be contained within that window until you have positive
verification that the document has arrived from the server and been
processed by the browser.
2. In the child window, I have used
opener.document.parentform.testtext1.value =
document.childform.text2.value;

which is working fine. But I would like to use the
name of opener window, "Iamparentwindow" instead.
Could you please help me?

Yes, stop wanting that. Then you will no longer care that it isn't going
to be possible.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top