styler
styler

Reputation: 16521

how can i test if internet explorer version is version 7 or version 8?

I want to test if IE is version 7 or 8 and if it is prevent a specific piece of code running?

I've tried the following code but this doesnt seem to work:

if($.browser.msie && parseInt($.browser.version, 10) <= 8) {
        $(document).on('mouseenter', '.thumb', function () {
          $(this).find('.bgg').stop().animate({ opacity : 1 });
        });

        $(document).on('mouseleave', '.thumb', function () {
          $(this).find('.bg').stop().animate({ opacity : .5 });
        });
      }

Ideally I really dont want to use this kind of detection but in this case it has to be used.

Upvotes: 2

Views: 131

Answers (4)

kennebec
kennebec

Reputation: 104850

IE8 can be used as and act like IE7-

to distinguish, you can test document.documentMode

adding a property to the navigator object saves having to test again

//(Run= {};

if(window.addEventListener){
    Run.handler= function(who, typ, fun){
        if(who && who.addEventListener) who.addEventListener(typ, fun, false);
    }// all browsers except IE8 and below
}
else if(window.attachEvent){
    /*@cc_on
    @if(@_jscript_version>5.5){
        navigator.IEmod= document.documentMode?
        document.documentMode:window.XMLHttpRequest? 7:6;
    }
    @end
    @*/
    Run.handler= function(who, typ, fun){
        if(who && who.attachEvent){
            who.detachEvent('on'+typ, fun);
            who.attachEvent('on'+typ, fun);
        }
    }
}

Upvotes: 0

Code Maverick
Code Maverick

Reputation: 20425

I've had to UA sniff for IE in my projects due to the requirement of having only one script file. We don't want the extra http request that @Kolink's method requires, nor do we want to split functionality. For that I would simply use:

var ltie9 = $.browser.msie && parseInt($.browser.version, 10) < 9;

and then do whatever you want by using:

if (ltie9) { ... }

I've got a jsFiddle that shows several different IE detections up to IE10 just to demonstrate.

Upvotes: 2

KooiInc
KooiInc

Reputation: 123016

Use object detection. IE7 doesn't provide a querySelector method, so for example replace

if ($.browser.msie && parseInt($.browser.version, 10) <= 8)

with

 if (document.all && !document.querySelector)

Here are some more ideas

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324840

Foolproof method:

<!--[if lte IE 8]><script type="text/javascript">
    // specific code for IE8 and below goes here.
</script><![endif]-->

Upvotes: 4

Related Questions