Skip to content

Instantly share code, notes, and snippets.

@dbergey
Created December 6, 2011 19:50
Show Gist options
  • Save dbergey/1439654 to your computer and use it in GitHub Desktop.
Save dbergey/1439654 to your computer and use it in GitHub Desktop.
Browser-Specific Classes
// add OS-specific classes for targeting a whole set of browser on one OS.
$('html').addClass('browser-os-'+ (
navigator.userAgent.match(/Macintosh/) ? 'macos' :
navigator.userAgent.match(/Windows/) ? 'windows' :
navigator.userAgent.match(/Linux/) ? 'linux' :
'unknown')
)
// now add more general browser classes for targeting all version of any one browser. IE disabled since conditional comments added.
.addClass('browser-'+ (
$.browser.msie ? 'ie' :
$.browser.mozilla ? 'ff' :
$.browser.webkit ? 'wk' :
$.browser.opera ? 'op' :
'unknown')
)
// make the classes more concise than the tokens jQuery returns, also add version
.addClass('browser-'+ (
$.browser.msie ? 'ie-' + navigator.userAgent.match(/MSIE ([\d]+)/ )[1] :
$.browser.mozilla ? 'ff-' + navigator.userAgent.match(/Firefox\/(\d\.\d?)/)[1] :
$.browser.webkit ? 'wk-' + navigator.userAgent.match(/WebKit\/(\d+)/)[1] :
$.browser.opera ? 'op-' + navigator.userAgent.match(/Version\/([\d.]+)/)[1] :
'unknown').replace('.', '-')
);
@dbergey
Copy link
Author

dbergey commented Dec 6, 2011

Use Modernizr when you can. This is for purely visual inconsistencies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment