最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to detect IE7 and lower using jQuery.support? - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question edited Apr 2, 2013 at 3:26 icaru12 asked Apr 2, 2013 at 3:15 icaru12icaru12 1,58216 silver badges21 bronze badges 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). – Lee Taylor Commented Apr 2, 2013 at 3:25
  • @Lee Taylor this detection is needed for fixing some buggy behaviour in IE7 & lower, no feature involved – icaru12 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. – RobG 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 – icaru12 Commented Apr 2, 2013 at 4:08
  • 1 Wouldn't <button type="submit">...</button> work for you? – Ja͢ck Commented Apr 2, 2013 at 4:13
 |  Show 2 more comments

7 Answers 7

Reset to default 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.

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';
}

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

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.

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.

U Can detect by using following condition.

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

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...
发布评论

评论列表(0)

  1. 暂无评论