Sunday, 6 January 2008

Encapsulating javascript objects (xmlhttprequest)

As much as I loathe Javascript objects, using them gives the developer control and limit the opportunity of overriding existing Javascript. What I really wanted to do is encapsulate the xmlhttprequest into a nice class. Unfortunately, xmlhttprequest itself uses a caller function, onreadystatechange. This proves a huge challenge. Essentially, you want to include a function/method to handle the call. But believe me, this is a headscratcher. One interesting technique is to take a copy of the class instance before running the onreadystatechange function. You then have scope to the global instance and as well as the current instance as demonstrated at mike chambers. Unfortunately, it looks like firefox and ie handle things a little different. I tried to set a text attribute for my class within the caller function, to my surprise, Internet Explorer sets it but for some reason Firefox has it undefined and only used my text attribute in a local scope. Another solution is provided hunlock. This looks to be a useful module indeed, exactly what I am looking for. But once again, firefox doesn't appear to like it.

No comments: