0

There are any number of questions on this topic out there, but I have not seen my situation addressed so I will describe my scenario.

I have a popup window. There are 2 ways you can close it: pressing a close button within the popup window or by pressing the x button in the upper right hand corner of the window. They have the exact same effect. If I could disable the x I would, but that is unnecessary and not happening anytime soon in your favorite browser. In essence, I am doing some post-processing when the window is closed (session clean-up actually) and am not spawning endless new popup windows to annoy my users. Trust me, the customer asked for this behavior.

So I could do window.onunload or I can do the body onunload and either way will give me the results I wish. However, there is a third scenario which complicates matters. Within the popup window, there can be a form submission which alters the contents of that popup window. If this is the case, either window.onunload or body unload will be invoked, depending upon which I have setup on my page. This is not what I want. I want the post-processing to happen upon the popup window visually disappearing from sight (x or close button), but not to post-process when a form submission has taken place leaving the popup still visually on this screen.

Can anyone speak to this scenario?

EDIT: My attempt at a solution which does not trigger window.onunload

    function searchFieldTitles()
{
    var searchOptionValue;
    var radioObj = document.getElementsByName("searchOption");
    var radioLength = radioObj.length;
    for(var i = 0; i < radioLength; i++) {
        if(radioObj[i].checked) {
            searchOptionValue = radioObj[i].value;
            break;
        }
    }

    $.get("/myroot/SearchFieldTitles.action", 
            {filterString: document.getElementsByName("filterString")[0].value, 
             searchOption: searchOptionValue, 
             filterBy: document.getElementsByName("filterBy")[0].value},
                  function(data) {                
                  });

      location.href="/myroot/jsp/field_titles/lookupFieldTitles.jsp";
}
demongolem
  • 9,474
  • 36
  • 90
  • 105
  • I am purposely keeping this question open. The portion of the question which I feel I need more information on is protecting against refresh. That is if a user initiates a refresh, I would not want the session cleanup I do to be called. I want a behavior which only responds to the X button, or my close button. This behavior should not be triggered by form submit (which the answer addresses and I am grateful for) or refresh. – demongolem Jun 24 '11 at 16:57

2 Answers2

0

It is a bit insane, but you can add a click-handler on every link (except for the close-button) and submit handler on every form submit in that popup that first disables the onunload handler and then continues with the default action. That way the user can submit forms and load new pages inside the popup without triggering the cleanup.

This does not help, however, if the user reloads the page i.e. with pressing the F5 key. You can add a keydown handler on the html element to disable the onunload handler if F5 or Ctrl-r is pressed, and if the popup has no reload widget from the browser, this seems to catch most page reload actions (only tested in Firefox however). If other browsers support other key combinations they would have to be added, too. I feel not very well recommending this, however, as capturing all key-events on the complete page makes me feel uneasy about what might go wrong, and also one need to maintain a list of browser depending key combination, which sounds like a maintenace hell. Better add a HTML button labelled "Click here to Refresh" to the page and hope your users are so friendly to use that button instead of F5.

Clemens Klein-Robbenhaar
  • 3,457
  • 1
  • 18
  • 27
0

You could use AJAX for the form submission. You can even receive a response and change the window contents with ever unloading the window.

js1568
  • 7,012
  • 2
  • 27
  • 47
  • Using the edited text, I have been able to get the basic functionality down. I am concerned though because if someone does a refresh within the popup window, it is curtains for my solution because the session info will be lost. Any way to protect against accidental/malicious refresh? – demongolem Jun 16 '11 at 22:17