Flags and Lollipops

Sunday, January 14, 2007

onreadystatechange in Firefox

Just some Googleable info to save other people from banging their heads against a brick wall: I came across a weird bug in Firefox (2?)'s XMLHttpRequest implementation.

The onreadystatechange is, in theory, meant to fire whenever the ready state of the XMLHttpRequest object changes. The readystate goes (realistically) from 1 (loading request) to 4 (request complete). As you might expect, in practice different browsers have different behaviours.

Anyway, the bug: when loading one particular URL (using code very similar to this) only transition to state 1 was firing the onreadystatechange event. Using Firebug you could see the GET request going out and the correct data coming back, so the request was being fulfilled. With other URLs everything worked as it should.

The fix was to set up the onreadystate change handler after opening and sending.
http = new xmlhttprequest object, etc. etc.

http.open('get', editUrl);
http.send(null);
http.onreadystatechange = handleEditResponse();
Don't have a clue why...

Labels: , , ,

Comments and trackbacks Feel free to post your comments Anonymous Anonymous Anonymous Anonymous Anonymous LIMS/Bioinfo programmer Anonymous Anonymous Anonymous redghia69 Blogger Pheliox Anonymous Anonymous Blogger Joel . This post has trackbacks.

Trackbacks:

8 Comments:

At February 06, 2007 3:30 PM, Anonymous Anonymous said...

I had blood trickling down my forehead. Then I read your post! Hurray, thank you sooo much!

 
At March 15, 2007 12:25 AM, Anonymous Anonymous said...

When you set http.onreadystatechange, you need to set the value as the handler function. Your example is calling the handleEditResopnse function instead of setting it up as the handler.

 
At May 30, 2007 1:37 AM, Anonymous LIMS/Bioinfo programmer said...

Your blog did help me, in conjunction with the previous post detailing that we are really setting the string name of the function to call in the obj.onreadystatechange = dosomething;
// This works because it sets rather than calls a function.

I was also calling the function rather than setting it.

obj.onreadystatechange=dosomething(message_element);
//calls the function dosomething and attempts to pass an id to set an output message, and gives the same behavior you had written about.

 
At June 23, 2007 5:53 AM, Anonymous Anonymous said...

I'm still having a problem with that,
if I open("GET",url,true) AND send(null) befor the onreadystatechange,
it go's from working only in IE to working in none.

It seems in my experiences IE will only take the open befor the onreadystatechange and not the send.

 
At September 02, 2008 11:29 PM, Anonymous redghia69 said...

Thanks for pointing me in the right direction with this. I never had this trouble with FF2 (except just recently it appeared on a mac running older FF2). A similar oddity is back in FireFox 3: if you want your response function to have access to the responseText, you have to set onreadystatechange after send. Further, where code such as:

obj.onreadystatechange = dosomething;

worked in FF2, FireFox 3.0.1 seems to require the function brackets, whether or not you're passing any info to the function:

obj.onreadystatechange = dosomething();

 
At December 12, 2008 3:25 AM, Blogger Pheliox said...

Hey... It doesn't work with me..
onreadystatechange = function(){code};

why?

 
At October 21, 2009 5:21 PM, Anonymous Anonymous said...

Thanks! I was only 2 hours into that little gem of a bug till I found this post, just in time for the gf coming home to go to the pub as well.

Also, onreadystatechange = function will work in Chrome and IE but in FF it has to be onreadystatechange = function() with brackets that I'd missed off :)

 
At March 17, 2010 11:04 PM, Blogger Joel said...

Thank you, thank you, thank you! I thought I was going insane. Usually it's hacks for IE's non-standard behavior. Nice that I can do a hack for Firefox once in awhile.

 

Post a Comment

<< Home


See all posts from: July 2005 August 2005 September 2005 October 2005 November 2005 December 2005 January 2006 February 2006 March 2006 April 2006 May 2006 June 2006 July 2006 September 2006 October 2006 November 2006 December 2006 January 2007 February 2007 March 2007 April 2007 May 2007 June 2007 July 2007 August 2007 October 2007 November 2007 December 2007 January 2008 February 2008 March 2008 April 2008 May 2008 October 2008 December 2008 January 2009 February 2009 March 2009 June 2009