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

最后都变了- 提交于 2019-11-29 23:19:53

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)
}

Your code looks fine, but you forgot to set the radix parameter in parseInt:

if ($.browser.msie && parseInt($.browser.version, 10) > 9){

  // you need to wait a couple years to test if it works...
  alert("I'm IE10 or 11...");

}

This cannot cause any errors

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>

There's an even easier method.

Since this is a comment, it will be ignored by all other browsers except IE.

var IE;
//@cc_on IE = navigator.appVersion;

Then just use this in you'r script to detect all IE versions.

if (IE) {
//This is IE.
}
else {
//This is NOT IE.
}

Or match against any version like this:

if (IE <= 6) {}, if (IE < 9) }, if (IE == 7) {} etc etc...

Here's the source where i found these types of conditionals: http://www.javascriptkit.com/javatutors/conditionalcompile.shtml

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
}
<!--[if (IE 9)|!(IE)]><!--> Script here <!--<![endif]-->

I found cascading it works great for multibrowser detection. This code was used to change a fade to show/hide in ie 8 7 6.

$(document).ready(function(){
    if(jQuery.browser.msie && jQuery.browser.version.substring(0, 1) == 8.0)
         { 
             $(".glow").hide();
            $('#shop').hover(function() {
        $(".glow").show();
    }, function() {
        $(".glow").hide();
    });
         }
         else
         { if(jQuery.browser.msie && jQuery.browser.version.substring(0, 1) == 7.0)
         { 
             $(".glow").hide();
            $('#shop').hover(function() {
        $(".glow").show();
    }, function() {
        $(".glow").hide();
    });
         }
         else
         {if(jQuery.browser.msie && jQuery.browser.version.substring(0, 1) == 6.0)
         { 
             $(".glow").hide();
            $('#shop').hover(function() {
        $(".glow").show();
    }, function() {
        $(".glow").hide();
    });
         }
         else
         { $('#shop').hover(function() {
        $(".glow").stop(true).fadeTo("400ms", 1);
    }, function() {
        $(".glow").stop(true).fadeTo("400ms", 0.2);});
         }
         }
         }
       });
<!--[if IE 8]>
  <script type="text/javascript">
    // this is only executed for IE 8
  </script>
<![endif]-->

or you could do this

<!--[if IE 9]>
  <script type="text/javascript">
    // this is only executed for IE 9
  </script>
<![endif]-->
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!