Also, if you’re using a master page, you’re only going to have one body tag for all your pages.
Not necessarily, this depends on how smart you programmed it to be. The ideal solution is to use some sort of variable such as $_GET[‘page’] which corresponds to the page, so if you linked to say the about page, linked it as index.php?page=about
And use the $_GET[‘page’] to echo the class name in the body, such as <body class=“section-<?php echo $_GET[‘page’]; ?>”> ( though you might wanna check if its defined ).
Though I would never do this personally, if you really want to rely on JS alone it’s not preferred to have inline event handlers and the like as it isn’t as flexible as unobtrusive code, I would do it as such:
<!doctype html>
<html>
<head>
<title>one active state</title>
<script type="text/javascript" src="foo.js"></script>
<style type="text/css">
* { margin:0; padding:0; }
body {
font:76% verdana;
padding:10em 0 0;
}
ul#nav {
background:#333;
list-style:none;
margin:0 auto;
padding:1em;
width:10em;
}
ul#nav li {
display:block;
width:100px;
margin:0 0 10px;
}
ul#nav li a:visited, ul#nav li a:link {
color:#fff;
}
ul#nav li.current a {
color:red;
}
</style>
</head>
<body>
<ul id="nav">
<li class="current"><a href="#">Home</a></li>
<li><a href="#">Contact Us</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Tools</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Hiring</a></li>
</ul>
<script>
(function() {
var addEvent = (function() {
return window.addEventListener ?
function( el, ev, fn ) {
el.addEventListener( ev, fn, false );
}
:
function ( el, ev, fn ) {
el.attachEvent( 'on' + ev, function() {
fn.call( el );
});
}
})(),
hasClass = function( el, className ) {
return (' ' + el.className + ' ').indexOf( ' ' + className + ' ') != -1;
},
getElementsByClass = function(c, node, tag) {
node = node || document;
var els = tag || '*';
var elements = node.getElementsByTagName(els);
var matchedArray = [];
for (var i=0, l=elements.length; i<l; i++) {
if ( hasClass ( elements[i], c ) ) {
matchedArray.push(elements[i]);
}
}
return matchedArray;
}
addClass = function( c, e ) {
e.className += c;
};
removeClass = function( c, e ) {
e.className = e.className.replace( c, '' );
};
if ( typeof Array.prototype.forEach == 'undefined' ) {
Array.prototype.forEach = function( fn ) {
var len = this.length, thisp = arguments[1];
for ( var i = 0; i<len; ++i ) {
fn.call( thisp, this[i], i, this );
}
}
}
addEvent( window, 'load', init );
function init() {
var nav = document.getElementById( 'nav' ), // update nav ID here
activeClass = 'current', // update current class here
current = getElementsByClass( activeClass, nav, 'li' )[0],
lis = nav.getElementsByTagName('li');
// since we're dealing with a nodeList
Array.prototype.forEach.call( lis, function( el ) {
addEvent( el, 'click', function( e ) {
if ( !hasClass( this, activeClass ) ) {
removeClass( activeClass, current );
addClass( activeClass, el );
current = el;
if ( e.preventDefault ) {
e.preventDefault();
} else {
e.returnValue = false;
}
}
});
});
}
})();
</script>
To get the JS block I typed to function correctly, the easiest way is to place the <script> tags before the </body> end tag. An alternative would be invoking this on window’s load event, or on dom ready.