Friday, March 7, 2014

jQuery get browser and os details

http://jquery.thewikies.com/browser/


$(document).ready(function () {
    $.browserTest = function (userAgent, z) {
        var u = 'unknown', x = 'X';
        var getBrowserName = function (userAgent, userAgents) {
            for (var i = 0; i < userAgents.length; i = i + 1) {
                userAgent = userAgent.replace(userAgents[i][0], userAgents[i][1]);
            }
            return userAgent;
        }
        var getBrowserDetails = function (userAgent, browserNameRegex, layouts, browserVersionRegex) {
            var r = {
                name: getBrowserName((browserNameRegex.exec(userAgent) || [u, u])[1], layouts)
            };
            r[r.name] = true;
            r.version = (browserVersionRegex.exec(userAgent) || [x, x, x, x])[3];
            if (r.name.match(/safari/) && r.version > 400) {
                r.version = '2.0';
            }
            if (r.name === 'presto') {
                r.version = ($.browser.version > 9.27) ? 'futhark' : 'linear_b';
            }
            r.versionNumber = parseFloat(r.version, 10) || 0;
            r.versionX = (r.version !== x) ? (r.version + '').substr(0, 1) : x;
            r.className = r.name + r.versionX;
            return r;
        }
        userAgent = (userAgent.match(/Opera|Navigator|Minefield|KHTML|Chrome/) ? getBrowserName(userAgent, [
            [/(Firefox|MSIE|KHTML,\slike\sGecko|Konqueror)/, ''],
            ['Chrome Safari', 'Chrome'],
            ['KHTML', 'Konqueror'],
            ['Minefield', 'Firefox'],
            ['Navigator', 'Netscape']
        ]) : userAgent).toLowerCase();
        console.log(userAgent);

        $.browser = getBrowserDetails(userAgent, /(camino|chrome|firefox|netscape|konqueror|lynx|msie|opera|safari)/, [], /(camino|chrome|firefox|netscape|netscape6|opera|version|konqueror|lynx|msie|safari)(\/|\s)([a-z0-9\.\+]*?)(\;|dev|rel|\s|$)/);
        console.log($.browser);

        $.layout = getBrowserDetails(userAgent, /(gecko|konqueror|msie|opera|webkit)/, [
            ['konqueror', 'khtml'],
            ['msie', 'trident'],
            ['opera', 'presto']
        ], /(applewebkit|rv|konqueror|msie)(\:|\/|\s)([a-z0-9\.]*?)(\;|\)|\s)/);
        console.log($.layout);

        $.os = {
            name: (/(win|mac|linux|sunos|solaris|iphone)/.exec(navigator.platform.toLowerCase()) || [u])[0].replace('sunos', 'solaris')
        };
        console.log($.os);
    }
    $.browserTest(navigator.userAgent);
});

No comments:

Post a Comment