15

Is there any way to detect IE7?

I don't have any problems with my codes in IE8, but I have a problem with IE7.

So what I thought is that I can add a class with jquery when a browser is IE7 detecting it by javascript.

I want to change from

<div id="system">

to

<div id="system" class="ie7">

Thanks in advance.

shin
  • 31,901
  • 69
  • 184
  • 271
  • 2
    Is this issue related to styles or JavaScript? If this is a style-related thing, you should consider using conditional comments like suggested. If this is a JavaScript thing, you should read about feature detection, which beats browser sniffing in a lot of ways. – jwueller Sep 08 '10 at 17:12

7 Answers7

49

if you really want to solve this by using javascript you might want to check the version like this:

if (navigator.appVersion.indexOf("MSIE 7.") != -1)
    $('#system').addClass('ie7');
Alex
  • 5,240
  • 1
  • 31
  • 38
  • +1 for a solution that does not depend on the available options of a javascript framework, or conditional comments. – klewis Apr 30 '14 at 09:30
10

You could use an IE conditional comment to add the class via javascript, something like this:

<!--[if IE 7]>
<script type="text/javascript">
$(document).ready(function() {
    $('#system').addClass('ie7');
});
</script>
<![endif]-->
Jimmy
  • 555
  • 4
  • 15
9

You can do it just with HTML:

css

.ie7{
   padding: 0;
   color: red;
}

html

<!--[if IE 7 ]> <div id="system" class="ie7"> <![endif]-->
<!--[if (gt IE 7)|!(IE)]><!--> <div id="system"> <!--<![endif]-->
</div>

This creates the div with the class ie7 if executed in internet explorer 7. All other browser and IE > 7 would just create that div without the class.

jAndy
  • 231,737
  • 57
  • 305
  • 359
  • I think he is wanting a jQuery solution. However, your solution is a better way to go since a user could have javascript disabled. – Todd Moses Sep 08 '10 at 17:13
  • 3
    +1. I usually do this in one place, on ``, and use a CSS containment selector like `body.ie7 #system`, to avoid having to repeat conditional comments throughout the document. – bobince Sep 08 '10 at 17:20
9

To detect ie7 just use

if($.browser.msie && parseFloat($.browser.version) < 8){
    //do other stuff
    return;
}

To do what you want with it just:

if($.browser.msie && parseFloat($.browser.version) < 8){
    $('#system').addClass('ie7');
}
Boro
  • 7,913
  • 4
  • 43
  • 85
Todd Moses
  • 10,969
  • 10
  • 47
  • 65
3

Try this:

<!--[if IE 7]>
<script type="text/javascript">
  $('#system').addClass('ie7');
</script>
<![endif]-->
Enlightened
  • 239
  • 2
  • 13
0

Please be informed that IE8 compat view will also have <!--[if IE 7 ]> as true

So you should do a second level test on the document mode, if you dont want the changes to be reflecting in IE8 Compat view.

<!--[if IE 7]>
    <script type="text/javascript"> 
   try
    {
    if(document.documentMode!=8){
     //Your code comes here
    }
    }
    catch(exception){
    }
   </script>
<!--<![endif]-->
Vignesh Subramanian
  • 7,161
  • 14
  • 87
  • 150