Highlighting The Current Page With CSS & jQuery
- Share /
- CSS, PHP, User Interface, jQuery / 9.10.08 / 64 Comments / /
Here’s a common web development scenario: How do we highlight the current page in a navigation seamlessly and dynamically? The answer is jQuery.
See a Live Demo
My recipe for this common scenario involves a mixture of CSS and JavaScript with a dash of jQuery.
XHTML
<ul id="navigation"> <li><a href="about-us.html">about us</a></li> <li><a href="our-products.html">our products</a></li> <li><a href="blog.html">blog</a></li> <li><a class="active" href="login.html">login</a></li> <li><a class="active" href="contact.html">contact</a></li> </ul>
JavaScript / jQuery
<script type="text/javascript"> $(function(){ $page = jQuery.url.attr("file"); if(!$page) { $page = 'index.html'; } $('ul.navigation li a').each(function(){ var $href = $(this).attr('href'); if ( ($href == $page) || ($href == '') ) { $(this).addClass('on'); } else { $(this).removeClass('on'); } }); }); </script>
Usage
You’ll need a copy of jQuery, along with a copy of the jQuery URL Parser plugin.
After you include jQuery, and the URL Parser add the JavaScript snippet above to the <head> section of your document.
How it works
What we do here is simple, we grab the current name of the page, for example about.html. Then we check out ul.navigation <ul> to see if any of the anchors contain an href attribute with that value. If any of the links are a match we flag the parent of the anchor with a CSS class. In this case I’ve chosen current to keep things self explanatory.
Notes
In this case I chose to add the “current” class to the <li> rather than to the anchor. Usually I will create a navigation using CSS sprite techniques, and this allows me greater control over the presentation of that element. This can come in handy for rounded corners or tabs.















