Parent to child back to parent

A

Adrian MacNair

I have a question in the child and I need to pass the answer back to the
parent:

var go = 0
var mywin = null;
opts = 'width=300,height=300,status=yes,toolbar=no,menubar=no,location=no'
mywin = window.open('','mywin',opts)
mywin.document.open()
mywin.document.write('<html><head><title>My Title</title>'
+'<script type="text/javascript">'
+'function sayyes() {'
+'go=1;'
+'window.close()'
+'}'
+'</script>'
+'</head><body>'
+'<p>Do you want to go or not?</p>'
+'<a href="#" onClick="sayyes()">Yes I do</a><br>'
+'<a href="#" onClick="window.close()">No I do not</a>'
+'</body></html>')
mywin.document.close()
if (go==1) {
do stuff
}

My problem is that the parent thinks var go is still 0 and doesn't get set
to 1. Any ideas?
 
S

SEvans

There are several ways of doing it. Try this by saving the code to an
..HTML file.

<script>
var go = 0
function shazam() {
alert(go);
}
function popWin () {
var mywin = null;
opts =
'width=300,height=300,status=yes,toolbar=no,menubar=no,location=no'
mywin = window.open('','mywin',opts)
mywin.document.open()
mywin.document.write('<html><head><title>My Title</title>' +
'<script type="text/javascript">' +
'function sayyes(answer) {' +
'opener.go=answer;' +
'opener.shazam();' +
'window.close();' +
'}')
mywin.document.write('</'+'script>')
mywin.document.write('</head><body>' +
'<p>Do you want to go or not?</p>' +
'<a href="#" onClick="sayyes(1)">Yes I do</a><br>' +
'<a href="#" onClick="sayyes(0)">No I do not</a>' +
'</body></html>')
mywin.document.close()
}
</script>
<a href="javascript:popWin();">PopWin</a>
 
R

Random

Adrian said:
I have a question in the child and I need to pass the answer back to the
parent:

var go = 0
var mywin = null;
opts = 'width=300,height=300,status=yes,toolbar=no,menubar=no,location=no'
mywin = window.open('','mywin',opts)
mywin.document.open()
mywin.document.write('<html><head><title>My Title</title>'
+'<script type="text/javascript">'
+'function sayyes() {'
+'go=1;'
+'window.close()'
+'}'
+'</script>'
+'</head><body>'
+'<p>Do you want to go or not?</p>'
+'<a href="#" onClick="sayyes()">Yes I do</a><br>'
+'<a href="#" onClick="window.close()">No I do not</a>'
+'</body></html>')
mywin.document.close()
if (go==1) {
do stuff
}

My problem is that the parent thinks var go is still 0 and doesn't get set
to 1. Any ideas?


The parent is right-- you're not waiting for an answer. That, and your
popup isn't talking to the parent.

How about an answer() function that does your if( go ) logic?



In the parent:
function answer( go ) {
if( go ) {
// go do stuff
}
}


In the popup:
function answer( go ) {
// do some tests to find out if the parent is still
// relevant to this document, then...
window.opener.answer( go );
self.close();
return false;
}


<a href=# onclick=return(answer(true))>Sure, let's go</a>
<a href=# onclick=return(answer(no))>Nah... let's wait</a>
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top