Blogger :
Ajaxian Blog
All posts :
All posts by Ajaxian Blog
Category :
XML
Blogged date : 2006 Mar 31
If you’ve ever been programming along, working with your Ajax connections and wondered why you have to make a new one each time, you’re not alone. Eric Pascarello asked himself the same question and decided to do something out it. He’s written up a simple test Ajax app that just pulls in XML data from a remote script to illustrate the example.
One thing I see popping up over message boards is people want to reuse the XMLHttpRequest Object instead of creating a new instance every single time. Some developers think this will help in memory leaks. I have not tested it in any way to see if it really helps. But I thought that I would show you it is possible to reuse the same object over again and again with Internet Explorer.
For this code I am going to be using a very basic example. I am not going to use OO code so just bear with it. With Firefox you are able to reuse the XMLHttpRequest object without this craziness, but with Internet Explorer 6 you are not able to do that (I have not tested this in IE 7 yet so hopefully someone will comment on the reuse factor!)
His example involves the simple click of two buttons to load the remote file into the Ajax object. Everything behanves just fine in Firefox when clicking both of the buttons - object reuse is fine. The real issue comes around when Internet Explorer is thrown into the mix. Click on the first button and all is well. Click on the second and nothing happens. It just doesn’t understand normally how to reuse that previously created object.
Eric did, however, find a way to get around this lovely little bug in IE - the use of the abort() method in the loading of the XMLHttpRequest object. The issue is that Internet Explorer doesn’t seem to release the connection once it’s made, so it can’t reuse a still-open connection. Abort() closes it and allows the object to be refreshed and open to whatever use on whatever browser.
Always good to see a simple solution for a simple problem…