0

So, I want to have the same default bootstrap navbar on every page on my website. If I change the name of one page (eg: Page 1), I want to have it changed across all my pages. From what I understand, I can just use jQuery to load the header separately. However, this doesn't seem to pull the "active" class for the page which is currently active. How would I have the "active" class only on the page which is currently active.

FYI, I'm using the AngularJS framework.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Case</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="#">WebSiteName</a>
    </div>
    <div>
      <ul class="nav navbar-nav">
        <li class="active"><a href="">Home</a></li> <!-- Change this ".active" class-->
        <li><a href="/page1">Page 1</a></li>
        <li><a href="/page2">Page 2</a></li>
        <li><a href="/page3">Page 3</a></li>
      </ul>
    </div>
  </div>
</nav>
  
</body>
</html>

EDIT: Also, the links to the other pages would change. For example, if I were on page1, the link to home would be "../", link to page2 would be "../page2", link to itself would be "".

xjars
  • 31
  • 5
  • 2
    If you're using Angular use ng-class and conditionally set `active` based on the current route/state url – Carol Skelly Jun 29 '15 at 14:52
  • possible duplicate of [Set active tab style with AngularJS](http://stackoverflow.com/questions/12295983/set-active-tab-style-with-angularjs) – Jeff Lambert Aug 08 '15 at 17:21

1 Answers1

0

I don't know AngularJS, but I use this javascript for my web site:

$('.navbar').find('a').each(function()
{
    if (window.location.href.indexOf($(this).attr('href')) > -1)
    {
            $(this).parent().addClass('active');
    }
});
max890
  • 517
  • 2
  • 9