• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to user navigation

Designody

WordPress and Genesis Tutorials

  • Get Started
  • About
  • Archive
  • Services
  • Membership
  • My Account

Add js and no-js class to body in Genesis

Last Updated on June 28, 2019Leave a Comment Favorited: 0 times

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:

Browser statistics on JavaScript disabled on StackOverflow.

Related Posts

  • Typing effect using Typed.js in Genesis
  • Infinite Scroll in Genesis
  • Masonry in Genesis
  • Adds an Additional JS control to the WordPress Customizer
  • Equal Heights Columns in Genesis

Categories: Free Content, Genesis Tutorials Tags: js, no-js

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You should use these Shortcodes tag and attributes while sharing the code:
Example: [html]I <3 HTML[/html] → [html][css][php][js][java][json][raw]

Primary Sidebar

WPEngine WordPress Hosting, Perfected.

Hosting You are looking for?.
Perfect solution for small business to global enterprise.

Learn more

StudioPress Genesis Theme Framework

The No.1 Theme Provider.
Creative, SEO rich Theme for all niche projects.

Learn more

Categories

  • Free Content
  • Genesis Tutorials
  • Premium Content
  • Snippets
  • What's New?
  • WordPress Tutorials

Tag Cloud

Archive Background Section blog center logo columns conditional tags CSS CSS Grid custom Customizer custom post type Custom Post Types custom template Custom Widget effect Featured Image front-page Genesis Genesis Sample header right hero section Image Background js layout Logo menu navigation Navigation Menu Nav Menu newsletter post page related posts responsive menu search search widget Shrinking Logo site header slide in-out Split Navigation Stylesheet Template Utility Bar Video Background widgets WordPress

Built with Genesis Framework + WordPress

  • Contact
  • FAQ
  • Disclaimer
  • Privacy Policy
  • Copyright Policy
  • Terms of Service