How do you use JavaScript to detect the homepage

I have tried is_front_page() but I believe that is the php way of doing it. Perhaps I’m not calling it right because, of course, I’m not getting a response. Maybe I’m just short of my detailed Js ways of accessing the WordPress classes.

What I’m trying to do is simple. If I am on the front page or home page add this class if I’m not add this other class. Very simple it’s just not working. I have even tried a pseudo span tag and add the class to that and it doesn’t work.

3 Answers
3

I just posted an answer to another question about how to do it.

In your case, assuming you used body_class() in your theme, your home page should have a <body> with class home to it.

So in your JS, you can:

if( $('body.home').length ){
  // Do stuff
}

Leave a Comment