HTML and CSS Reference
In-Depth Information
Microsoft.XMLHTTP will do, as Msxml2.XMLHTTP.3.0 (again, ships with
IE6) includes the Microsoft.XMLHTTP alias for backwards compatibility.
12.3.3 Implementing tddjs.ajax.create
With knowledge of the different objects available, we can take a shot at implementing
ajax.create , as seen in Listing 12.6.
Listing 12.6 Creating an XMLHttpRequest object
tddjs.namespace("ajax").create = function () {
var options = [
function () {
return new ActiveXObject("Microsoft.XMLHTTP");
},
function () {
return new XMLHttpRequest();
}
];
for (var i = 0, l = options.length; i < l; i++) {
try {
return options[i]();
} catch (e) {}
}
return null;
};
Running the tests confirms that our implementation is sufficient. First test green!
Before we hasten on to the next test, we should look for possible duplication and
other areas that could be improved through refactoring. Although there is no obvi-
ous duplication in code, there is already duplication in execution—the try/catch to
find a suitable object is executed every time an object is created. This is wasteful,
and we can improve the method by figuring out which object is available before
defining it. This has two benefits: The call time overhead is eliminated, and fea-
ture detection becomes built-in. If there is no matching object to create, then there
will be no tddjs.ajax.create , which means that client code can simply test
for its existence to determine if XMLHttpRequest is supported by the browser.
Listing 12.7 improves the method.
 
Search WWH ::




Custom Search