Christopher Seamon / 9.12.08 / 10:40 AM
Nice post Kevin. I have been leaning towards wanting to learn more about PHP (I am trying to learn more about back-end coding) and what I could do with it and stumbled upon your site.
I am a big time advocate of using Jquery and really like that you have a lot of information on it within your blog. Thanks for the good resources.
kevin / 9.12.08 / 10:50 AM
No problem. The first time I began working with PHP was inside Wordpress, and curiously led me to push my limits.
If your interested in going further with PHP in your work I’d recommend checking out PHP Solutions: Dynamic Web Design Made Easy
. I’m a sucker for friends of ED books…
revive / 7.6.09 / 9:06 AM
So, what about when someone first loads this page.. and the href is "/" and not including one of the linked page names.. none of the menu items will be hightlighted.. do you have a way to select a specific menu item.. say the first one, but using an id on the li, and adding a class to it if the rest of the 'equation' equals false??
kevinlearynet / 7.7.09 / 3:16 AM
Good point revive,
I've actually been using a similar revised method to this lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:16 AM
Good point revive,
I've actually been using a similar revised method to this lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:16 AM
Good point revive,
I've actually been using a similar revised method to this lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:16 AM
Good point revive,
I've actually been using a similar revised method to this lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:16 AM
Good point revive,
I've actually been using a similar revised method to this lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:16 AM
Good point revive,
I've actually been using a similar revised method to this lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:16 AM
Good point revive,
I've actually been using a similar revised method to this lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:17 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:17 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:17 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:17 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:17 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:17 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:17 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:17 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:17 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:17 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:17 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:17 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:17 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:17 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:17 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:17 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:17 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:17 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:18 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jQuery URL Parser plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:18 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jQuery URL Parser plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:18 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jQuery URL Parser plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:18 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jQuery URL Parser plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:18 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jQuery URL Parser plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:18 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jQuery URL Parser plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:18 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jQuery URL Parser plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:18 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jQuery URL Parser plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:18 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jQuery URL Parser plugin replaces the need for the PHP URL functions. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:18 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jQuery URL Parser plugin replaces the need for the PHP URL functions. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:18 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jQuery URL Parser plugin replaces the need for the PHP URL functions. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:18 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jQuery URL Parser plugin replaces the need for the PHP URL functions. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:18 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jQuery URL Parser plugin replaces the need for the PHP URL functions. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:18 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jQuery URL Parser plugin replaces the need for the PHP URL functions. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:18 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jQuery URL Parser plugin replaces the need for the PHP URL functions. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
kevinlearynet / 7.7.09 / 3:18 AM
Good point revive,
I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:
http://www.kevinleary.net/wp-samples/jquery-curre...
The jQuery URL Parser plugin replaces the need for the PHP URL functions. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.
Thanks!
revive / 7.7.09 / 12:08 PM
Hey Kevin,
thanks for the reply.. and the additional option for dealing with these active menu links.. great work ! I ended up having problems when going to the page originally, since there is no link that matches the root URL, nothing would 'highlight', so to speak..so, I went with a purely CSS solution, and assigned ID's to each pages body tag.. then assigned ID's to each
thanks again for the info on this.. I am sure another project will arise where the CSS solution won't be suitable and this just might be what's needed..
Cheers
revive / 7.7.09 / 12:09 PM
PS.. the info where I got the idea for the CSS only solution is here (they also show a JS solution that I preferred yours over):
http://www.richnetapps.com/automatically_highligh...
kevinlearynet / 7.7.09 / 1:48 PM
CSS is definitely a great solution for smaller situations, I've found that it works well for 5-6 navigation links. If you every find yourself using multiple navigation areas than the above JS solution may be of use.
Here's a little something you may find useful that I used on enernoc.com. This code will dynamically create body ID's for you based on the directory and page.
<code>
<?php
// Body ID
function current_dir() {
$path = dirname($_SERVER['PHP_SELF']);
$position = strrpos($path,'/') + 1;
return substr($path,$position);
}
function current_page() {
if(basename($_SERVER['PHP_SELF'], '.php') !== 'index') {
return basename($_SERVER['PHP_SELF'], '.php');
} else {
return 'index';
}
}
$bodyID = current_dir().'-'.current_page();
?>
<body id="<?= $bodyID; ?>">
</code>
Thanks for the feedback!
kevinlearynet / 7.7.09 / 1:48 PM
CSS is definitely a great solution for smaller situations, I've found that it works well for 5-6 navigation links. If you every find yourself using multiple navigation areas than the above JS solution may be of use.
Here's a little something you may find useful that I used on enernoc.com. This code will dynamically create body ID's for you based on the directory and page.
<?php
// Body ID
function current_dir() {
$path = dirname($_SERVER['PHP_SELF']);
$position = strrpos($path,'/') + 1;
return substr($path,$position);
}
function current_page() {
if(basename($_SERVER['PHP_SELF'], '.php') !== 'index') {
return basename($_SERVER['PHP_SELF'], '.php');
} else {
return 'index';
}
}
$bodyID = current_dir().'-'.current_page();
?>
<body id="<?= $bodyID; ?>">
Thanks for the feedback!
kevinlearynet / 7.7.09 / 1:49 PM
CSS is definitely a great solution for smaller situations, I've found that it works well for 5-6 navigation links. If you every find yourself using multiple navigation areas than the above JS solution may be of use.
Here's a little something you may find useful that I used on enernoc.com. This code will dynamically create body ID's for you based on the directory and page.
<?php
// Body ID
function current_dir() {
$path = dirname($_SERVER['PHP_SELF']);
$position = strrpos($path,'/') + 1;
return substr($path,$position);
}
function current_page() {
if(basename($_SERVER['PHP_SELF'], '.php') !== 'index') {
return basename($_SERVER['PHP_SELF'], '.php');
} else {
return 'index';
}
}
$bodyID = current_dir().'-'.current_page();
?>
<body id="<?= $bodyID; ?>">
Thanks for the feedback!
kevinlearynet / 7.7.09 / 1:50 PM
CSS is definitely a great solution for smaller situations, I've found that it works well for 5-6 navigation links. If you every find yourself using multiple navigation areas than the above JS solution may be of use.
Here's a little something you may find useful that I used on enernoc.com. This code will dynamically create body ID's for you based on the directory and page.
<?php
// Body ID
function current_dir() {
$path = dirname($_SERVER['PHP_SELF']);
$position = strrpos($path,'/') + 1;
return substr($path,$position);
}
function current_page() {
if(basename($_SERVER['PHP_SELF'], '.php') !== 'index') {
return basename($_SERVER['PHP_SELF'], '.php');
} else {
return 'index';
}
}
$bodyID = current_dir().'-'.current_page();
?>
<body id="<?= $bodyID; ?>">
Thanks for the feedback!
revive / 7.7.09 / 2:09 PM
Hey Kevin..
That's great! Thanks again! Question for you on this code, and the JS code for that matter.. the main issue I had with the JS code was it not highlighting ANY menu items when the root URL was visited, since the url didn't match any of the links <a href's … how does this body ID tag maker 'see' the base URL ??
will it return a null value for that homepages body tag??
kevinlearynet / 7.16.09 / 4:48 PM
Looks like that URL is broken:
http://www.kevinleary.net/wp-samples/jquery-curre...
kevinlearynet / 7.16.09 / 4:48 PM
Looks like that URL is broken:
http://www.kevinleary.net/wp-samples/jquery-curre...
kevinlearynet / 7.16.09 / 4:48 PM
Looks like that URL is broken:
http://www.kevinleary.net/wp-samples/jquery-curre...
kevinlearynet / 7.16.09 / 4:48 PM
Looks like that URL is broken:
http://www.kevinleary.net/wp-samples/jquery-curre...
kevinlearynet / 7.16.09 / 4:48 PM
Looks like that URL is broken:
http://www.kevinleary.net/wp-samples/jquery-curre...
kevinlearynet / 7.16.09 / 4:48 PM
Looks like that URL is broken:
http://www.kevinleary.net/wp-samples/jquery-curre...
kevinlearynet / 7.16.09 / 4:48 PM
Looks like this URL is broken. Try this:
http://www.kevinleary.net/wp-samples/jquery-curre...
kevinlearynet / 7.16.09 / 4:48 PM
Looks like this URL is broken. Try this:
http://www.kevinleary.net/wp-samples/jquery-curre...
kevinlearynet / 7.16.09 / 4:48 PM
Looks like this URL is broken. Try this:
http://www.kevinleary.net/wp-samples/jquery-curre...
kevinlearynet / 7.16.09 / 4:48 PM
Looks like this URL is broken. Try this:
http://www.kevinleary.net/wp-samples/jquery-curre...
kevinlearynet / 7.16.09 / 4:48 PM
Looks like this URL is broken. Try this:
http://www.kevinleary.net/wp-samples/jquery-curre...
kevinlearynet / 7.16.09 / 4:48 PM
Looks like this URL is broken. Try this:
http://www.kevinleary.net/wp-samples/jquery-curre...
kevinlearynet / 7.20.09 / 8:35 PM
I've modified this post, the solution no longer uses PHP, just JavaScript and CSS.