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?

7
  • 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). Commented Apr 2, 2013 at 3:25
  • @Lee Taylor this detection is needed for fixing some buggy behaviour in IE7 & lower, no feature involved Commented Apr 2, 2013 at 4:00
  • 2
    What buggy behaviour can't be feature detected? Far better to ask how to work around the missing features or buggy behaviour. Commented Apr 2, 2013 at 4:01
  • In IE7 and older, there is a buggy behaviour where clicking on a button does not cause its form to be submitted rommelsantor.com/clog/2012/03/12/fixing-the-ie7-submit-value Commented Apr 2, 2013 at 4:08
  • 1
    Wouldn't <button type="submit">...</button> work for you? Commented Apr 2, 2013 at 4:13

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that conditional comments have been removed in IE 10, so this method can't be used to detect IE versions 10 or above.
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';
}

1 Comment

wow, thanks jack that works very well for detect button submit issue in IE7
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

Comments

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.

Comments

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.

1 Comment

Thanks RobG, will try this, yeah can't use input type submit for some styling reason ><
0

U Can detect by using following condition.

if (!$.support.leadingWhitespace) {
    //IE7 stuff
}

Comments

-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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.