if condition: if the browser is IE and IE browser version is older than 9

前端 未结 9 1101
时光说笑
时光说笑 2020-12-24 09:17

The if condition below I think it says - if the browser is IE and IE browser version is newer than 9, but I don\'t have IE 9 to test it so it is hard to know the correct out

相关标签:
9条回答
  • 2020-12-24 09:37

    or you could do this

    <!--[if IE 9]>
      <script type="text/javascript">
        // this is only executed for IE 9
      </script>
    <![endif]-->
    
    0 讨论(0)
  • 2020-12-24 09:38
    <!--[if IE 8]>
      <script type="text/javascript">
        // this is only executed for IE 8
      </script>
    <![endif]-->
    
    0 讨论(0)
  • 2020-12-24 09:39

    Firstly, you can get IE9 preview by downloading it from Microsoft's site: http://ie.microsoft.com/testdrive/

    Secondly, parseInt($.browser.version) > 9 would presumably check that the version is greater than 9, which of course it won't be until v10 is released. (you maybe intended >= ('greater than or equal to')?

    I usually tell people to avoid browser detection or browser-specific code. There are times when it is necessary, but they're quite rare. Most of the time the developer would be better served by knowing what was failing and working around it (tools like Modernizr really help for this sort of thing).

    However there are times when one simply has to do browser detection.

    In your case, if you really need to detect IE, don't do it the way you're doing (ie checking for version 9); it'd be better to check for older versions, and I'd suggest that a conditional comment would be the best way to do it.

    <script>
    var i_am_old_ie = false;
    <!--[if LT IE  9]>
    i_am_old_ie = true;
    <![endif]-->
    </script>
    

    Then your if() statement later on can just look like this:

    if(i_am_old_ie) {
       //do stuff for IE6/7/8
    } else {
       //do stuff for all other browsers (including IE9)
    }
    
    0 讨论(0)
  • 2020-12-24 09:44

    Instead of putting in a bunch of inline javascript you could just add an empty div for the version of ie you are targeting, so put the following at the top of your html, just inside the body tag:

    <!--[if LT IE  9]>
        <div id="iedetect" class="oldie"></div>
    <![endif]-->
    

    Then you can place an if statement in your jquery to check for that block.

    if ($("#iedetect").is(".oldie")) {
        //Do something
    } else {
       //Do Something else in all other browsers
    }
    
    0 讨论(0)
  • 2020-12-24 09:49

    Here is the Javascript version to test if the browser is IE:

    <script>
    
        function getIEVersion() {
            var rv = -1; // Return value assumes failure.
            if (navigator.appName == 'Microsoft Internet Explorer') {
                var ua = navigator.userAgent;
                var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
                if (re.test(ua) != null)
                    rv = parseFloat( RegExp.$1 );
            }
            return rv;
        }
    
    
        function checkVersion() {
            var ver = getIEVersion();
    
            if ( ver != -1 ) {
                if (ver <= 9.0) {
                    // do something
                }
            }
        }
    
        checkVersion();
    
    </script>
    

    This method is useful in case You are using HTML5 specific modules and want to implement some fallback functions in case the browser not supporting those features, for ex. <canvas>

    0 讨论(0)
  • 2020-12-24 09:51
    <!--[if (IE 9)|!(IE)]><!--> Script here <!--<![endif]-->
    
    0 讨论(0)
提交回复
热议问题