Skip to content

Instantly share code, notes, and snippets.

@danrichards
Created May 23, 2016 19:14
Show Gist options
  • Save danrichards/616b2b29f8a1334c0b1bde0b761e885b to your computer and use it in GitHub Desktop.
Save danrichards/616b2b29f8a1334c0b1bde0b761e885b to your computer and use it in GitHub Desktop.
Underscore Mixin for IE (Internet Explorer) browser detection.
// https://jsfiddle.net/danrichards/16mxh9th/
_.mixin({
isIE: function(mixed) {
if (_.isUndefined(mixed)) {
mixed = [/MSIE\s7\./, /MSIE\s8\./, /MSIE\s9\./, /MSIE\s10\./, /Trident.*rv\:11\./];
} else if (_.isArray(mixed)) {
mixed = mixed.map(function(browser) {
return new RegExp('MSIE\s'+browser.toString()+'\.', 'g');
});
} else if (_.isNumber(mixed) ) {
switch (mixed) {
case 11:
mixed = [new RegExp('Trident.*rv\:11\.', 'g')];
break;
default:
mixed = [new RegExp('MSIE\s'+mixed.toString()+'\.', 'g')];
}
}
if (mixed.length == 0) {
return false;
}
return mixed.reduce(function (previous, current, index) {
return previous + (!! window.navigator.userAgent.match(current) ? 1 : 0);
}, null) > 0;
}
});
console.log(_.isIE());
console.log(_.isIE([7,8,9]));
console.log(_.isIE(11));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment