Skip to content

Instantly share code, notes, and snippets.

@pixelhijack
Last active August 29, 2015 14:01
Show Gist options
  • Save pixelhijack/f96555e813053fd3910f to your computer and use it in GitHub Desktop.
Save pixelhijack/f96555e813053fd3910f to your computer and use it in GitHub Desktop.
/*==============================
BROWSER DETECTION
================================
quick: console.log(/WebKit/.test(navigator.userAgent)) */
function getBrowser(){
var ua = window.navigator.userAgent.toLowerCase();
var ver = window.navigator.appVersion.toLowerCase();
var name = 'unknown';
if (ua.indexOf("msie") != -1){
if (ver.indexOf("msie 6.") != -1){
name = 'ie ie6';
}else if (ver.indexOf("msie 7.") != -1){
name = 'ie ie7';
}else if (ver.indexOf("msie 8.") != -1){
name = 'ie ie8';
}else if (ver.indexOf("msie 9.") != -1){
name = 'ie ie9';
}else if (ver.indexOf("msie 10.") != -1){
name = 'ie ie10';
}else{
name = 'ie';
}
}else if(ua.indexOf('trident/7') != -1){
name = 'ie ie11';
}else if (ua.match(/opera|opr\//)){
//as opera now using chromium engine, ua string shows as 'chrome' without this hack:
//http://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser
name = 'opera';
}else if (ua.indexOf('chrome') != -1){
name = 'chrome';
}else if (ua.indexOf('safari') != -1){
name = 'safari';
}else if (ua.indexOf('firefox') != -1){
name = 'firefox';
}
return name;
};
$('html').addClass(getBrowser());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment