12

I have what I think is a simple question but I can't get it to work for the life of me.

All I want to do is add some javascript to the page that adds a class to the main page container based on the URL.

Let's say I have a site at root.com and the following html structure (loosely speaking):

<html>
  <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  </head>
  <body>
    <div id="main" class="wrapper">
      Blah, blah, blah
  </body>
</html>

What I want to do is a script that, if the page = (for example) root.com/technology, it adds a class to the main div. So the div would now look like:

     <div id="main" class="wrapper tech">

I've loaded jquery, so I'd like to do it that way.

5 Answers 5

28

You can use window.location to get the current URL, and then switch based on that:

$(function() {
  var loc = window.location.href; // returns the full URL
  if(/technology/.test(loc)) {
    $('#main').addClass('tech');
  }
});

This uses a regular expression to see if the URL contains a particular phrase (notably: technology), and if so, adds a class to the #main element.

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

3 Comments

@alex vidal It works fine for me but it's loading a bit later than normal. I'm using this action to maintain a sub menu open once the user is in a determined Therefore the user can see after 1-2secs how the sub category menu opens. I would like it to do this instantly, how can I avoid this?
How can this be modified to work if you at the route (homepage) of the site?
Thanks! can you tell me what does .test do?
8

Well, here is how I would do it:

switch (window.location.pathname) {
    case '/technology':
        $('#main').addClass('tech');
        break;
    case '/something':
        // code block
        break;
    case '/somestuff':
        $('#main').addClass('some');
        break;
    default: 
        // code block
}

This way, you keep your code clean and you can easily add another case.

See here what window.location.pathname means.

3 Comments

I like this because, as you say, you can easily add another case without mucking things up. But I can't get it to work...
I like this answer better then the selected 'best' answer. Switch statements are way more clean then writing out conditionals.
@MattMartin that's probably because you need to add break; at the end of each case. That was the case for me.. lol
3

would something like below help? based on other answer, just by doing

console.log(window.location)

, you can wealth wealth of information related to your location object

     if(window.location.href=== "root.com/technology") {
         $("#main").addClass("tech");
     }

Comments

2

Use location built-in object to determine full URL or some parts of it.

1 Comment

Clearly this is correct, but it doesn't completely answer my question of how to do it.
0
      $(function(){
        var cUrl = window.location.href;
         $(".product-type-swatches__list a").each(function(){
                 if ($(this).attr("href") == cUrl){
                         $(this).addClass("is-selected");
                 }
         });
    });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.