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 . This post has trackbacks.

Trackbacks:

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

 

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