jquery detection hasClass

hasClass

by Leo Lanese

HTML

<!doctype html>
<!--[if IE 8 ]>    <html lang="en" class="no-js ie-old ie8"> <![endif]-->
<!--[if IE 9 ]>    <html lang="en" class="no-js ie ie9"> <![endif]-->
<!--[if !IE]><!--> <html lang="en" class="no-js"> <!--<![endif]--> 
 <head></head>
 <body>
     <p class="test">
         When Modernizr.js runs, it removes the "no-js" class and replaces it with "js". 
         This is a simple way to know whether or not js support is enabled
     </p>
 </body>
</html>

CSS

/* js = green */
.test { 
    background: green; 
    display:inline-block; 
    padding: 2%; 
    font-size:2rem;
}

/* no-js = red */
.no-js .test { 
    background: red; 
    display:inline-block; 
    padding: 2%; 
    font-size:2rem;
}

* {
    margin:0;
}

JavaScript

var hasClass = function (elem, className) {
    return new RegExp(' ' + className + ' ').test(' ' + elem.className + ' ');    
}
// hasClass test
hasClass(document.documentElement, 'js');



/*!
* Modernizr v2.5.3
* www.modernizr.com
*
* Copyright (c) Faruk Ates, Paul Irish, Alex Sexton
* Available under the BSD and MIT licenses: www.modernizr.com/license/
*/
/*
* Modernizr tests which native CSS3 and HTML5 features are available in
* the current UA and makes the results available to you in two ways:
* as properties on a global Modernizr object, and as classes on the
* <html> element. This information allows you to progressively enhance
* your pages with a granular level of control over the experience.
*
* Modernizr has an optional (not included) conditional resource loader
* called Modernizr.load(), based on Yepnope.js (yepnopejs.com).
* To get a build that includes Modernizr.load(), as well as choosing
* which tests to include, go to www.modernizr.com/download/
*
* Authors Faruk Ates, Paul Irish, Alex Sexton
* Contributors Ryan Seddon, Ben Alman
*/
window.Modernizr = (function(window, document, undefined) {

    var version = '2.5.3',

        Modernizr = {},

        // option for enabling the HTML classes to be added
        enableClasses = true,

        docElement = document.documentElement,

        /**
         * Create our "modernizr" element that we do most feature tests on.
         */
        mod = 'modernizr',
        modElem = document.createElement(mod),
        mStyle = modElem.style,

        /**
         * Create the input element for various Web Forms feature tests.
         */
        inputElem = document.createElement('input'),

        smile = ':)',

        toString = {}.toString,

        // List of property values to set for css tests. See ticket #21
        prefixes = ' -webkit- -moz- -o- -ms- '.split(' '),

        // Following spec is to expose vendor-specific style properties as:
        // elem.style.WebkitBorderRadius
        // and...