Sending information from one window to another

Is there possible to send information from one window to another, like submitting values from a form in a popup window to its parent, while closing the popup AND refreshing the parent window?
stigsterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fargoCommented:
Hii,
The above can be achieved in the following way....suppose u opened up a popup..say "insertScreen.jsp" the action of this jsp is to insert.jsp
then in insert.jsp....u can carry on with the inserts to the database and then in insert.jsp use the following logic

<html>
<head>
<script language="Javascript">
function back()
{
      window.history.back();      
}
</script>
</head>
<body class="gpbody">
<table>
      <tr>
      <td >
      <%
            if(INSERT_FAILED){
                  out.println( "<span >Insert failed</span>" );%><br /><br /><%
            
            }else {%>
                  <script language="Javascript">
                  window.opener.document.headerform.submit();
                  window.close();      
            </script>
            <%}%>
            </td>
            </tr>
            <tr>
                  <td>
                  <form name=dummyform>
                  <input type="button" value="back" onClick="back()">
                  </form>
                  </td>
            </tr>
      </body>
</html>


I here assume that u have the parent window with some form named headerform....which is used to retrieve something from the database...even u don't have some logic like this....then u can use the logic of assigning the values to the parent window form elements...

like window.opener.document.form.elementname.value=whatever user newly assigned...

Hope this helps
regards
fargo
stigsterAuthor Commented:
Maybe I should have mentioned that I'm using a servlet in the 'action=' attribute field.
I want the popup  window to send its info to the servlet AND close itself AND refresh the parent window.

Think of it as a forum, where you are able to reply to any post. By clicking 'reply', a popupwindow appears with reference to which post it is a reply to.
The problem emerges when submitting the new entry - the servlet cannot do a 'response.redirect("")' because that would redirect the popup window.
Instead I want it to .close() and refresh the window from which i was opened. I recon this is some javaScript trick, but I'm not very in to that :)
abuimadCommented:
When you open a popup using the window.open function, any document that is loaded in that window can access the opener window by using window.opener, which gives you a reference to the frame that opened the popup (or if the opener is not inside of a frameset, you'll get a reference to the window itself).


Anyways, here what you do (I'm using jsp because it would be shorter but you can do the same with servlets:

[MessageView.jsp]
<!-- this is the main window where the user is looking at a message -->
....
<a href="javascript:openReply( <%=msgID%> );">Click here to reply to this message</a>

<script>
// you should put the script in the head but I put it here for clairty
function openReply( msgID )
{
      window.open( "enterReply.jsp?msgID=" + msgID ); // this opens your popup and passing the message ID
}
</script>

[enterReply.jsp]
<!-- Here the user enters the reply in a form -->
<form action="saveReply" > <!-- saveReply is your servlet -->
      <input type="hidden" name="msgID" value="<%=request.getParameter( "msgID" ) %>">
      Enter your reply here:
      <textarea name="replyText"></textarea>
      <input type="submit" vlue="Submit Reply">
</form>

[saveReply servlet]
public void doPost( ........ ) ....
{
       // save reply to DB using a function (or do it here):
       saveReplyInDB( request.getParamter("msgID") , request.getParamter("replyText");
       
       // write javascript to close popup and refresh screen:
       out.writeln( "<script>window.opener.location.reload( true );" ) // makes a hard-refresh to the opener
       out.writeln( "window.opner.focus(); self.close();</script>" ) // send focus to the opener window and close popup
}

Hope that helps...
Post back any questions

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
abuimadCommented:
damn vbscript.... I have been working in stupid ASP for too long....

The last two lines of the servlet should read "out.println(...)" instead of "out.writeln(..)"

Sorry about that  
abuimadCommented:
the last line should be window.opener instead of window.opner

Sorry again
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.