Search

Favourite Projects

Barsy

Firefox firebug and synchronos calls problem

April 12th, 2007 by lukav

For those of you that have discovered that when you have Firebug installed and are developing some synchronous XMLHttpRequest everything works, and when you disable it it stops: here is the problem.

Believe it or not it is a bug in Firefox NOT in Firebug. It turns out that Firefox doesn’t call onreadystatechange when the you set the 3 parameter (async) to false in open. Probably FF expects that when a call is synchronous the developer will process the result in the lines following the send call. May be this make sense, but if you want to quickly test async/sync calls or use a library that doesn’t take care of this, you be in trouble.

I can say for certain (I’ve committed the code myself) that the next release of OpenLink AJAX Toolkit won’t have this problem and will make no difference if you have Firebug or not.

I use the following to catch the situation where Firebug is not installed or is installed but disabled:

  try{
    if(OAT.Dom.isGecko() && !xhr.options.async && xhr.obj.onreadystatechange == null)
      OAT.AJAX.response(xhr);
  }catch (e){}

OAT.Dom.isGecko() - return true if it mozilla and derivatives
OAT.AJAX.response - is the function that is normally called when the request finish.
xhr.options.async - is holding if the call is async or not
xhr.obj.onreadystatechange - is the original Firefox XMLHttpRequest object instance, since the code above works in the context of OAT

Posted in EN, OAT, OpenLink, Tech |

18 Responses

  1. A Casa do Java Says:

    Great! I had to use the workaround “process the result in the lines following the send call” cause my syncronous call didn’t work in Firefox 1.5. However, it works all the way in Firefox 2.0 and, of course, IE 6 and 7.

  2. Chris Bloom Says:

    Thanks for the solution! This was bugging me all weekend. So is this a bug in Mozilla? If so, I couldn’t find any bug report related to it in Bugzilla.

  3. lukav Says:

    My pleasure,

    Yes it is a bug in mozilla IMO, I’ve recently read some specs where is was explicitly stated that onreadystatechange should be called with sync calls also. But I have to admit that I haven’t checked the bugzilla.

  4. Chris Bloom Says:

    https://bugzilla.mozilla.org/show_bug.cgi?id=383304

  5. Phil Says:

    Can you explain how exactly this code should be implemented? Here is my code:

    xmlhttp.open(’GET’,getQuery, false);
    xmlhttp.onreadystatechange=state_Change;
    xmlhttp.send(null);

    Where should the code you posted go?

    Thanks,
    Phil

  6. lukav Says:

    Hi Phil,

    the code I posted is for OAT. And it is long included in the distribution. So in your case, since you don’t use OAT you should call the procedure by yourself like so:

    xmlhttp.open(’GET’,getQuery, false);
    xmlhttp.onreadystatechange=state_Change;
    xmlhttp.send(null);
    if (xmlhttp.onreadystatechange == null) state_Change();

    I think this should do the trick, so you don’t have problems with you app if Firebug is installed or not.

  7. Phil Says:

    Thanks Lukav — it’s firing now but I can’t seem to do anything with the xmlhttp.responseText that is returned. For example, take the returned XML and parse it.

  8. lukav Says:

    Phil,
    What do you mean?
    Is xmlhttp.responseText empty after the call? Can you provide an example?
    If your are using windows I recommend using ‘fiddler’ so you can see the actual http request- response, just to make sure a content is returned.
    I also recommend using some library like OAT or OpenAjax, since the browser incompatibilities are taken care of and you don’t need to reinvent the wheel all the time.

  9. Fellipe Cicconi Says:

    Thank you very much! This bug was killing me

  10. ben Says:

    yeah thanks a tons that bug was being a pain in the butt

  11. Federico Says:

    lukav,
    after days of unsuccesful search on the web, finally i found the solution on your one.

    xmlhttp.open(’GET’,getQuery, false);
    xmlhttp.onreadystatechange=state_Change;
    xmlhttp.send(null);
    if (xmlhttp.onreadystatechange == null) state_Change();

    It’s resolve all my problems with XMLHttp on mozilla’s browser!

    Thank a lot
    Bye
    Federico

  12. ScratchMyTail Says:

    Awesome. Have been working with this bug for ages now.
    This line works great:
    if (xmlhttp.onreadystatechange == null) state_Change();

    thanks!

  13. Alex from Chile Says:

    Gracias muchas muchas GRACIAS

  14. Jeremy Says:

    The line:
    if (xmlhttp.onreadystatechange == null) state_Change();

    fixed the firefox problem. But it broke Internet Explorer. What to do???

    Thanks

  15. lukav Says:

    Execute the line only if the browser is IE. There are plenty of samples how to detect the browser :)

  16. pradeep Says:

    Thanks Federico…
    We found this problem in our application…
    We got rid of this problem by using this single line of code..
    Thanks once again……..

  17. handan Says:

    firefox no onreadystatechange method?
    yes?

  18. lukav Says:

    Handan,
    what is your question?

Leave a Comment

OpenID

Anonymous

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.