javascriptvolusion

How can I dynamically add a class to 'body' using JavaScript only for the home page?


I added this to the head, but it's not working:

<script>
var xpathname = (window.location.pathname);
if (xpathname ==('/')) {
$('body').addClass('home');
}
</script>

The site is here: http://xotdr.unxpr.servertrust.com/

Volusion doesn't allow developers to code freely so there are a lot of workarounds that I need to implement, unfortunately.

Edit: I want the class to show only on the home page body.


Solution

  • Since you added this to the head you need to execute this snippet when body tag is available:

    $(function() {
        var xpathname = window.location.pathname;
        if (xpathname == '/') {
            $('body').addClass('home');
        }
    });