Capability detection - the better way
I read this article today on dev.opera.com about javascript capability detection. They are right that capability detection is a far better way to determine support for certain features than to make assumptions based upon sniffing the user-agent string.
Now my eye caught their example of a classic case:
JavaScript:
I'd say this is wrong; first of all it means you have to start using a non-standard function and thus a non-standard method of working throughout your code, and secondly the object detection is being done each and every time you call this function.
In my opinion this would be a far better way:
JavaScript:
Now you can use the standard way, using document.getElementById(), throughout your script without having to worry about older browsers. It's a non-obtrusive and efficient way to add modern capabilities to older browsers.
Now this might not be a very good example in itself because IE4 and NS4 are long out of the picture, but this might make a better one:
JavaScript:
Now my eye caught their example of a classic case:
JavaScript:
1 | function findElement(id){
|
I'd say this is wrong; first of all it means you have to start using a non-standard function and thus a non-standard method of working throughout your code, and secondly the object detection is being done each and every time you call this function.
In my opinion this would be a far better way:
JavaScript:
1 | if (!document.getElementById)
|
Now you can use the standard way, using document.getElementById(), throughout your script without having to worry about older browsers. It's a non-obtrusive and efficient way to add modern capabilities to older browsers.
Now this might not be a very good example in itself because IE4 and NS4 are long out of the picture, but this might make a better one:
JavaScript:
1 | if (!window.XMLHttpRequest)
|
|
|
.toJSONString() and Object.prototype |
|
|
Having fun with IE - part 3: doctype switching |
Comments
Comments are closed