Feature detection (web development): Difference between revisions

Content deleted Content added
m top: hatnote Template:About; formatting: A|AB (using Advisor.js)
Carriearchdale (talk | contribs)
m clean up, typo(s) fixed: constrast → contrast using AWB
Line 5:
'''Feature detection''' (also '''feature testing''') is a technique used in [[web development]] for handling differences between [[runtime environments]] (typically [[web browsers]] or [[user agents]]), by programmatically testing for clues that the environment may or may not offer certain functionality. This information is then used to make the application adapt in some way to suit the environment: to make use of certain [[API]]s, or tailor for a better user experience.{{Citation needed|date=March 2014}}
 
Its proponents claim it is more reliable and future-proof than other techniques like [[User_agentUser agent#User_agent_sniffingUser agent sniffing|user agent sniffing]] and browser-specific CSS hacks.{{Citation needed|date=March 2014}}
 
==Techniques==
 
A feature test can take many forms. It is essentially ''any'' snippet of code which gives some level of confidence that a required feature is indeed supported. However, in constrastcontrast to other techniques, feature detection usually focusses on performing actions which directly relate to the feature to be detected, rather than [[heuristics]].{{Citation needed|date=March 2014}}
 
As [[JavaScript]] is the most prevelent [[scripting language]] in web browsers{{Citation needed|date=March 2014}}, many feature detection techniques use JavaScript to inspect the [[Document Object Model|DOM]] and local JavaScript environment.
Line 20:
}</nowiki>
 
For a higher level of confidence, some feature tests will attempt to invoke the feature then look for clues that it behaved properly. For example, a test for support for [[HTTP_cookieHTTP cookie|cookies]] might attempt to set a value as a cookie and then verify it can be read back.
 
==Undetectables==
Line 26:
Some browser features are considered ''undetectable'', because no clues are known to give sufficient confidence that a feature is supported. These are often because limited information available to the JavaScript environment in the browser; generally features must be exposed via the DOM in some way in order to be detetable using JavaScript.
 
When undetectables are encountered, it is common to turn to [[User_agentUser agent#User_agent_sniffingUser agent sniffing|user agent sniffing]] as an alternative mechanism, or to employ [[defensive coding]] to minimise the impact if the feature turns out not to be supported.
 
The [[Modernizr]] project maintains a record of known undetectables on their [http://github.com/Modernizr/Modernizr/wiki/Undetectables wiki].