0

Hey guys i have written a simple function which reads the current url and sets some kind of highlight.

If I open up the url everything works fine but if I do more advanced request it's not working.

example: If i request "/customers" it works but if I request "/customers/10" it's not working anymore. Can someone provide me with some regex action ?

Thanks in advance

function setGUI() {
var url = window.location.pathname;
switch(url) {
    case "/orders":
        $(document).ready(function() {
            $(".nav li:nth-child(2)").addClass("active");
        });
        break;

    case "/customers": // match "customers/any_id"
        $(document).ready(function() {
            $(".nav li:nth-child(3)").addClass("active");
        });
        break;

    case "/partners": match "partners/anyid"
        $(document).ready(function() {
            $(".nav li:nth-child(4)").addClass("active");
        });
        break;

    case "/help":
        $(document).ready(function() {
            $(".nav li:nth-child(5)").addClass("active");
        });
        break;

    case "/users/": // match "/users/any_id/"
        $(document).ready(function() {
            $(".nav li:nth-child(6)").addClass("active");
        });
        break;

    default:
        alert("debug");
    }
}

setGUI();
2
  • switch cannot handle it.... you need to use if elseif construct Commented Dec 4, 2013 at 9:01
  • @ArunPJohny - There are (at least) two ways a switch can handle this requirement: (a) keep the existing switch but delete the end of the url before comparing, or (b) "cheat" with a switch(true) and put the conditions in each case. Commented Dec 4, 2013 at 9:15

2 Answers 2

1

Looks like your function doesn't need to use whatever information might be after the second slash, so I'd suggest simply deleting everything from the second slash onwards:

var url = window.location.pathname.replace(/^(\/[^/]+)\/.*/, "$1");

...and then leave your switch and case conditions as they are.

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

4 Comments

thanks this works for me... only "/users/id/edit" is not working. any ideas?
Your users case item is case "/users/" - try changing it to case "/users" (remove the trailing / so that it matches the format of the other urls).
I did still the same result
"/users/id/edit".replace(/^(\/[^/]+)\/.*/, "$1"); returns "/users" (I just tested it to be sure), so it should work with case "/users".
0

Switch will evaluate base on equality. One solution is to use if-else instead. I'd recommend using indexOf:

..
if(url.indexOf("/orders") > -1){
        $(document).ready(function() {
            $(".nav li:nth-child(2)").addClass("active");
        });
}else if(url.indexOf("/customers") > -1){
..

2 Comments

"You have to..." - Well no, you don't have to, there are at least two ways that you can use a switch to achieve this. (Arguably an if/else if structure is better, but it's not the only way.)
Thanks for that. I'll reword that sentence.

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.