Closing popup in Firefox 2 in AJAX response handler

Given the following thread:

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

There is an issue in FireFox 2 where closing the popup in the response handler fails (unless there is some user activity in the window such as mouse movement).

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 example, the window eventually closes, but first requires user interaction. According to this link :

Firefox gets annoyed if it still has an open socket making an async AJAX request and you try to do window.close ().

0


a source to share


3 answers


just wrap the closure in a short amount of time.

setTimeout(window.close, 100);

      



Enough time to complete closing the socket, and poopup to close itself.

+1


a source


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



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();
}

      

0


a source


I wouldn't use a regular window for this. I would use an iframe inside a div, simulating a window, having full control over it.

0


a source







All Articles