Skip to content

Instantly share code, notes, and snippets.

@astromac
Last active January 2, 2016 15:49
Show Gist options
  • Save astromac/8325565 to your computer and use it in GitHub Desktop.
Save astromac/8325565 to your computer and use it in GitHub Desktop.
Adds class to the html tag in order to address any styling quirks specific to those brosers. Example class might be "IE IE-9" or "iOS iO7-iPad".
// Detect IE and iOS and add browser class to document
var tag = document.getElementsByTagName('html')[0];
var ua = navigator.userAgent;
var version = '';
if (/msie (\d+)/i.test(ua)) {
version = 'IE IE-' + /msie (\d+)/i.exec(ua)[1];
if (tag.className.length > 0) {
tag.className = tag.className + ' ' + version;
} else {
tag.className = version;
}
} else if (/trident.*?; rv:(\d+)/i.test(ua)) {
version = 'IE IE-' + /trident.*?; rv:(\d+)/i.exec(ua)[1];
if (tag.className.length > 0) {
tag.className = tag.className + ' ' + version;
} else {
tag.className = version;
}
} else if (/\((\w+).*?OS (\d).*?like Mac OS X/i.test(ua)) {
version = 'iOS iOS' + /\((\w+).*?OS (\d).*?like Mac OS X/i.exec(ua)[2] + '-' + /\((\w+).*?OS (\d).*?like Mac OS X/i.exec(ua)[1];
if (tag.className.length > 0) {
tag.className = tag.className + ' ' + version;
} else {
tag.className = version;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment