Test File: AJAX + Close Window Firefox failure

This is a test file from mleiv.com.

Issue: When you try to close a window and use AJAX at the same time, there are failures and in one case Firefox crashes.

Tested in Firefox 1.504 07/06/2006

window.close() + ajax call:
ajax call + delayed window.close():
window.close() + delayed ajax call

Expected Behavior: You should see two alerts, one which says "Popup returned: PASSED" and one which says "AJAX returned: PASSED."

Click the first button above.
You will only get the first alert. This is a straightforward popup which calls a function in the parent page (this one) and then closes itself. The problem is when the parent page then issues an ajax call. Although this works in IE, in Firefox it issues some unreadable javascript error and refuses to reveal any more information about what went wrong. Upon some searching, I discovered Firefox's ajax cannot be called after any url event (like clicking a link and changing to the href url), so I am hypothesizing this behavior also extends to closing javascript-opened windows.

Click the second button above.
You will only get both alerts, but the window will not close. Here I changed it so the window.close() event happens *after* the ajax call (on the ajax return trip). Unfortunately, a second Firefox bug appears, where the javascript-opened window can no longer be closed at this point. In fact, if you lose focus of the popup and then try to close it yourself, Firefox will crash and all your windows will close. (Fun) I found a lot of people talking about this bug but no one actually said how they fixed it. (Helpful)

Click the third button above.
Success at last! Through pure trial and error I discovered this solution. I issued the window.close() first and then added a setTimeout of 100 to the ajax call. This nearly unnoticeable delay prevented the ajax/url event bug. But there is a caveat to this fix: You can't put the delay in the popup window's call to the parent function. This will prevent the window from closing. You must surrender all control back to the parent and let it go on from there.