javascript - Add Class To Body On Homepage - Stack Overflow

I'm trying to add a class to the body tag just on the homepage and that it. However, it's not

I'm trying to add a class to the body tag just on the homepage and that it. However, it's not working. What am I doing wring?

<script type="text/javascript">
    $(document).ready(function() {
          if(window.location.href===window.location.hostname) {
               $("body").addClass("home");
           }
    });
</script>

I'm trying to add a class to the body tag just on the homepage and that it. However, it's not working. What am I doing wring?

<script type="text/javascript">
    $(document).ready(function() {
          if(window.location.href===window.location.hostname) {
               $("body").addClass("home");
           }
    });
</script>
Share Improve this question asked Oct 22, 2015 at 14:01 XarcellXarcell 2,0116 gold badges35 silver badges66 bronze badges 7
  • 1 Do you need jQuery to do that? – anmarti Commented Oct 22, 2015 at 14:02
  • No. I actually thought plain old javascript would work better. – Xarcell Commented Oct 22, 2015 at 14:03
  • Why not set class in the HTML markup? <body class="home"> – anmarti Commented Oct 22, 2015 at 14:04
  • 1 I'm wondering how you debug it, seems easy to see what's going wrong – A. Wolff Commented Oct 22, 2015 at 14:05
  • @anmarti as mentioned, just homepage only. – Xarcell Commented Oct 22, 2015 at 14:06
 |  Show 2 more ments

5 Answers 5

Reset to default 3

window.location.href will never be the same as window.location.hostname since the former will contain protocol part (e.g.: http://) where as the latter doesn't.

I don't think

if(window.location.href===window.location.hostname) {
}

will ever be true. hostname will host be something like stackoverflow. where href will include protocol, ports and other things that may be apart of full url. You want to check if

if(window.location.href.indexOf("home.html") != -1) {
}

Or something of that nature. But as pointed out in ment this seems much simpler to just add to that html file or do it on the server if you generate the HTML.

window.location.href contains protocol information and will never equal window.location.hostname

As per W3

  • window.location.href returns the href (URL) of the current page
  • window.location.hostname returns the domain name of the web host
  • window.location.pathname returns the path and filename of the current page

You should rather check the pathname for home page location:

var path = window.location.pathname;
if (path == '/' || path == '/home.html') {
  $("body").addClass("home");
}

It is possible without jquery very easy.

window.addEventListener("load", MyFunction);
function MyFunction(){
  if(window.location.origin == window.location.href) {
    var body = document.getElementsByTagName('body');
    body[0].className = "myclass";
  }
}

or

window.addEventListener("load", MyFunction);
function MyFunction(){
  if(window.location.origin == window.location.href) {
    document.getElementsByTagName('body')[0].className = "myclass";
  }
}

body[0] because document.getElementsByTagName return value is an array.

With the ments being made, it got me thinking. I tried this and it worked.

<script type="text/javascript">
    $(document).ready(function() {
        switch (window.location.pathname) {
          case '':
          case '/index.php':
              $('body').addClass('home')
        }
    });
</script>

I forgot to mention in the question that it is on a PHP powered website. I answered my own question to help others who one day might have the exact same question.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745419408a4626899.html

相关推荐

  • javascript - Add Class To Body On Homepage - Stack Overflow

    I'm trying to add a class to the body tag just on the homepage and that it. However, it's not

    6小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信