Skip to content

Instantly share code, notes, and snippets.

@alexindigo
Created February 20, 2012 22:03
Show Gist options
  • Save alexindigo/1871785 to your computer and use it in GitHub Desktop.
Save alexindigo/1871785 to your computer and use it in GitHub Desktop.
set browser specific classes using jQuery
// {{{ win-safari hacks, scratch this,
// let's just expose platform/browser to css
(function()
{
var uaMatch = '', prefix = '';
if (navigator.userAgent.match(/Windows/))
{
$('html').addClass('x-win');
}
else if (navigator.userAgent.match(/Mac OS X/))
{
$('html').addClass('x-mac');
}
else if (navigator.userAgent.match(/X11/))
{
$('html').addClass('x-x11');
}
// browser
if (navigator.userAgent.match(/Chrome/))
{
uaMatch = ' Chrome/';
prefix = 'x-chrome';
}
else if (navigator.userAgent.match(/Safari/))
{
uaMatch = ' Version/';
prefix = 'x-safari';
}
else if (navigator.userAgent.match(/Firefox/))
{
uaMatch = ' Firefox/';
prefix = 'x-firefox';
}
else if (navigator.userAgent.match(/MSIE/))
{
uaMatch = ' MSIE ';
prefix = 'x-msie';
}
// add result preifx as browser class
if (prefix)
{
$('html').addClass(prefix);
// get major and minor versions
// reduce, reuse, recycle
uaMatch = new RegExp(uaMatch+'(\\d+)\.(\\d+)');
var uaMatch = navigator.userAgent.match(uaMatch);
if (uaMatch && uaMatch[1])
{
// set major only version
$('html').addClass(prefix+'-'+uaMatch[1]);
// set major + minor versions
$('html').addClass(prefix+'-'+uaMatch[1]+'-'+uaMatch[2]);
}
}
})();
// }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment