How to detect IE version with JavaScript

Sometimes you cannot use conditionals to detect version of Internet Explorer. So I made this JavaScript that does the same thing really.

This script adds a class with the version of IE to the HTML tag.

<script>
if (/\bMSIE 6/.test(navigator.userAgent) && !window.opera) {
 document.documentElement.className+=' ie6';
} else if (navigator.appVersion.indexOf("MSIE 7.") != -1) {
  document.documentElement.className+=' ie7';
} else if (navigator.appVersion.indexOf("MSIE 8.") != -1) {
  document.documentElement.className+=' ie8';
} else if (navigator.appVersion.indexOf("MSIE 9.") != -1) {
  document.documentElement.className+=' ie9';
} else if (/*@cc_on!@*/false && document.documentMode === 10) {
  document.documentElement.className+=' ie10';
}
</script>

Share your thoughts on this

Your email address will not be published.