Uncommented Bytes

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:

  • 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.

    By Anonymous Anonymous, at 16 November, 2006 02:47  

  • 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.

    By Anonymous Anonymous, at 29 December, 2006 13:05  

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

    Thanks again!

    By Anonymous Anonymous, at 27 September, 2007 19:05  

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

    Fokas

    By Anonymous Anonymous, at 04 October, 2007 03:02  

  • 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.

    By Anonymous Anonymous, at 05 May, 2014 01:07  

  • 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

    By Blogger Jeff Sheets, at 05 May, 2014 10:24  

Post a Comment

Links to this post:

Create a Link

<< Home