Ajax cleans up

Today I made my first Ajax call in anger.

One thing that bothered me about the examples I'd seen was that they all seemed to have a global xml http request object. When I were a lad, all the village elders, without exception, said that global anything was BAD. But still, what I wanted was to call InitiateAjaxRequest(url, myHandler), define myHandler and leave it at that with no sneaky little var http = new XmlHttpRequests lurking in the shadows.

On the face of it, this is tricky, because the handler, in all the examples I'd seen, needed the http request object to be in scope so it could check the ready state, status and get the response text.

Joel Spolsky give me the clue here. What I needed was an anonymous function. See, if you provide an anonymous handler in the body of InitiateAjaxRequest(url, myHandler), it has access to the http object, can check what it needs to check and then call your handler, passing, say, just the response text.

Code tomorrow.

Comments

Popular Posts