468,765 Members | 1,698 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

Parent to child back to parent

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,menuba r=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?
Jul 23 '05 #1
2 1295
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,menuba r=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>

Jul 23 '05 #2
Adrian MacNair wrote:
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,menuba r=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>

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Goran Djuranovic | last post: by
4 posts views Thread by Richard Lewis Haggard | last post: by
8 posts views Thread by Rick | last post: by
1 post views Thread by Richard | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.