How to detect IE7 with jQuery possibly using jQuery.browser?
-
6Browser detection=code smellspender– spender2010-07-02 12:14:19 +00:00Commented Jul 2, 2010 at 12:14
-
1Counter-question: what do you need to differently in IE7?Piskvor left the building– Piskvor left the building2010-07-02 12:16:50 +00:00Commented Jul 2, 2010 at 12:16
-
3@Marcel if you're checking the browser in order to make a page layout or behavior work better, it's OK of a spoofed user agent string defeats the code. It's also OK if a user uses Firebug to edit a page and make all the buttons stop working; it's clearly something they're doing just to entertain themselves :-)Pointy– Pointy2010-07-02 12:27:44 +00:00Commented Jul 2, 2010 at 12:27
-
@Pointy – Haha, yeah, you're right about that. :)Marcel Korpel– Marcel Korpel2010-07-02 12:45:27 +00:00Commented Jul 2, 2010 at 12:45
-
7@spender Welcome to the real world.Sliq– Sliq2013-02-06 15:19:34 +00:00Commented Feb 6, 2013 at 15:19
3 Answers
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
4 Comments
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.
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>Use a selector with hasClass in jQuery
$('html').hasClass('ie7'); $('html.ie7 .myclass').dostuff();And use as part of a normal selector in css
.mydiv {max-height:50px} .ie6 .mydiv {height:50px} /* ie6 max-height fix */
6 Comments
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.