Window communication with popup

Discussion in 'JavaScript' started by coolecho, Apr 13, 2010.

  1. #1
    Hello,

    I am new to Javascript and am trying out window communication with a popup.
    In the main window it opens up a popup window with a button click. The popup window is trying to grab text from the main window and put it in it's (the popup's) window. I guess it needs a test to see if the window is ready??

    Here's the code:

    <html>
    <head>
    <title>Main</title>
    </head>
    <body>
    <script language="JavaScript" type="text/javascript">

    var newWindow;
    function butOpenWindow()
    {
    newWindow = window.open('newindow.html','DS_window','width=300,height=300,resizable=yes,scrollbars=yes');
    }

    </script>
    <form name=form1>
    <br>
    Main window<br>
    Input text<br>
    <input type="text" name="text1">
    <br><br>
    <input type="button" value="Open Popup" name=butOpenWin
    onclick="butOpenWindow()"> <br><br>
    </form>
    </body>
    </html>

    The Popup:
    <html>
    <head>
    <title>Popup Window/title>
    </head>
    <body>
    <script language="JavaScript" type="text/javascript">

    document.form2.text2.value = window.opener.document.form1.text1.value;

    </script
    <form name=form2>
    <br>
    Main Window text:<br>
    <input type="text" name=text2>
    <br>
    </form>
    </body>
    </html>

    Thank You,
     
    coolecho, Apr 13, 2010 IP
  2. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this:

    <html>
    <head>
    <title>Main</title>
    </head>
    <body>
    <script language="JavaScript" type="text/javascript">
    
    var newWindow;
    function butOpenWindow()
    {
    newWindow = window.open('newindow.html','DS_window','width=300,height=300,resizable=yes,scrollbars=yes');
    }
    
    </script>
    <form name="form1">
    <br/>
    Main window<br/>
    Input text<br/>
    <input type="text" name="text1">
    <br/><br/>
    <input type="button" value="Open Popup" name="butOpenWin"
    onclick="butOpenWindow()"> <br/><br/>
    </form>
    </body>
    </html>
    
    The Popup:
    <html>
    <head>
    <title>Popup Window</title>
    </head>
    <body>
    <script language="JavaScript" type="text/javascript">
    
    if(document.getElementById('text2')){ document.getElementById('text2').value = window.opener.document.form1.text1.value; }
    
    </script>
    <form name=form2>
    <br/>
    Main Window text:<br/>
    <input type="text" name="text2" id="text2">
    <br/>
    </form>
    </body>
    </html>
    Code (markup):
     
    Imozeb, Apr 13, 2010 IP
  3. coolecho

    coolecho Member

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    Hi,

    I tried it with Firefox 3.5.9 and it doesn't put the main window text into the popup box.
    Someone else suggested:

    <script type="text/javascript">
    var item= opener.document.getElementById("text1").value;
    </script>
    .
    .
    .
    <td colspan="2">Main window text <script type="text/javascript">document.write(item);</script> </td>

    Which does work.

    Thanks
     
    coolecho, Apr 13, 2010 IP