I have a common side navigation bar where I have linked several pages to it. I have included it to all the pages using the below php include statement.
<?php include '../php/playersidenav.php';?>
The sidenavigation is as follows.
<div class="column sidenav">
<div class="row">
<img src="../images/avatar.png" alt="Avatar">
<h1 style="color:white;float:right;padding-top:7%">Hello John!</h1>
</div>
<a class="normal" href="news.php" onclick="makeActive(event)">NEWS</a>
<a class="normal" href="schedule.php" onclick="makeActive(event)">SCHEDULE</a>
<a class="normal" href="partners.php" onclick="makeActive(event)">PARTNERS</a>
<a class="normal" href="coaches.php" onclick="makeActive(event)">COACHES</a>
<a class="normal" href="tournaments.php" onclick="makeActive(event)">TOURNAMENTS</a>
<a class="normal" href="events.php" onclick="makeActive(event)">EVENTS</a>
<a class="normal" href="shops.php" onclick="makeActive(event)">SHOPS</a>
</div>
One way that you could do it would be to make use of the browser'slocalStorage
- If you modify the aboveplayersidenav.php
slightly by removing the inline event handler like this and then including some Javascript within this file so that it looks like this:
<div class="column sidenav">
<div class="row">
<img src="../images/avatar.png" alt="Avatar">
<h1 style="color:white;float:right;padding-top:7%">Hello John!</h1>
</div>
<a class="normal" href="news.php">NEWS</a>
<a class="normal" href="schedule.php">SCHEDULE</a>
<a class="normal" href="partners.php">PARTNERS</a>
<a class="normal" href="coaches.php">COACHES</a>
<a class="normal" href="tournaments.php">TOURNAMENTS</a>
<a class="normal" href="events.php">EVENTS</a>
<a class="normal" href="shops.php">SHOPS</a>
</div>
<script>
//The name of the store in localStorage
const store='LINK_ACTIVATED_MEMORY';
// the class to add to indicate active state
const cn='active';
// nodelist of all sidenav links
const links=document.querySelectorAll('.sidenav > a.normal');
// ensure any "active" links have class removed and then
// save new hyperlink to localStorage
const makeActive=(e)=>{
e.preventDefault();
links.forEach(a=>a.classList.remove(cn));
localStorage.setItem( store, e.target.href );
location.href=e.target.href;
};
// on page load run this function - check the store
// and iterate through nodelist to match saved value and
// node value.
const loadActive=()=>{
let stored=localStorage.getItem( store );
if( stored==null )return;
Array.from( links ).some(a=>{
if( stored==a.href ){
a.classList.add(cn)
return true;
}
});
};
//bind the event handler
links.forEach(a=>a.addEventListener('click',makeActive));
// load active link class
loadActive();
</script>
The link that is clicked will be stored inlocalStorage
and then when the new page is loaded theloadActive
function will be invoked to set theactive
class. As per the comment by @Kiko Software - the default action when clicking a hyperlink would mask any highlighting attempt anyway so the actual setting of the class should be done on page load/reload.
After thinking about the suggestion by @Kiko Software that this could be done with PHP and dispense with Javascript altogether I wrote that as an alternative solution.
The included fileplayersidenav.php
:
<?php
$cn='active';
$links=array(
'home.php' => 'TEST PAGE HOME',
'news.php' => 'NEWS',
'schedule.php' => 'SCHEDULE',
'partners.php' => 'PARTNERS',
'coaches.php' => 'COACHES',
'tournaments.php' => 'TOURNAMENTS',
'events.php' => 'EVENTS',
'shops.php' => 'SHOPS'
);
?>
<div class="column sidenav">
<div class="row">
<img src="../images/avatar.png" alt="Avatar" />
<h1 style="color:white; float:right; padding-top:7%">Hello John!</h1>
</div>
<?php
foreach( $links as $href => $text ){
printf(
'<a href="%s" class="normal %s">%s</a>',
$href,
( basename( $_SERVER['SCRIPT_FILENAME'] )==$href ? $cn : '' ),
$text
);
}
?>
</div>
And an example page calling said file, calledhome.php
:
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title></title>
<style>
.sidenav a.active {
background-color: #001F5A;
color: white;
border-radius: 5px;
}
a{margin:1rem;}
</style>
</head>
<body>
<?php
require sprintf('%s/playersidenav.php',__DIR__);
?>
<h1>Hello World!</h1>
</body>
</html>
The PHP include file compares the array key to the basename from the currentSCRIPT_FILENAME
server variable and adds the class accordingly and negates the need for Javascript completely. Just proves the old saying -"There's more than one way to skin a cat"
Solution #1
function makeActive(evt) {
var i, normal;
normal = document.getElementsByClassName("normal")
for (i = 0; i < normal.length; i++) {
normal[i].className = "normal"
}
evt.target.className = "normal active"
}
Solution #2
function makeActive(evt) {
var i, normal;
normal = document.getElementsByClassName("normal")
for (i = 0; i < normal.length; i++) {
normal[i].classList.remove('active')
}
evt.target.classList.add("active")
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.