Geeks With Blogs

News
Welcome to my blog.
Here's what we've got on the menu today:

Lorin Thwaits A geek says what?

I'm writing a web page that uses AJAX to poll the server every few seconds to see if there are any updates to data.  Works great in IE6 and older using the XMLHttpRequest ActiveX object that you can instantiate from the progid "Msxml2.XMLHTTP.6.0".  In order to keep polling I've got an event handler listening to the onreadystatechange event that calls a setTimeout at the end, so it pauses for 5 seconds, then calls into another function that sends out another request.  Once you get the ball rolling, it keeps going.  Here's the code:

var xhr = new ActiveXObject("Msxml2.XMLHTTP.6.0");

 

xhr.onreadystatechange = function() {

if (xhr.readyState == 4) { if (xhr.status == 200) {

doUpdates(xhr.responseText);   // Go do the updates from the response text

setTimeout("checkUpdates();",5000); // Poll again after 5 seconds

} }

};

 

function checkUpdates(){

// See if there has been a change in the data visible to this user

xhr.open("POST","poll.ashx", true);

xhr.send(username);

}

 

// This kicks it all off:

setTimeout("checkUpdates();",5000); // Kick off the first poll after 5 seconds

I really should make a watchdog that runs every 20 seconds in case something gets hung up, but overall the every 5 second polling thing works quite well.  Except in IE7.

UPDATE: I was fooled, and later found out that the problem actually lies in the reusability of the XMLHttpRequest object in IE7.  Like in FireFox, you can't use it for more than one request, and have to new up another each time you call back to the server.  But the older ActiveX versions of XMLHttpRequest can be reused.

If you try to use this polling technique with the native XMLHttpRequest object in IE7, then when it is in the event handler code apparently it runs in a different security context or something, and after the setTimeout object is brought to life, it disappears as soon as execution leaves the event handler!  Must be some kind of attempt to provide enhanced security for AJAX, but unfortunately this makes it ridiculously difficult to create a routine that polls on a consistent basis, a few seconds after a response is returned.  I ended up just using the old-school Msxml2.XMLHTTP.6.0, which fortunately is still supported in IE7.

Posted on Wednesday, February 28, 2007 9:59 AM AJAX | Back to top


Comments on this post: Idiosyncrisy with the built-in XMLHttpRequest object in IE7

# re: Idiosyncrisy with the built-in XMLHttpRequest object in IE7
Requesting Gravatar...
The 100 percent cure to this is:

don't use AJAX!

It absolutely will not work in a properly locked down browser environment.

BTW, it is idiosyncracy
Left by spenser on Feb 13, 2009 10:02 PM

# re: Idiosyncrisy with the built-in XMLHttpRequest object in IE7
Requesting Gravatar...
I first tried to install normally using VMAdditions...
Left by DDos Protection on Nov 08, 2009 11:52 PM

# re: Idiosyncrisy with the built-in XMLHttpRequest object in IE7
Requesting Gravatar...
Hi blogger, thank you for providing this blog post.. I found it brilliant. Have a nice day, Winona!!
Left by hidden object games on Mar 06, 2010 6:58 PM

# re: Idiosyncrisy with the built-in XMLHttpRequest object in IE7
Requesting Gravatar...

I was so fortunate to encounter the ll,thank you very much .
Left by cheap mbt shoes on Mar 17, 2010 7:15 PM

Your comment:
 (will show your gravatar)


Copyright © Lorin Thwaits | Powered by: GeeksWithBlogs.net