最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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: /

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.

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./

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.

Share Improve this question edited Mar 2, 2014 at 7:21 mplungjan 178k28 gold badges181 silver badges240 bronze badges asked Mar 2, 2014 at 7:14 J82J82 8,45723 gold badges59 silver badges89 bronze badges 1
  • Now you removed the x, the body has a class attribute of "home" so perhaps it is the CSS that is not doing anything visible – mplungjan Commented Mar 2, 2014 at 7:24
Add a ment  | 

5 Answers 5

Reset to default 3

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');
    }
});
<script>
    var bodyclass=document.createAttribute("class");
    bodyclass.value="home";
    document.getElementsByTagName("body")[0].setAttributeNode(bodyclass);
</script>

Give this a try

var b = document.getElementsByTagName('body')[0];
b.className += 'home';

I know it's a old post, but the question will remain useful.

var xpathname = (window.location.pathname);
var ndeBody = document.getElementsByTagName("body")[0];
if (xpathname ==('/')) {
    ndeBody.classList.toggle("home");
}
else{
    ndeBody.classList.toggle("home");
}

When I go to that URL you have syntax error:

 Uncaught ReferenceError: x$ is not defined

e.g. you want to delete the x in x$('body').addClass('home');

发布评论

评论列表(0)

  1. 暂无评论