HTML and CSS Reference
In-Depth Information
This method is able to recognize callable host objects based on the following
observations:
ActiveX properties always have a typeof result of "unknown."
Non-ActiveX callable host objects in Internet Explorer usually have a
typeof result of "object." The boolean coercion is required to avoid
null , which also has a typeof result of "object."
In other browsers, callable objects tend to have a typeof result of
"function," even host methods
Using this helper, we can improve our cross-browser event handler, as seen in
Listing 10.8.
Listing 10.8 Improved feature detection for addEventHandler
function addEventHandler(element, type, listener) {
if (tddjs.isHostMethod(element, "addEventListener")) {
element.addEventListener(type, listener, false);
} else if (tddjs.isHostMethod(element, "attachEvent") &&
listener.call) {
element.attachEvent("on" + type, function () {
return listener.call(element, window.event);
});
} else {
// Possibly fall back to DOM0 event properties or abort
}
}
10.2.4 Sample Use Testing
Testing for the existence and type of an object is not always sufficient to ensure it
can be used successfully. If a browser provides a buggy implementation of some
feature, testing for its existence before using it will lead us straight into a trap. To
avoid such buggy behavior, we can write a feature test in which we use the feature
in a controlled manner before determining if the current environment supports the
feature.
The strftime implementation provided in Chapter 1, Automated Testing ,
heavily relies on the String.prototype.replace method accepting a func-
tion as its second argument, a feature not available on certain older browsers.
Listing 10.9 shows an implementation of strftime that uses replace in a con-
trolled manner, and then defines the method only if the initial test passes.
 
Search WWH ::




Custom Search