Home › Forums › JavaScript › Dynamic 'active' menu
- This topic is empty.
-
AuthorPosts
-
April 24, 2017 at 9:29 pm #254153lprintzParticipant
Hi guys!
I’ve researched and tried SO many things to no avail…I simply want to dynamically set an ‘active’ class based on page url
The latest I tried seemed very clear and concise but no go – http://www.siphon-marketing.com/nav-test/nav-test.html
Can somebody PLEASE help?
Len
April 24, 2017 at 10:44 pm #254155BeverleyhParticipantFunny you should ask. Just wrote this up on Friday http://blog.fofwebdesign.co.uk/40-highlight-the-menu-button-for-the-current-web-page
April 25, 2017 at 3:40 am #254166AtelierbramParticipant@beverleyh bookmarked! ;)
@lprintz there is a javaScript error, visible if you open console in DevTools: jQuery is not included.But besides javaScript solutions, there are more ways to skin this cat when using server-side rendering, in PHP for example (PHP frameworks like WordPress will set a
.current
class on the right list-item when using the default menu-builder).April 25, 2017 at 5:57 am #254172BeverleyhParticipant@Atelierbram thanks! :D
Yep, there are ways to do it in PHP too. For anyone not using a framework that adds a class, here’s a simple way to do it in PHP…
At the very top of the web page – above all HTML, even doctype;
<?php $pg = basename(substr($_SERVER['PHP_SELF'],0,strrpos($_SERVER['PHP_SELF'],'.'))); // get file name from url and strip extension ?>
This will take a value of “about-us” from an URL that looks like “www.mywebsite.com/about-us.php”.
And to use it in a menu’s
li
elements to add a class…;<li class="<?php if($pg=='about-us'){?>current<?php }?>"> <a href="/about-us.php">About Us</a> </li> <li class="<?php if($pg=='services'){?>current<?php }?>"> <a href="/services.php">Services</a> </li>
The output on the “about-us.php” page when it is viewed in a browser will look like this;
<li class="current"> <a href="/about-us.php">About Us</a> </li> <li class=""> <a href="/services.php">Services</a> </li>
April 25, 2017 at 6:03 am #254173BeverleyhParticipantApril 25, 2017 at 6:07 am #254174GMK HussainParticipantuse jQuery
jQuery(document).ready(function() { var url = window.location; jQuery('ul.nav a[href="' + url + '"]').parent().addClass('active'); jQuery('ul.nav a').filter(function() { return this.href == url; }).parent().addClass('nav-path-selected'); });April 25, 2017 at 7:23 am #254180AtelierbramParticipant@beverleyh You could rescue it from this gist (copy & paste from email)
@gmkhussain something like that was attempted in the demo by @lprintz but the jQuery library was not includedApril 25, 2017 at 7:36 am #254182BeverleyhParticipantGulped again! Something about it really isn’t liking me… I’d better wait for a mod to step in :/
April 25, 2017 at 7:42 am #254183AtelierbramParticipant@beverleyh happens to me sometimes … don’t know why. Posting to much links (anti spam measure?) ,editing the reply too often and/or two fast in succesion?
April 25, 2017 at 8:55 am #254181BeverleyhParticipantSweet! I’ll try again…
… there are ways to do it (set an ‘active’ class based on page url) in PHP too. For anyone not using a framework that adds a class, here’s a simple way to do it in PHP…
At the very top of the web page – above all HTML, even doctype;
<?php $pg = basename(substr($_SERVER['PHP_SELF'],0,strrpos($_SERVER['PHP_SELF'],'.'))); // get file name from url and strip extension ?>
This will take a value of “about-us” from an URL that looks like http://www.mywebsite.com/about-us.php.
And to use it in a menu’s li elements to add a class…;
<li class="<?php if($pg=='about-us'){?>active<?php }?>;">; <a href="/about-us.php">About Us</a> </li> <li class="<?php if($pg=='other-stuff'){?>active<?php }?>;">; <a href="/other-stuff.php">Other Stuff</a> </li>
The output on the “about-us.php” page when it is viewed in a browser will look like this;
<li class="active"> <a href="/about-us.php">About Us</a> </li> <li class=""> <a href="/other-stuff.php">Other Stuff</a> </li>
April 25, 2017 at 8:56 am #254189Paulie_DMember@Beverleyh Et Voila
April 25, 2017 at 10:19 am #254191BeverleyhParticipantThanking you, kind Sir :)
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.