Skip to content

Instantly share code, notes, and snippets.

@mr-moon
Last active August 29, 2015 14:05

Revisions

  1. mr-moon revised this gist Aug 21, 2014. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    var
    INTERNET_EXPLORER = 'Internet Explorer',
    INTERNET_EXPLORER = 'Internet Explorer',
    FIREFOX = 'Firefox',
    CHROME = 'Chrome',
    OPERA = 'Opera',
    @@ -69,10 +69,10 @@ var
    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.
    }
    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.
    }
  2. mr-moon created this gist Aug 21, 2014.
    78 changes: 78 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    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.
    }