This tutorial provides the steps to add js and no-js classes in any child theme based on the Genesis Framework.
Why ‘js’ and ‘no-js’?
It provides the ability to apply different CSS rules depending on whether or not Javascript support is enabled.
It works as a conditional rule for CSS, we can learn more about this here in tutorial Conditional CSS in Genesis.
How to Setup?
Open functions.php and add the following. The snippet includes additional classes for frontpage too.
add_filter( 'body_class', 'designody_body_classes' ); /** * Add additional classes to the body element. * * @since 1.0.0 */ function designody_body_classes( $classes ) { if ( is_front_page() ) { //Add additional classes for front page in <body> $classes[] = 'has-some-class other-class'; } //Add 'no-js' class to the body class values. $classes[] = 'no-js'; return $classes; } add_action( 'genesis_before', 'designody_js_nojs_script', 1 ); /** * Echo out the script that changes 'no-js' class to 'js'. * * @since 1.0.0 */ function designody_js_nojs_script() { ?> <script> //<![CDATA[ (function(){ var c = document.body.classList; c.remove( 'no-js' ); c.add( 'js' ); })(); //]]> </script> <?php }
done.
Optional: Not Required. You can also make sure ‘no-js’ is removed when javascript is enabled by adding a line of jquery snippet.
Open your .js file like genesis-sample.js file and after
‘use strict’;
or before the closing of jquery function.
add
$('body').removeClass('no-js');
Usage:
When Javascript is enabled (99%) in the Browser.
.js .class-name { }
When Javascript is disabled or not supported (1%) in the Browser.
.no -js .class-name { }
more: