Detecting Internet Explorer using JavaScript

Sometimes in JavaScript you need to detect whether the current user is using IE8 or below as generally support should be maintained for the previous three versions of IE (my opinion) and therefore the user may need to be prompted that perhaps they aren’t getting the best experience.
The script below checks the user’s browser capability for leading whitespace see: (http://stackoverflow.com/questions/8890460/how-to-detect-ie7-and-ie8-using-jquery-support) and then checks the browser version. This is because firefox doesn’t seem to support leading whitespace in all versions so the browser version then needs to be checked.

    
 if (!$.support.leadingWhitespace) {
            if ($.browser.msie && parseFloat($.browser.version) <= 8) {
                alert('This website is best viewed in Internet Explorer 9 and above, please upgrade your browser for the best experience.');
            }
        }

In this example an alert is shown but you could easily add a custom stylesheet or perform some other useful function.

Leave a Reply