How to detect IE7 with jQuery possibly using jQuery.browser?


Got a method

if ($.browser.msie  && parseInt($.browser.version, 10) === 7) {
  alert('IE7'); 
} else {
  alert('Non IE7');
}

-- update

Please note that $.browser is removed from jQuery 1.9


See $.browser. This feature has been deprecated and you should consider $.support instead.

To answer the question, this is a rock solid technique primarily for use in stylesheets but can also be used easily to check browser version, or best still part of a selector.

  1. Use the following markup (from HTML5 Boilerplate)

    <!--[if lt IE 7]><html class="ie6"><![endif]-->
    <!--[if IE 7]><html class="ie7"><![endif]-->
    <!--[if IE 8]><html class="ie8"><![endif]-->
    <!--[if gt IE 8]><!--><html><!--<![endif]-->
    <head>
    
  2. Use a selector with hasClass in jQuery

    $('html').hasClass('ie7');
    $('html.ie7 .myclass').dostuff();
    
  3. And use as part of a normal selector in css

    .mydiv {max-height:50px}
    .ie6 .mydiv {height:50px} /* ie6 max-height fix */
    

All other things considered:

if ($.browser.msie && $.browser.version == 7) {

    //do something

    };

Should work. Whether or not it is the right way to go about things is another question.