0

I have a navigation bar at the side of a page and want to highlight one of several entries depending on GET-parameters.

After a little Reading I came to this solution, but it does not seem to work:

in the .html:

<script type="text/javascript">
    $(document).ready(highlight_me());
</script>

the JS functions:

function highlight_me() {
    // ensure all links have class 'regular'
    document.links.className = 'regular';
    // determine which link to highlight
    var id = 'home';
        switch (querystring('view')) {
            case "set":
                id = 'settings';
                break;
            case "mc":
                id = 'messages';
                break;
            default:
                id = 'home';
        }
    // highlight link
    document.getElementById(id).className = 'highlight';
}

function querystring(key) {
    // extract GET-value for key
    var re = new RegExp('(?:\\?|&)' + key + '=(.*?)(?=&|$)', 'gi');
    var r = [], m;
    while ((m = re.exec(document.location.search)) != null) r[r.length] = m[1];
    return r;
}

the CSS classes:

a, a.regular, a:visited {
    color: #f0ce96;
}

a:active, a:hover, a.highlight {
    text-decoration: underline;
    color: #ffeebb;
}

I'd be thankful for a hint that points me to where I go wrong, here.

3 Answers 3

3

You are not passing the "id" parameter when you call the highlight_me function.

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

17 Comments

the id parameter is optional - if none or other than the specified ones are given, id defaults to 'home'
the JS seems to be right, I don't know much about regular expressions but did you debug the querystring function to check if it is doing what you want?
Ah... could have thought of that myself. This is, where the problem seems to be - i just copy-pasted that function from somewehere, because someone stated that it worked, so I actually have no real idea, what this piece of code finally does...
check this netlobo.com/url_query_string_javascript.html it seems to work for me.
With that, I seem to get no result at all from the query string function. I inserted a simple alert(results) and also tried with alert(results[1]) before the if-clause, but never got anything back other than null.
|
1

Any reason why you need id as a parameter? You're not using it anywhere inside the function. Shouldn't a call to querystring be enough? In that case, you don't need to do the if(id.length) part. Set id to 'home' by default, and then have the switch statement to modify the variable accordingly.

Here's what I'm talking about:

function highlight_me() {
// ensure all links have class 'regular'
document.links.className = 'regular';
// Set id to home by default
var id = 'home';
    switch (querystring('view')) {
        case "set":
            id = 'settings';
            break;
        case "mc":
            id = 'messages';
            break;
        default:
            id = 'home';
    }
// highlight link
document.getElementById(id).className = 'highlight';
}

1 Comment

Good point for simplyfying the function, but I planned on... yeah, what was I actually planning on in the first case? ;) But unfortunately, the real problem has been already tracked down to the querystring function not working properly, so this does not help to selve this :/
0

function $(highlight_me()); want a parameter id which is not supply when you call this function.And put your function inside document.ready.

 $(document).ready(function() {
   // put all your function here.
 });

1 Comment

As i stated in answer to Papaya's suggestion, the parameter id is optional an defaults to 'home' if not specified or other than defined values (at least my IDE also says so). But putting it this way: $(document).ready(highlight_me) at least made the 'home' part work

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.