views:

462

answers:

3

Given the following flow:

1) User clicks on a link, opens a popup window. 2) User does some things, clicks on a button to save changes. 3) Using AJAX, some data is POSTed. 4) In the AJAX response handler, the popup is closed and the main window refreshed.

There is a problem in FireFox 2, where closing the popup within a response handler fails (until some user activity, such as mouse movement, is performed on the window).

Sample code (popup window only):

function ajax_cb()
{
    window.close();
}

// Send a POST request, ajax_cb will be called when readystate == 4.
function test_ajax_close()
{
    Request.sendPOST("file:///test.html", "", ajax_cb);
}

In the above sample, the window will eventually close, but first requires user interaction. According to this link:

Firefox gets unhappy if it still has a socket open doing an async AJAX request and you attempt to do a window.close().

A: 

One solution I came up with is to poll some variable externally and close the window when it changes. I'm looking for a better solution, but the sample code is:

var response_received = 0;

function ajax_cb()
{
    response_received = 1;
}

function monitor_response()
{
    if (response_received)
    {
        self.close();
        return;
    }

    setTimeout("monitor_response()", 100);
}

function test_ajax_close()
{
    Request.sendPOST("file:///test.html", "", ajax_cb);
    monitor_response();
}
jthompson
A: 

I would not use a regular Window for this. I would use an Iframe inside a div, simulating a window, having total control over it.

José Leal
+1  A: 

just wrap your close in a short timeout..

setTimeout(window.close, 100);

It shoould be enough time for the socket to finish closing, and the poopup to self-close.

Tracker1
Yeah, I originally thought this didn't work, but tried it a bit later on yesterday and it solved the problem.
jthompson