6

Currently I'm using jQuery.browser to detect IE7 and lower

if ($.browser.msie && parseInt($.browser.version) <= 7) {
    //codes
}

but jQuery.browser was deprecated in jQuery 1.3 and removed in jQuery 1.9, I read from jQuery website that we should use feature detection instead (jQuery.support).

So, how to detect IE7 and lower using jQuery.support?

icaru12
  • 1,522
  • 16
  • 21
  • 2
    It's advised that rather than detect the browser that you detect the feature(s) that you need, or to detect (the lack thereof). – Lee Taylor Apr 02 '13 at 03:25
  • @Lee Taylor this detection is needed for fixing some buggy behaviour in IE7 & lower, no feature involved – icaru12 Apr 02 '13 at 04:00
  • 2
    What buggy behaviour can't be feature detected? Far better to ask how to work around the missing features or buggy behaviour. – RobG Apr 02 '13 at 04:01
  • In IE7 and older, there is a buggy behaviour where clicking on a button does not cause its form to be submitted http://rommelsantor.com/clog/2012/03/12/fixing-the-ie7-submit-value/ – icaru12 Apr 02 '13 at 04:08
  • 1
    Wouldn't `` work for you? – Ja͢ck Apr 02 '13 at 04:13
  • @Jack IE7 will submit the incorrect value of the button because it uses the innerHTML of the pair of tags rather than the value attribute – icaru12 Apr 02 '13 at 04:28
  • possible duplicate of [How to detect IE7 and IE8 using jQuery.support](http://stackoverflow.com/questions/8890460/how-to-detect-ie7-and-ie8-using-jquery-support) – Chris Moschini Apr 02 '13 at 04:59

7 Answers7

10

The best way is to use conditional comments and check it using jQuery's hasClass().

<!--[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]-->

And in your jQuery, check for IE 7:

// Check IE 7
if ($('html').hasClass('ie7');

This method cannot be spoofed no matter what. Also see: Conditional Stylesheets by Paul Irish.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • Note that conditional comments have been removed in IE 10, so this method can't be used to detect IE versions 10 or above. – Kevin Laity Jan 31 '14 at 22:00
2

This small function will tell you whether the button code is broken:

function isButtonBroken()
{
    var b = document.createElement('button');

    b.value = 1;
    b.appendChild(document.createTextNode('2'));

    return b.value === '12';
}
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
1

jQuery.Support does not give you the browser. As of jQuery 1.9 the $.browser function is deprecated. If your after a quick was the easiest way is to use the browsers native navigator object.

//check for IE7
if(navigator.appVersion.indexOf("MSIE 7.")!=-1)

Using Modernizr

//check for IE7 or less
if ($('html').hasClass('lt-ie7');

This is not recommended however as the browser can easily "spoof" this object. Using a library such as Moderizer to feature detect is modern approach. For more details info see: 5+ WAYS TO CHECK IE VERSION USING JAVASCRIPT/JQUERY

Sam Deering
  • 369
  • 1
  • 7
1

As others have said trying to detect a browser version is a bad idea and you should rely on feature detection.

That said the only reliable way to detect an IE browser rendering engine is using conditional comments. You can use this little snippet that'll get it for you:

var ie = (function(){

    var undef,
        v = 3,
        div = document.createElement('div'),
        all = div.getElementsByTagName('i');

    while (
        div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
        all[0]
    );

    return v > 4 ? v : undef;

}());

Keep in mind that this will only work in IE versions supporting conditional comments and it will not work in IE10 or above.

Aaron Powell
  • 24,927
  • 18
  • 98
  • 150
0

A no script solution can be based on giving the button a type of submit and a name that is the value you want to submit. Then, at the server, make the action dependent on the value of the name attribute, not the value, e.g.

<button type="submit" name="whatever" value="Click me 0">Click me 0</button>

Now every browser will submit the button as &whatever=Click+me+0. I don't have IE 7 to test with, but from what you've posted, that should work.

If you have multiple buttons in the form, only the one that is clicked to submit the form will be successful, so only that button's name and value will be submitted.

Of course the simplest of all is to use input type submit, but maybe you can't do that.

RobG
  • 142,382
  • 31
  • 172
  • 209
0

U Can detect by using following condition.

if (!$.support.leadingWhitespace) {
    //IE7 stuff
}
Gowtham
  • 31
  • 6
-1

You cannot detect the browser using jQuery.support.

Rather than checking the browser, you should check the feature of browsers you want to use.

For example, if you want to use ajax feature, you sould check the presence of XMLHttpRequest object by jQuery.support.ajax

var ajaxEnabled = $.support.ajax;
if (ajaxEnabled) {
  // do something
}

jQuery.browser document says that

jQuery.browser may be moved to a plugin in a future release of jQuery.

And also says that

Because $.browser uses navigator.userAgent to determine the platform, it is vulnerable to spoofing by the user or misrepresentation by the browser itself. It is always best to avoid browser-specific code entirely where possible. The $.support property is available for detection of support for particular features rather than relying on $.browser.


You can make your own browser-detection code. See below. But keep in mind that this code is vulnerable to spoofing as jQuery document says.

var ua = navigator.userAgent.toLowerCase();

var isOpera : (ua.indexOf('opera') >= 0) ? true : false;
var isFirefox : (ua.indexOf('firefox') >= 0) ? true : false;
var isMSIE : (ua.indexOf('msie') >= 0) ? true : false;
var isMSIE6 : (ua.indexOf('msie 6.0') >= 0) ? true : false;
var isMSIE7 : (ua.indexOf('msie 7.0') >= 0) ? true : false;
var isMSIE8 : (ua.indexOf('msie 8.0') >= 0) ? true : false;
var isMSIE9 : (ua.indexOf('msie 9.0') >= 0) ? true : false;
// and other browsers...
breadmj
  • 185
  • 11