Detect IE version (prior to v9) in JavaScript

前端 未结 30 1368
半阙折子戏
半阙折子戏 2020-11-22 08:44

I want to bounce users of our web site to an error page if they\'re using a version of Internet Explorer prior to v9. It\'s just not worth our time and money to

相关标签:
30条回答
  • 2020-11-22 09:03

    Your code can do the check, but as you thought, if someone try to access your page using IE v1 or > v19 will not get the error, so might be more safely do the check with Regex expression like this code below:

    var userAgent = navigator.userAgent.toLowerCase();
    // Test if the browser is IE and check the version number is lower than 9
    if (/msie/.test(userAgent) && 
        parseFloat((userAgent.match(/.*(?:rv|ie)[\/: ](.+?)([ \);]|$)/) || [])[1]) < 9) {
      // Navigate to error page
    }
    
    0 讨论(0)
  • 2020-11-22 09:04

    This is my preferred way of doing it. It gives maximum control. (Note: Conditional statements are only supported in IE5 - 9.)

    First set up your ie classes correctly

    <!DOCTYPE html>
    <!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
    <!--[if IE 7]>    <html class="lt-ie9 lt-ie8"> <![endif]-->
    <!--[if IE 8]>    <html class="lt-ie9"> <![endif]-->
    <!--[if gt IE 8]><!--> <html> <!--<![endif]-->    
    <head>
    

    Then you can just use CSS to make style exceptions, or, if you require, you can add some simple JavaScript:

    (function ($) {
        "use strict";
    
        // Detecting IE
        var oldIE;
        if ($('html').is('.lt-ie7, .lt-ie8, .lt-ie9')) {
            oldIE = true;
        }
    
        if (oldIE) {
            // Here's your JS for IE..
        } else {
            // ..And here's the full-fat code for everyone else
        }
    
    }(jQuery));
    

    Thanks to Paul Irish.

    0 讨论(0)
  • 2020-11-22 09:04

    I'm going to recommend not rewriting this code for the umpteenth time. I would recommend you use the Conditionizr library (http://conditionizr.com/) which is capable of testing for specific IE versions as well as other browsers, operating systems, and even the presence or absence of Retina displays.

    Include the code for only the specific tests you need and you also gain the benefit of a tested library which has been through many iterations (and which would be easy to upgrade without breaking your code).

    It also meshes nicely with Modernizr which can handle all of those cases where you are better off testing for a specific capability rather than a specific browser.

    0 讨论(0)
  • 2020-11-22 09:04

    I realise I am a little late to the party here, but I had been checking out a simple one line way to provide feedback on whether a browser is IE and what version from 10 down it was. I haven't coded this for version 11, so perhaps a little amendment will be needed for that.

    However this is the code, it works as an object that has a property and a method and relies on object detection rather than scraping the navigator object (which is massively flawed as it can be spoofed).

    var isIE = { browser:/*@cc_on!@*/false, detectedVersion: function () { return (typeof window.atob !== "undefined") ? 10 : (typeof document.addEventListener !== "undefined") ? 9 : (typeof document.querySelector !== "undefined") ? 8 : (typeof window.XMLHttpRequest !== "undefined") ? 7 : (typeof document.compatMode !== "undefined") ? 6 : 5; } };
    

    The usage is isIE.browser a property that returns a boolean and relies on conditional comments the method isIE.detectedVersion() which returns a number between 5 and 10. I am making the assumption that anything lower than 6 and you are in serious old school territory and you will something more beefy than a one liner and anything higher than 10 and you are in to newer territory. I have read something about IE11 not supporting conditional comments but I've not fully investigated, that is maybe for a later date.

    Anyway, as it is, and for a one liner, it will cover the basics of IE browser and version detection. It's far from perfect, but it is small and easily amended.

    Just for reference, and if anyone is in any doubt on how to actually implement this then the following conditional should help.

    var isIE = { browser:/*@cc_on!@*/false, detectedVersion: function () { return (typeof window.atob !== "undefined") ? 10 : (typeof document.addEventListener !== "undefined") ? 9 : (typeof document.querySelector !== "undefined") ? 8 : (typeof window.XMLHttpRequest !== "undefined") ? 7 : (typeof document.compatMode !== "undefined") ? 6 : 5; } };
    
    /* testing IE */
    
    if (isIE.browser) {
      alert("This is an IE browser, with a detected version of : " + isIE.detectedVersion());
    }
    
    0 讨论(0)
  • 2020-11-22 09:06

    Here is the way AngularJS checks for IE

    /**
     * documentMode is an IE-only property
     * http://msdn.microsoft.com/en-us/library/ie/cc196988(v=vs.85).aspx
     */
    var msie = document.documentMode;
    
    if (msie < 9) {
        // code for IE < 9
    }
    
    0 讨论(0)
  • 2020-11-22 09:06
    var Browser = new function () {
        var self = this;
        var nav = navigator.userAgent.toLowerCase();
        if (nav.indexOf('msie') != -1) {
            self.ie = {
                version: toFloat(nav.split('msie')[1])
            };
        };
    };
    
    
    if(Browser.ie && Browser.ie.version > 9)
    {
        // do something
    }
    
    0 讨论(0)
提交回复
热议问题