2

I use these three lines of code to detect lesser internet explorer versions.

<!--[if lt IE 7]><html class="lt-ie8 lt-ie7 lesser-browser get-lost-ie-user please-leave-now"><![endif]-->
<!--[if IE 7]><html class="lt-ie8 ie7"> <![endif]-->
<!--[if !IE]><!--><html class="gtie7"><!--<![endif]-->

Using javascript, how can I find out whether or not the lt-ie8 class is enabled for the <html> element?

ben
  • 653
  • 2
  • 7
  • 16
  • 1
    No demonstrated research effort, and it's not useful because it's using browser detection which is almost never the right way of solving issues with missing features. – Kevin B Oct 30 '13 at 16:10
  • @KevinB i actually am using this to block ie6 and ie7, because anyone still using them... well I am not going to say. I would get arrested if I did – ben Oct 30 '13 at 16:13
  • @KevinB I still need to warn users about their ungodly habits, so that they might upgrade and then re-visit with their firefox 27 super browser – ben Oct 30 '13 at 16:17

6 Answers6

3

Using JavaScript, you can use:

document.getElementsByTagName("html")[0].classList.contains("lt-ie8");

That gives the list of classes. If classList is not implemented, it can be checked this way:

if (document.getElementsByTagName("html")[0].className.indexOf("lt-ie8") != -1)
  // The browser...

Or use jQuery's $.hasClass() if there's a class set:

$("html").hasClass("lt-ie8");

So, you can use it this way:

if ($("html").hasClass("lt-ie8"))
  // Class is enabled.
else
  // Class is not there.
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
1

I would use document.body.parentNode to get your <html> tag as the DOM does not need to be searched like the other methods. Reimplementing the jQuery hasClass method is the best way to check if a javascript class has a class in legacy browsers since the other methods normally don't work when there are multiple classes or if the class is a substring of another class.

Source

function hasClass(elem, klass) {
    return (" " + elem.className + " ").indexOf(" " + klass + " ") > -1;
}

if (hasClass(document.body.parentNode, "lt-ie8")) {
    // ...
}

If you're only targeting modern browsers then using classList is the best option.

if (document.body.parentNode.classList.contains("lt-ie8")) {
    // ...
}
Community
  • 1
  • 1
Daniel Imms
  • 47,944
  • 19
  • 150
  • 166
0
if (document.getElementsByClassName('lt-ie8').length > 0) {
    // EDIT due to the important comment below
    //console.log("you are using a crappy browser");
    alert("you are using a crappy browser");
}
0
var isBrowserCrappy = document.getElementsByTagName('html')[0].className.indexOf('lt-ie8') !== -1;
vzwick
  • 11,008
  • 5
  • 43
  • 63
0

Try:

if( document.getElementsByTagName("html")[0].className == 'gtie7' )
    alert( 'html has class "gtie7"' );
iambriansreed
  • 21,935
  • 6
  • 63
  • 79
0

Since you've got the question tagged with jquery just use:

if ($(".lt-ie8").length > 0) {
    . . . do stuff . . .
}

I tested it in Firefox, IE7, IE9, and Chrome . . . works fine in all of them . . .

Edit: $("html.lt-ie8").length is even more efficient . . .

Note: in HTML4 and XHTML, class is not a valid attribute for the <html> tag. Even though it seems to work, you might want to consider moving it to the <body> tag instead.

talemyn
  • 7,822
  • 4
  • 31
  • 52