Tuesday, June 13, 2006

Firefox AJAX Bug - Plus Workaround

Today we stumbled across a fairly significant firefox bug with popup windows and making XHR requests. Here is the scenario:

We have a calendar selection popup window for our users to select a date (typical of any reservation website). User clicks calendar icon, calendar select popup appears, user selects date, popup window closes & fills text field with date.

Today we wanted to add an AJAX call triggered just as the popup window closes. This call would request filtered data for another field based on the date selected. The AJAX call (via DWR) was working fine when we didn't use the calendar popup, but when using the popup we would get "No data received from server" from DWR.

Luckily we found a reference to the firefox bug on the dwr mailing list.

Basically, Firefox incorrectly tries to make the XHR call from the popup window (even though the javascript explicitly says to call the XHR from the opener). The workaround is to call the dwr function from inside a window.setTimeout('myAjaxCall()',0). This will allow the window to close successfully in Firefox.

Here's the code example from the example website:
Without seeing your code its hard to say, but I bet that from your popup you are invoking a dwr call on the popup's window.opener. (i.e. in your popup you have something like window.opener.makeDWRCall()). The problem is when you do that, the XMLHttpRequest is "owned" by the popup window even though the call resides on your main window.

If you take a look at the Firefox bug listed above you will see a workaround to the issue:

function closePickupWindow(id) {
opener.pickupId.value = id;
if (opener.pickupFunctionPointer != null) {
opener.launchLookup();
}
window.close();

}

// This is to work around the following bug in firecox
function launchLookup() {
window.setTimeout('pickupFunctionPointer()', 0);
}

Some of this code is extraneous, but the basic idea is that from your popup you call a function on window.opener that uses setTimeout to issue the DWR call. Note that the timeout can be instantaneous because all you are really trying to do is change the caller of the DWR function to window.opener instead of your popup.

Hope this helps.
John Kleinschmidt

6 comments:

Anonymous said...

Thanks man! I've been hunting this bug for a couple of hours now, and it was driving me insane. So thank you for posting this.

Anonymous said...

Oh, dude, you just saved my backside. What a strange bug; even once I understood it I couldn't work around it -- until I found your technique.

Anonymous said...

Thank you for making this information available. We've been battling this question all day; life worked in IE but not FF.

Thanks again!

Anonymous said...

Thanks a lot... I'll make a link in my own blog if you allow me.
My blog: http://blog.epikaf.net

Fokas

Anonymous said...

Thanks for this post , i have been sitting with this issue for a week now.And i tried the workaround posted here and it solved the issue.

Jeff Sheets said...

Glad that it helped you! Supposedly the bug was finally fixed a couple of months ago so hopefully the latest versions of Firefox will start working now