Dateigrösse: 5.86 kb
1 ## Bowser 2 A Browser detector. Because sometimes, there is no other way, and not even good modern browsers always provide good feature detection mechanisms. 3 4 [](https://travis-ci.org/ded/bowser/) 5 6 So... it works like this: 7 8 ``` js 9 if (bowser.msie && bowser.version <= 6) { 10 alert('Hello China'); 11 } 12 ``` 13 14 ## 1.1.0 breaking changes 15 We don't save built script in the repo anymore. The main file (`src/bowser.js`) is available through NPM or Bower. 16 Also you can download minified file from [the release page](https://github.com/ded/bowser/releases). 17 18 ## 1.0.0 breaking changes 19 `browser = require('bowser').browser;` becomes `bowser = require('bowser');` 20 21 --- 22 23 ## API 24 25 ### bowser()`:Object` 26 Use it to get object with detected flags of your current browser. 27 28 ### bowser._detect(ua `:String`)`:Object` 29 Use it to get object with detected flags from User Agent string. 30 31 ### bowser.check(minVersions`:Object`, strictMode`:Boolean`, [ua]`:String`)`:Boolean` 32 Use it to check if browser supported. 33 34 ``` js 35 /** 36 * in case of using IE10 37 */ 38 bowser.check({msie: "11"}); // true 39 bowser.check({msie: "9.0"}); // false 40 41 /** 42 * specific user agent 43 */ 44 bowser.check({chrome: 45}, window.navigator.userAgent); // true 45 46 /** 47 * but false in strict mode 48 */ 49 bowser.check({chrome: 45}, true, window.navigator.userAgent); // false 50 ``` 51 52 ### bowser.compareVersions(versions`:Array<String>`)`:Number` 53 Use it to compare two versions. 54 55 ``` js 56 bowser.compareVersions(['9.0', '10']); 57 // -1 58 ``` 59 60 ### bowser.isUnsupportedBrowser(minVersions`:Object`, [strictMode]`:Boolean`, [ua]`:string`)`:Boolean` 61 Use it to check if browser is unsupported. 62 63 ``` js 64 bowser.isUnsupportedBrowser({msie: "10"}, window.navigator.userAgent); 65 // true / false 66 ``` 67 68 See more examples in [tests](test/test.js). 69 70 --- 71 72 ## Bowser Flags 73 Your mileage may vary, but these flags should be set. See Contributing below. 74 75 ``` js 76 alert('Hello ' + bowser.name + ' ' + bowser.version); 77 ``` 78 79 ### All detected browsers 80 These flags are set for all detected browsers: 81 82 * `name` - A human readable name for this browser. E.g. 'Chrome', '' 83 * `version` - Version number for the browser. E.g. '32.0' 84 85 For unknown browsers, Bowser makes a best guess from the UA string. So, these may not be set. 86 87 ### Rendering engine flags 88 If detected, one of these flags may be set to true: 89 90 * `webkit` - Chrome 0-27, Android <4.4, iOs, BB, etc. 91 * `blink` - Chrome >=28, Android >=4.4, Opera, etc. 92 * `gecko` - Firefox, etc. 93 * `msie` - IE <= 11 94 * `msedge` - IE > 11 95 96 Safari, Chrome and some other minor browsers will report that they have `webkit` engines. 97 Firefox and Seamonkey will report that they have `gecko` engines. 98 99 ``` js 100 if (bowser.webkit) { 101 // do stuff with safari & chrome & opera & android & blackberry & webos & silk 102 } 103 ``` 104 105 ### Device flags 106 If detected, one of these flags may be set to true: 107 108 * `mobile` - All detected mobile OSes are additionally flagged `mobile`, **unless it's a tablet** 109 * `tablet` - If a tablet device is detected, the flag `tablet` is **set instead of `mobile`**. 110 111 ### Browser flags 112 If detected, one of these flags may be set to true. The rendering engine flag is shown in []'s: 113 114 * `chrome` - [`webkit`|`blink`] 115 * `firefox` - [`gecko`] 116 * `msie` 117 * `msedge` 118 * `safari` - [`webkit`] 119 * `android` - native browser - [`webkit`|`blink`] 120 * `ios` - native browser - [`webkit`] 121 * `opera` - [`blink` if >=15] 122 * `samsungBrowser` - [`blink`] 123 * `phantom` - [`webkit`] 124 * `blackberry` - native browser - [`webkit`] 125 * `webos` - native browser - [`webkit`] 126 * `silk` - Amazon Kindle browser - [`webkit`] 127 * `bada` - [`webkit`] 128 * `tizen` - [`webkit`] 129 * `seamonkey` - [`gecko`] 130 * `sailfish` - [`gecko`] 131 * `ucbrowser` — [`webkit`] 132 * `qupzilla` — [`webkit`] 133 * `vivaldi` — [`blink`] 134 * `sleipnir` — [`blink`] 135 * `kMeleon` — [`gecko`] 136 137 For all detected browsers the browser version is set in the `version` field. 138 139 ### OS Flags 140 If detected, one of these flags may be set to true: 141 142 * `mac` 143 * `windows` - other than Windows Phone 144 * `windowsphone` 145 * `linux` - other than `android`, `chromeos`, `webos`, `tizen`, and `sailfish` 146 * `chromeos` 147 * `android` 148 * `ios` - also sets one of `iphone`/`ipad`/`ipod` 149 * `blackberry` 150 * `firefoxos` 151 * `webos` - may also set `touchpad` 152 * `bada` 153 * `tizen` 154 * `sailfish` 155 156 `osversion` may also be set: 157 158 * `osversion` - for Android, iOS, Windows Phone, WebOS, Bada, and Tizen. If included in UA string. 159 160 iOS is always reported as `ios` and additionally as `iphone`/`ipad`/`ipod`, whichever one matches best. 161 If WebOS device is an HP TouchPad the flag `touchpad` is additionally set. 162 163 ### Browser capability grading 164 One of these flags may be set: 165 166 * `a` - This browser has full capabilities 167 * `c` - This browser has degraded capabilities. Serve simpler version 168 * `x` - This browser has minimal capabilities and is probably not well detected. 169 170 There is no `b`. For unknown browsers, none of these flags may be set. 171 172 ### Ender Support 173 174 `package.json` 175 176 ``` json 177 "dependencies": { 178 "bowser": "x.x.x" 179 } 180 ``` 181 182 ``` js 183 if (require('bowser').chrome) { 184 alert('Hello Silicon Valley') 185 } 186 ``` 187 188 ### Contributing 189 If you'd like to contribute a change to bowser, modify the files in `src/`, then run the following (you'll need node + npm installed): 190 191 ``` sh 192 $ npm install 193 $ make test 194 ``` 195 196 Please do not check-in the built files `bowser.js` and `bowser.min.js` in pull requests. 197 198 ### Adding tests 199 See the list in `src/useragents.js` with example user agents and their expected bowser object. 200 201 Whenever you add support for new browsers or notice a bug / mismatch, please update the list and 202 check if all tests are still passing. 203 204 ### License 205 Licensed as MIT. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details. 206