Detect client browser support JavaScript in WordPress theme

Open “functions.php” and add below code.

function hs_body_classes( $classes ) {

    // Helps detect if JS is enabled or not.
    $classes[] = 'no-js';

    return $classes;
}
add_filter( 'body_class', 'hs_body_classes' );

function hs_supports_js() {
    echo '<script>document.body.classList.remove("no-js");</script>';
}
add_action( 'wp_footer', 'hs_supports_js' );

In the first function we add “no-js” class to body class. In the second function if browser support the JavaScript, the code will run and delete “no-js” class.

If body has “no-js” class, then browser doesn’t support JavaScript or it is disabled.

Shopping Cart