07-02-2007, 05:16 PM
[eluser]the real rlee[/eluser]
Here's a simple bit of JavaScript which sets the currently active link. Perhaps someone could build this into a helper or library
JS:
HTML:
Here's a simple bit of JavaScript which sets the currently active link. Perhaps someone could build this into a helper or library
JS:
Code:
/*
@author: Richard Lee aka the real rlee
A simple JavaScript to set "active" class to active menu item based on url
*/
initMenu = function(){
var url = "<?=site_url($this->uri->uri_string())?>";
var menu = document.getElementById('main-nav').getElementsByTagName("A");
for( var x = 0; x < menu.length ; x++ ){
if (menu[x].href == url) {
menu[x].className = 'active';
}
}
}
if (window.addEventListener) {
window.addEventListener('load', initMenu, false);
} else if (window.attachEvent) {
window.attachEvent("onload", initMenu);
}
</script>
Code:
<ul id="main-menu">
<li>
<?=anchor('home', 'home')?>
</li>
<li>
<?=anchor('about', 'about')?>
</li>
<li>
<?=anchor('locations', 'locations')?>
</li>
</ul>