HTML and CSS Reference
In-Depth Information
basic fallback solution. In such cases, feature detection can be used to discriminate
incapable browsers.
Going back to the strftime example, if we don't want to support
enhanced features in browsers that cannot handle a function argument to
String.prototype.replace , we simply abort the definition of the method in
browsers in which this feature test fails. Interfaces that use this method may choose
to do the same, i.e., if the strftime method is not available, higher level enhance-
ments that depend on it can choose to abort as well. As long as feature detection
is built into every layer of the application, avoiding some or all enhancements in
inadequate browsers should not be too complicated. The upside of this approach
is that it will work with all browsers that don't support the required functionality,
old and new alike, and even those we aren't aware of.
10.6.2 Undetectable Features
Some features are hard to detect. An example can be found in how Internet Ex-
plorer 6 renders certain replaced elements, such as select lists. Displaying another
element over such a list will cause the list to show through the overlaid element.
The quirk can be fixed by layering an iframe behind the overlay. Even if we cannot
detect this problem, the fix is not known to cause problems in other browsers, and
so can be safely employed in all browsers. If the fix to a problem won't have ill
effects in any browsers, applying the fix for everyone can often be simpler than de-
tecting the problem. Before applying a fix preemptively, it's a good idea to consider
performance implications.
Designing the problem away is another technique that is highly effective at
avoiding cross-browser woes. For instance, IE's implementation of getElement-
ById will gladly return elements whose name property matches the provided id.
This problem is simple to detect and work around, yet it is even simpler to make
sure HTML elements never use ids that match some name property on the page,
perhaps by prefixing ids.
10.7 Summary
In this chapter we dove into feature detection, the most reliable and future proof
technique available for writing cross-browser JavaScript. Browser sniffing in various
forms has several pitfalls, and cannot be trusted. Not only is this technique unreliable
and brittle, but it also requires knowledge about specific browsers in a way that make
it a maintainability nightmare.
 
 
Search WWH ::




Custom Search