Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 817
hyperbeam22 Avatar asked Apr 02 '13 03:04

hyperbeam22


3 Answers

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.

like image 200
Praveen Kumar Purushothaman Avatar answered Nov 19 '22 17:11

Praveen Kumar Purushothaman


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';
}
like image 42
Ja͢ck Avatar answered Nov 19 '22 17:11

Ja͢ck


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

like image 1
Sam Deering Avatar answered Nov 19 '22 17:11

Sam Deering