Parent to child back to parent

Discussion in 'Javascript' started by Adrian MacNair, May 26, 2005.

  1. 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?
    Adrian MacNair, May 26, 2005
    #1
    1. Advertising

  2. Adrian  MacNair

    SEvans Guest

    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>
    SEvans, May 26, 2005
    #2
    1. Advertising

  3. Adrian  MacNair

    Random Guest

    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,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>
    Random, May 26, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mark
    Replies:
    1
    Views:
    560
  2. Jeff Rodriguez
    Replies:
    23
    Views:
    1,093
    David Schwartz
    Dec 9, 2003
  3. Noel Dolan
    Replies:
    0
    Views:
    218
    Noel Dolan
    Jul 18, 2004
  4. Jason Carlton
    Replies:
    4
    Views:
    158
    Thomas 'PointedEars' Lahn
    Dec 16, 2009
  5. Bitswapper
    Replies:
    5
    Views:
    119
    Prasad, Ramit
    Aug 27, 2013
Loading...

Share This Page