tags:

views:

42

answers:

3

Is there a javascript code that can detect what page my user is on and then add a class to a ul item? I want to put my menu in my static HTML website in a separate file and then use PHP include to add it, but I want class="current" for the current page.

A: 
if(window.location == 'myfixedurl')
{
    document.getElementById('menuList').className += 'current';
}

You probably want to tweak window.location so that it looks at relative paths, or absolute without hostname, etc.

Keep in mind if the user disables JavaScript they won't have the visual indicator of which page they're on.

If you have access to PHP why not do it in the server side code instead of client side?

Michael Shimmins
A: 

I would do it in PHP, but you can use something like window.location

galambalazs
+1  A: 

You're probably interested in window.location or window.location.pathname.

I would recommend to do this in PHP though if you're already using it anyway.

deceze