How can I determine which version of IE a user is running in JavaScript?

后端 未结 12 1878
生来不讨喜
生来不讨喜 2021-02-04 04:48

In some existing code there is a test to see if the user is running IE, by checking if the object Browser.Engine.trident is defined and returns true.

But how can I deter

相关标签:
12条回答
  • 2021-02-04 05:16

    Well... here is what I came up after thinking for a while. just wanted to find a simple solution.

    if (navigator.appName == 'Microsoft Internet Explorer') {
            // Take navigator appversion to an array & split it 
            var appVersion = navigator.appVersion.split(';');
            // Get the part that you want from the above array  
            var verNumber = appVersion[1];
    
            alert(verNumber);
    }
    

    It returns ex:- MSIE 10.0, MSIE 9.0, MSIE 8.0

    further extend, if you want to check if it's "lower than" or "greater than" IE version, you can slightly modify

    if (navigator.appName == 'Microsoft Internet Explorer') {
            var appVersion = navigator.appVersion.split(';');
            var verNumber = appVersion[1];
            // Reaplce "MSIE " from the srting and parse it to integer value        
            var IEversion = parseInt(verNumber.replace('MSIE ', ''));
    
            if(IEversion <= 9){
                alert(verNumber);
            }
    }
    

    got the base idea from w3schools, hope this will help some one... :D

    0 讨论(0)
  • 2021-02-04 05:16

    This should give you more details than you'll want:

    var agent = navigator.userAgent;
    var msiePattern = /.*MSIE ((\d+).\d+).*/
    if( msiePattern.test( agent ) ) {
      var majorVersion = agent.replace(msiePattern,"$2");
      var fullVersion = agent.replace(msiePattern,"$1");
      var majorVersionInt = parseInt( majorVersion );
      var fullVersionFloat = parseFloat( fullVersion );
    }
    
    0 讨论(0)
  • 2021-02-04 05:16

    As no-one seems to have said it yet:

    The test is needed inside a JavaScript function so a conditional comment doesn't seem suitable.

    You can easily put a conditional comment — a JScript conditional comment, not an HTML one — inside a function:

    function something() {
        var IE_WIN= false;
        var IE_WIN_7PLUS= false;
        /*@cc_on
        @if (@_win32)
            IE_WIN= true;
            @if (@_jscript_version>=5.7)
                IE_WIN_7PLUS = true;
            @end
        @end @*/
        ...
    }
    

    It's more typical to do the test once at global level though, and just check the stored flags thereafter.

    CCs are more reliable than sifting through the mess that the User-Agent string has become these days. String matching methods on navigator.userAgent can misidentify spoofing browsers such as Opera.

    Of course capability sniffing is much better for cross-browser code where it's possible, but for some cases — usually bug fix workarounds — you do need to identify IE specifically, and CCs are probably the best way to do that today.

    0 讨论(0)
  • 2021-02-04 05:21

    If you are already using jQuery in a pre-1.9 version AND you don't need to detect IE 11, you can do this:

    if (jQuery.browser.msie == true) { 
    if (jQuery.browser.version == 7.0)
      // .. do something for 7.0
    else 
      // .. do something for < 7.0
    }
    
    0 讨论(0)
  • 2021-02-04 05:23

    If you really want to be sure you are using IE and a specific version then you could obviously use IE's conditional tags to only run certain code within IE. It's not really that pretty but at least you can be sure that it is really IE and not some spoofed version.

    <script>
        var isIE = false;
        var version = -1;
    </script>
    <!--[if IE 6]>
        <script>
            isIE = true;
            version = 6
        </script>
    <![endif]-->
    <!--[if IE 7]>
        <script>
            isIE = true;
            version = 7
        </script>
    <![endif]-->
    

    It's pretty self explanatory. In IE6 isIE is true and version is 6, In IE7 isIE is true and version is 7 otherwise isIE is false and version is -1

    Alternatively you could just roll your own solution using code plagarised from jQuery.

    var userAgent = navigator.userAgent.toLowerCase();
    var version = (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
    var isIE = /msie/.test( userAgent ) && !/opera/.test( userAgent ),    
    
    0 讨论(0)
  • 2021-02-04 05:23

    This is probably going to get voted down, because it's not directly answering the question, but... You should not be writing browser-specific code. There's very little you can't do while coding for most widely-accepted browsers.

    EDIT: The only time I found it useful to have conditional comments was when I needed to include ie6.css or ie7.css.

    0 讨论(0)
提交回复
热议问题