• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling a JavaScript function after the completion of a Struts action

 
J Chung
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I'm trying to do:

Window A displays a list of objects, and window B is the detail page for an object that the user selects. The user is able to edit the detail page and to save his changes. When the user clicks save, I want window B to close and window A to refresh.

The save button on window B submits the form to a save action. I tried including an onclick event on this button to call the following JavaScript...

opener.location.reload(true);
window.close();

...but it appears that doing so prevents the action from being called, so the detail page doesn't save.

I'd appreciate any ideas. Thanks.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you need to perform some javaScript tasks when you submit the form for Window B, I'd suggest changing your <html:submit> to an <html:button>. Then include submitting the form as part of your onclick tasks (this.form.submit()).

I've had problems, though, with closing the window immediately after submitting a form. Sometimes the form doesn't get submitted before the window closes. There are two ways to handle this. One is to use the setTimeoutValue() javascript method to wait a second or two before submitting. The other is not to close the window at all in the submitting JSP, and to have the action forward to a page that does nothing but close the window in the onload method of the <body> tag.
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my project, we use the second approach mentioned by Merrill ("have the action forward to a page that does nothing but close the window in the onload method of the <body> tag"). This also give you the ability to display validation error messages on the popup window.

- Brent
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just thought of something else on this problem: If the action for Window B puts something in the HttpSession, and you're expecting the document in Window A to show that change when it's refreshed, you probably want to use the second method of closing the window, and also refresh Window A from the forwarded-to JSP, rather than from the submitting JSP.

If you do it this way, you are assured that the action that puts something in the session has completed before Window A gets refreshed. Otherwise, you have no guarantees that Window A will show the new data when it gets refreshed.

so, you'd have something like this

<body on load="opener.location.reload(true);window.close()">
 
J Chung
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies. Calling the opener.location.reload from the Window B JSP (as part of an onlick event for the save button) appears to refresh Window A with the updated information as desired.

In regard to your comments related to the session, wouldn't making this JavaScript call from the forwarded-to JSP--i.e., the JSP that closes itself upon loading--refresh Window B instead of Window A?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic