Today is a good day to code

More on the XMLHttpRequest

Posted: December 31st, 1969 | Author: | Filed under: Uncategorized | No Comments »

More on the XMLHttpRequest

Picture of IrvinI kept plugging away at it until I had a reusable and modular xmlhttprequest javascript class. It took a while for me to understand the javascript concept of a class, and trying to understand what is instantiated and what is not, but I have something now and I plan to use it frequently.

One of the things I noticed right off is that while it is a pain to use the xmlhttprequest object, it is way faster than using iframes. Most of the people who have been doing it for a while are probably like duh! But I hadn't been using it much so I didn't notice how much overhead managing the iframe was. I will upload the code as a zip toward the end of this post, and I won't go into the class itself, but I will just drop a hint as to how to instantiate it.

var xhttp = new Request('','functionToDo','text');

The first command creates a new instance of the object that holds the instances of the xmlhttprequest function as well as all of the parameters it needs to execute, like the url, the function to call when it is done, and what type of return it should use. The first parameter in the Request class is the url to post or get to. By the way, right now it always does gets, but it could easily be modified to do posts. The second parameter is the function to call, courtesy of the invaluable eval() javascript function, and the third is whether to send back text or javascript to the function to be executed. The hard part is that the function that should fire when the response comes back good can only accept one parameter, if you try to send in another, it will cause it to break.

The second command tells the new instance of the xmlhttprequest to execute. Whenever it comes back, the function will be tripped and everything is good.

I'm still a little foggy on the use of the self namespace in javascript. I understand conceptually what it is doing, and I understand in practice what it is doing, but when it comes to how it is working in instances I had to use trial and error to make sure I wasn't declaring any globals. I haven't noticed any memory leaks yet, but I'll keep an eye on it. Anyway, there it is a reusable javascript object. Much of the code for the xmlhttprequest object in earnest came from Apple's site concerning the selfsame object.

Apple's XMLHttpRequest Article

Its a good read and it helped me get along with using it.

Here are the files, enjoy!

XmlHttpRequest class code

PS: There is no coldfusion in the code. I wanted it to remain strictly scripting language independent so that I could plug it in anywhere, so this is no substitute for cfajax or dojo or anything like that. In fact, for most purposes those are probably the better solution because you don't have to write all of the code to use the javascript. This is raw, and for those who are do-it-yourselfers when it comes to coding.