0

I've recently made a site where I have a navigation bar, which is included to the pages using PHP.

<?php include "Assets/Inclusions/NavigationBar.php" ?>

I wanted the active page list item to have bold text, so I used JavaScript to fetch the current path and make the relative element bold with a lot of if statements.

<script>
if(location.pathname == 'location.php')
document.getElementById("location").style.fontWeight="800";
    } ...and so on
</script>

Is there a better way I should have done this?

2
  • I recommend using some PHP logic in your NavigationBar.php file to add a CSS class to the active element. Commented Jul 1, 2014 at 5:15
  • That's what I was looking for, but how in the world could I change the CSS in NavigationBar.php without including it in the clients' document? Commented Jul 1, 2014 at 5:17

2 Answers 2

1

This seems like something that should be done on the server-side of things. Please feel free to correct this if I have made a syntax error, it's been a while since I've written PHP, but here's the gist of it:

NavigationBar.php

<?php
    function isActive($page){
      $currentPage = $_GET['page']; // mypage.com?page='something'

      if ( !isset($currentPage) ) $currentPage = 'home';

      return $currentPage == $path;
    }
?>

<ul class="navigation">
  <li><a href="?page=home" 
          <?php if ( isActive('home') ) echo('class="active"'); ?> >Home</a></li>
  <li><a href="?page=About" 
          <?php if ( isActive('about') ) echo('class="active"'); ?>>About Us<a/></li>
</ul>

style.css

a.active{
    font-weight: 800;
}
Sign up to request clarification or add additional context in comments.

3 Comments

That's perfect! It's just what I was looking for. Honestly, I'm not innovative all the time like you all!
You should also avoid to access superglobals unguarded. Better: $currentPage = isset($_GET['page']) ? $_GET['page'] : '';
Agreed, a PHP guru should totally better-up this code
0

You could use a for loop, grab all the link elements in your nav, then compare the path to the href.

var path = window.location.pathname;
var links = document.getElementsByClassName("navlink");
for(var i=0; i<links.length; i++){
   if(links[i].getAttribute("href")==path){
      links[i].style.fontWeight = "800";
      break;
   }
}

Obviously you would have to do the correct comparison for your situation but this would be easier than writing a bunch of ifs and easier to maintain as you dont have to hardcode the locations to test against.

And if you are not worried about compatibility with older browsers, or using jQuery you could use the attribute selector with document.querySelector or jQuery

//With querySelector
var link = document.querySelector("a[href='"+path+"']");
if(link!==null){
   link.style.fontWeight=="800";
}

//with jQuery
$("a[href='"+path+"']").css("font-weight","800");

1 Comment

That's brilliant! I'll try it when I'm with my computer again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.