You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: page/code-organization/feature-browser-detection.md
+9-35
Original file line number
Diff line number
Diff line change
@@ -14,13 +14,13 @@ In general, we recommend specific feature detection. Let's look at why.
14
14
15
15
### Browser Detection
16
16
17
-
Browser detection is a method where the browser's User Agent (UA) string is checked for a particular pattern unique to a browser family or version. For example, this is Chrome 18's UA string on Mac OS X Lion:
17
+
Browser detection is a method where the browser's User Agent (UA) string is checked for a particular pattern unique to a browser family or version. For example, this is Chrome 39's UA string on Mac OS X Yosemite:
18
18
19
19
```
20
-
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.142 Safari/535.19
20
+
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
21
21
```
22
22
23
-
Browser UA detection may check this string for something like "Chrome" or "Chrome/18" or any other part the developer feels identifies the browser they intend to target.
23
+
Browser UA detection may check this string for something like "Chrome" or "Chrome/39" or any other part the developer feels identifies the browser they intend to target.
24
24
25
25
While this seems to be an easy solution, there are several problems:
26
26
@@ -51,7 +51,6 @@ Now how would you go about doing that?
51
51
There are several ways to go about feature detection:
This is a very simple way to provide conditional experiences, depending on the features present in the user's browser. We can extract this into a helper function for reuse, but still have to write a test for every feature we're concerned about. This can be time-consuming and error-prone.
74
-
75
-
What if someone else wrote all of that for us?
76
-
77
-
#### $.support
78
-
79
-
jQuery performs many tests to determine feature support to allow cross-browser use of many of the features we've come to love. jQuery's internal feature detection can be accessed through [jQuery.support](http://api.jquery.com/jQuery.support/).
80
-
81
-
However, we do not recommend this for general use. As the API page says:
82
-
83
-
> Since jQuery requires these tests internally, they must be performed on every page load. Although some of these properties are documented below, they are not subject to a long deprecation/removal cycle and may be removed once internal jQuery code no longer needs them.
84
-
85
-
This detection may be removed from jQuery without notice. That's reason enough not to use it. What other options do we have?
72
+
This is a very simple way to provide conditional experiences, depending on the features present in the user's browser. We can extract this into a helper function for reuse, but still have to write a test for every feature we're concerned about. This can be time-consuming and error-prone. Instead you might be interested in using a helper library.
86
73
87
74
#### A Helper Library
88
75
89
-
Thankfully, there are some great helper libraries (like [Modernizr](http://modernizr.com)) that provide a simple, high-level API for determining if a browser has a specific feature available or not.
76
+
Thankfully, there are some great helper libraries (like [Modernizr](http://modernizr.com/)) that provide a simple, high-level API for determining if a browser has a specific feature available or not.
90
77
91
78
For example, utilizing Modernizr, we are able to do the same canvas detection test with this code:
92
79
@@ -98,39 +85,26 @@ if ( Modernizr.canvas ) {
98
85
}
99
86
```
100
87
101
-
That's it. Easy.
88
+
For more in-depth information on Modernizr, feel free to check out their [documentation](http://modernizr.com/docs/).
102
89
103
90
### Performance Considerations
104
91
105
92
So, while the Modernizr syntax is great, it can end up being quite cumbersome to have several conditionals. Secondly, we're sending the code for both conditions to every browser, regardless if we'll need it or not.
106
93
107
-
The `Modernizr` object exposes a `load()` method that many prefer over the syntax mentioned previously. This is due to another library that Modernizr now uses internally: [yepnope](http://yepnopejs.com/). Testing for canvas can now become something like this:
108
-
109
-
```
110
-
Modernizr.load({
111
-
test: Modernizr.canvas,
112
-
yep: "canvas.js",
113
-
nope: "canvas-polyfill.js"
114
-
});
115
-
```
116
-
117
-
Using the `load` method allows us to send only the required polyfills and code over the wire. You can also pass an array of objects as an argument to `.load()`, and it will serve the correct files to the correct audience.
118
-
119
-
Additionally, Modernizr has a [production build configurator](http://modernizr.com/download/) that allows you to specify exactly what parts of Modernizr you want to include and exclude the parts you don't.
94
+
If you're using Modernizr, we highly encourage you to use the [build configurator](http://modernizr.com/download/), a tool that allows you to create custom builds of the library. You can exclude checks you don't need, which might save you quite a bunch of bytes.
120
95
121
96
### Other Resources
122
97
123
98
#### Feature Detection Tools
124
99
125
100
*[modernizr](http://modernizr.com/) — Conditionally check to see if a specific feature is available in a browser.
126
101
*[html5please](http://html5please.com/) — Use the new and shiny responsibly.
127
-
*[html5please api](http://api.html5please.com/) — An API you can query to see how good (or bad) support for a specific feature is.
102
+
*[html5please API](http://api.html5please.com/) — An API you can query to see how good (or bad) support for a specific feature is.
128
103
*[caniuse](http://caniuse.com/) — Browser compatibility tables for HTML5, CSS3, SVG, etc.
*[Using Modernizr to detect HTML5 and CSS3 browser support](http://www.adobe.com/devnet/dreamweaver/articles/using-modernizr.html)
135
-
*[Polyfilling the html5 gap](http://addyosmani.com/polyfillthehtml5gaps/slides/#1) by Addy Osmani
109
+
*[Polyfilling the HTML5 gaps](http://addyosmani.com/polyfillthehtml5gaps/slides/#1) by Addy Osmani
136
110
*[Feature, Browser, and Form Factor Detection: It's Good for the Environment](http://www.html5rocks.com/en/tutorials/detection/index.html) by Michael Mahemoff
0 commit comments