[thelist] AJAX: Generlizating Response Handling, RFC

Matt Warden mwarden at gmail.com
Mon May 16 20:04:39 CDT 2005


Sam,

Thanks a lot for your response. I have a few comments below.

On 5/16/05, sam at sam-i-am.com <sam at sam-i-am.com> wrote:
> In IE, using onreadystatechange you pretty much just get an event that
> somethings state has changed, so I have to loop over the
> window.remoteRequests'
> xhr properties (objects) to see if any are ready. In gecko/safari
> "this" in the
> context of the event handler is the XHR object itself. So its a little quicker
> to lookup.

This is very good information to be aware of. Thanks for pointing this out.

> The RemoteRequests are culled when they're handled. AFAIK there's no way to
> reuse a XmlHttpRequest object for multiple requests.

Here's where you had me seriously worried. As you can tell from my
original message, I made the assumption (that I didn't actually
confirm with a test) that the same XMLHttpRequest object could be used
multiple times. I believe that certain articles led me to think this,
without actually coming out and saying it or providing an example
where it was re-used. Indeed, most example code instantiates a new
object for every request.

However, at least in Firefox, this doesn't seem to be necessary. See
my sandbox example:

http://mwarden.f2o.org/sandbox/ajax.htm

Type in a username and then move focus off of the field. Then replace
focus and then remove it again, to get a second request. You'll notice
that you get an alert notifying you of a nwely instantiated object
only the first time, yet the request works fine both times.

It would be helpful if someone could confirm this behavior in IE as
well. And, if someone has a Mac lying around, Safari would be nice to
test, too.

Specifically, I'm using this code to make requests, modified from
Apple's example code (req is initialized to null):

    // native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        if (req == null)
	{
		alert('Creating a new instance of XMLHttpRequest object');
		req = new XMLHttpRequest();
	} // end if
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // IE/Windows
    } else if (window.ActiveXObject) {
        if (req == null)
	{
		alert('Creating a new instance of Microsoft.XMLHTTP object');
		req = new ActiveXObject("Microsoft.XMLHTTP");
	} // end if
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    } // end if


> There's another way. You can create a unique event handler for each request,
> using an anonymous function to pass in your argments to another function, like
> so:
>   onreadystatechange = new Function("myAjaxResponseHandler('" + requestId +
> "')");

Interesting solution. My aim is to avoid creating an instance of
XMLHttpRequest for every request, though. Of course, one might argue
that if I am creating a new request, then I should be creating a new
request object.

> Anyhow, its nice to see more people thinking about this - this gave me some
> serious brain ache a few weekends ago.

Indeed. As it's a relatively new (well, newly feasible with cross
browser support) method, it will require some discussion before best
practices are developed. One can simply look at current articles (like
the few on xml.com, the solutions of which violate basic programming
principles like encapsulation and abstraction) to confirm that this
has not yet happened.

-- 
Matt Warden
Miami University
Oxford, OH, USA
http://mattwarden.com


This email proudly and graciously contributes to entropy.


More information about the thelist mailing list