Hello All,
I have noticed a very common problem in twitter-bootstrap navbar , when user click on any item or navigate on other page by clicking any item from navbar, item not remain selected/active.
Here is the solution for above problem
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<meta charset=”utf-8″>
<title>AXSyst</title>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<meta name=”description” content=”Advanced Xpert Systems”>
<meta name=”author” content=””>
<!– Le styles –>
<link href=”css/bootstrap.css” rel=”stylesheet”>
<style type=”text/css”>
body {
padding-top: 60px;
padding-bottom: 40px;
}
</style>
<link href=”css/bootstrap-responsive.css” rel=”stylesheet”>
<!– Le HTML5 shim, for IE6-8 support of HTML5 elements –>
<!–[if lt IE 9]>
<script src=”http://html5shim.googlecode.com/svn/trunk/html5.js“></script>
<![endif]–>
<script src=”js/jquery.js”></script> <!–THIS NEEDS TO BE PLACED HERE BECAUSE $(document).ready(function(){ WON’T WORK. ALL OTHER JS ARE LOCATED IN _footer.php –>
</head>
<body>
<!–NavBar –>
<div>
<div>
<div>
<a data-toggle=”collapse” data-target=”.nav-collapse”>
<span></span>
<span></span>
<span></span>
</a>
<a href=”index.php”>MySite</a>
<div>
<ul id=ul_nav>
<li id=”li_home”><a href=”index.php”>Home</a></li>
<li id=”li_about”><a href=”about.php”>About</a></li>
<li id=”li_cont”><a href=”contact.php”>Contact</a></li>
</ul>
</div>
</div>
</div>
</div>
<!–NavBar end –>
Add following line of code to make selected node active in navbar….
———————————————————————————–
<script type=”text/javascript”>
function GetCurrentPageName() {
//method to get Current page name from url.
var PageURL = document.location.href;
var PageName = PageURL.substring(PageURL.lastIndexOf(‘/’) + 1);
return PageName.toLowerCase() ;
}
$(document).ready(function(){
var CurrPage = GetCurrentPageName();
switch(CurrPage){
case ‘index.php’:
$(‘#li_home’).addClass(‘active’) ;
break;
case ‘about.php’:
$(‘#li_about’).addClass(‘active’) ;
break;
case ‘contact.php’:
$(‘#li_cont’).addClass(‘active’) ;
break;
}
});
</script>
———————————————————————————–
Hope it helps !