Skip to content

Instantly share code, notes, and snippets.

@dustinleer
Created September 21, 2021 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustinleer/2869a88da1509c6f01c4d87437fc598c to your computer and use it in GitHub Desktop.
Save dustinleer/2869a88da1509c6f01c4d87437fc598c to your computer and use it in GitHub Desktop.
Adds body class based on browser detection
function GetIEVersion() {
var sAgent = window.navigator.userAgent;
var Idx = sAgent.indexOf("MSIE");
// If IE, return version number.
if (Idx > 0) {
return parseInt(sAgent.substring(Idx+ 5, sAgent.indexOf(".", Idx)));
// If IE 11 then look for Updated user agent string.
} else if (!!navigator.userAgent.match(/Trident\/7\./)) {
return 11;
} else {
return 0; //It is not IE
}
}
var ua = navigator.userAgent.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i),
browser;
if ( navigator.userAgent.match(/Edge/i) ) {
browser = 'edge';
} else if ( navigator.userAgent.match(/Trident.*rv[ :]*11\./i) ) {
browser = 'ie';
} else {
browser = ua[1].toLowerCase();
}
if (GetIEVersion() > 0) {
$('body').addClass(browser);
} else {
$('body').addClass(browser);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment