Today is a good day to code

XMLHttpRequest vs iFrame

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

XMLHttpRequest vs iFrame

Picture of IrvinFor a while now, I've eschewed developing using the xmlhttprequest object for the iframe. The reasons are pretty simple. It is easy to pop open the iFrame window to debug whatever is happening behind it. When something doesn't work, you can see exactly why in a couple of seconds. This isn't the case with the xmlhttprequest object, which does everything behind the scenes.

I did have to use the xmlhttprequest object for a recent project and I liked how it worked. I was able to get the debug output from it by populating an iframe with the responseText from the action, so I could see what was happening in the same way. By piping the text in, and using an innerHTML to write to the iframe, it saw it as HTML which was good.

Another thing that was nice about the object was that it doesn't trip the loading function of the browser so it gives the user a cleaner experience. I only still gripe about it because sometimes I don't want to deal with XML, which isn't that tough to parse with the DOM. Or because I don't want to have to worry about sniffing out whether the user has internet explorer or not.

I came across another couple of issues which make testing scripts on a local machine difficult. In Firefox there is some sort of permissions system that prevents scripts from performing xmlhttprequest to access local assets. This makes a lot of sense, you wouldn't want a malicious person to be able to, in any way, read the contents of the local file system. The problem is that when you are testing scripts, you are using your local file system. There is a way to defeat this for testing, but I haven't figured out how to do it yet.

Another issue that cropped up was that the open attribute of the xmlhttprequest object doesn't like relative paths. So I have to change the URLs before I upload the scripts to the remote testing server. This isn't a big deal, just annoying.

Finally is the issue of how to handle the responses asychronously. There are many clever ways to do this. One is to use a function that calls the xmlhttprequest and contains the function executed onreadystatechange as a class. That way you can instantiate the object whenever you need to make a call. That will get you multiple instances of the object, but you still have to handle the responses. I have thought of many ways to do this, and only one seems good enough. It is to create an array object and add a slot for the function executing the xmlhttprequest. When the response comes in, you can tell the function called in the onreadystatechange attribute to put the responseXML into that array spot where the function will know where to pick it up. Now I'm just trying to work out how the array will notify the function that it belongs to that it's request is in. Oh well, never a dull day!