Skip to content

Instantly share code, notes, and snippets.

@mr-moon
Last active August 29, 2015 14:05
Show Gist options
  • Save mr-moon/77c90944804f7da71ecd to your computer and use it in GitHub Desktop.
Save mr-moon/77c90944804f7da71ecd to your computer and use it in GitHub Desktop.
Doing supported browsers check with WhichBrowser.net library.
var
INTERNET_EXPLORER = 'Internet Explorer',
FIREFOX = 'Firefox',
CHROME = 'Chrome',
OPERA = 'Opera',
OPERA_MINI = 'Opera Mini',
SAFARI = 'Safari',
MAXTHON = 'Maxthon',
WEBKIT = 'WebKit',
TRIDENT = 'Trident',
BLINK = 'Blink',
GECKO = 'Gecko',
IOS = 'iOS',
ANDROID = 'Android',
ua = new WhichBrowser(),
createAssertFunction = function (test, debug) {
return function (name, compare, version) {
return debug
? function() {
var result = ua[test].call(ua, name, compare, version);
if (result) {
console.warn('Requirement is met: ' + ua + ' | ' + name + ' ' + compare + ' ' + version);
}
return result;
}
: function () {
return ua[test].call(ua, name, compare, version);
}
}
},
debug = true,
os = createAssertFunction('isOs', debug),
engine = createAssertFunction('isEngine', debug),
browser = createAssertFunction('isBrowser', debug),
browserTests = [
[browser(CHROME, '>=', 36)],
[browser(FIREFOX, '>=', 23)],
[browser(SAFARI, '>=', 5)],
[browser(OPERA, '>=', 21)],
[browser(MAXTHON, '>=', '4.4')],
[browser(INTERNET_EXPLORER, '>=', 9)],
[os(IOS, '>=', 6), browser(CHROME, '>=', 36)],
[os(IOS, '>=', 6), browser(OPERA_MINI, '>=', 8)],
[os(ANDROID, '>=', 4), browser(CHROME, '>=', 18)]
],
engineTests = [
[engine(BLINK, '>=', 537)],
[engine(WEBKIT, '>=', 537)],
[engine(TRIDENT, '>=', 5)],
[engine(GECKO, '>=', 36)]
],
supported = function (tests) {
for (var i = 0; i < tests.length; i++) {
var test = tests[i];
if (Object.prototype.toString.call(test) === '[object Function]') {
if (test()) {
return true;
}
}
else if (Object.prototype.toString.call(test) === '[object Array]') {
if (supported(test)) {
return true;
}
}
else {
throw new Error('Unsupported test: ' + test);
}
}
return false;
};
if (supported(browserTests)) {
// Browser is fully supported
}
else if (supported(engineTests))
// Real browsers were not found, well that can happen with Android's default browser for example.
// In theory, we can test against rendering engine and it still *SHOULD* work.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment