0

I am working in wordpress, I am using jquery to add remove class in body tag.

I want to apply class "home" to homepage and for other pages I want to apply "innerpage" class to body tag.

following is my code.

$(document).ready(function(){
var loc = window.location.href; // returns the full URL
//alert(loc);   
if (loc == "http://n.net/") 
    {
        $('body').removeClass('innerpage').addClass('home');
        $('.widget-area').removeAttr('style');

    }else {
        $('body').removeClass('home').addClass('innerpage');
        }


});

but it is not working if the url comes like: http://n.net/?=s

2
  • Try loc.indexOf('"http://n.net/"') == 0 Commented Sep 19, 2013 at 8:13
  • I think still u have not get my query. e.g stage 1: suppose if I have my page URL is n.net then the home page css gets apply to body. stage 2: Suppose if my URL is n.net/innepage, then innerpage css gets apply stage 3: but if my URL is n.net/?s= at this time home page css shld get apply which is not happening Commented Sep 19, 2013 at 8:16

3 Answers 3

2

If you only want to match the path, use pathname instead:

var path = window.location.pathname;
if(path === "/") {
  // Homepage
  $('body').removeClass('innerpage').addClass('home');
  $('.widget-area').removeAttr('style');
}
else {
  // Other pages
  $('body').removeClass('home').addClass('innerpage');
}

This will match for:

http://n.net/

http://n.net

http://n.net/?a=1

http://n.net/?a=1#hash

Have a look at all the properties of the location object, it contains a lot more than just href

Sign up to request clarification or add additional context in comments.

4 Comments

I do it myself. Sometimes questions seem so clear and then you realise it actually means something completely different!
@Archer : really sorry, but that the issue I am facing. I have mention 3 stages. I have issue where 3rd stage comes in. I dont want to make it hardcoded. URL name can be anything.
@RGraham: I tried it, it works only for innerpage not for Home page, because for homepage there is not "/" at the end of URL
@RGraham: Thanks alot, it was my mistake. I paste ur code again. Its working as per expected. Thanks again.
0

If you just want the URL, without the querystring, use this...

window.location.href.split("?")[0];

Comments

0

Try this,

$(document).ready(function(){
var loc = window.location.href; // returns the full URL
//alert(loc);   
if (loc.split('.net')[1] == '/' || loc.split('.net')[1] == '') 
{
    $('body').removeClass('innerpage').addClass('home');
    $('.widget-area').removeAttr('style');

}else {
    $('body').removeClass('home').addClass('innerpage');
    }


});

2 Comments

Its working only for innerpage, not for home page and any other page
sorry i edited it.. there is change in the split function... try now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.