PDA

View Full Version : [General Development] Highlighting active page in a navbar?


Dr.CustUmz
02-19-2021, 09:50 AM
When it comes to highlighting the active page in a navbar I find myself using if conditions, just what I've grown accustomed to.

Example: (may not be perfect, just going from the top of my head)
<a href="" class="class<if condition="(THIS_SCRIPT=='index')"> active</if>">Home</a>
<a href="" class="class<if condition="($_REQUEST['do']=='page')"> active</if>">Page</a>
<a href="" class="class<if condition="in_array(THIS_SCRIPT, array(showthread,forumdisplay))"> active</if>">Threads</a>
<a href="" class="class<if condition="($_REQUEST['do']!='AD')"> active</if>">Highlight if not active</a>

I was wondering to avoid the multiple if's, is their an easier way (or better way) for achieving this concept?

I'm asking here rather than on stack because we are vBulletin users, and a lot of stack members tend to have issues with vb lol, and maybe vb has a whole different way of detecting the current page.

yilmaz
02-19-2021, 02:57 PM
Change

<if condition="(THIS_SCRIPT=='index')"> active</if>">Home</a>


To

<vb:if condition="(THIS_SCRIPT=='index')"> active</vb:if>">Home</a>

puertoblack2003
02-19-2021, 03:52 PM
Change

<if condition="(THIS_SCRIPT=='index')"> active</if>">Home</a>


To

<vb:if condition="(THIS_SCRIPT=='index')"> active</vb:if>">Home</a>


isn't that vb 4 vb statement ?

Dr.CustUmz
02-19-2021, 03:57 PM
Change

<if condition="(THIS_SCRIPT=='index')"> active</if>">Home</a>


To

<vb:if condition="(THIS_SCRIPT=='index')"> active</vb:if>">Home</a>


no no no lol, I'm sorry if I wasn't clear its not about vb3 or vb4. It's about coding in general. I'm wondering if there is a better approach to detecting which page I am on, rather than having to use an if condition for each link.

I got to thinking a jQuery solution, I may add classes based on links. It would look something like:
$(".navLink a[href*='" + location.pathname + "']").addClass("active");

Which I'm thinking will be way better than a bunch of if conditions. it would turn that top piece of code into:

<div class="navLink">
<a href="index.php" class="active">Home</a> //We are on index so jQuery added class="active"
<a href="page.php">Page</a>
<a href="showthread.php">Threads</a>
</div>

Which is much easier to work with rather than having a bunch of ifs
as for the link we want styled if its not the current page, add its own class to it, and tell jQuery to remove it if it is the current page.

yilmaz
02-19-2021, 06:06 PM
isn't that vb 4 vb statement ?

Sorry, I replied as the topic is in the vb4 section.

Dr.CustUmz
02-19-2021, 06:14 PM
It was more a coding in general type of question, maybe should have posted in private coders, but i feel this section gets the most viewers lol.

It's something that could be applied too ANY vb version though.

yilmaz
02-19-2021, 06:33 PM
<a href="" class="class<if condition="(THIS_SCRIPT=='index')"> active</if>">Home</a>
<a href="" class="class<if condition="($_REQUEST['do']=='page')"> active</if>">Page</a>
<a href="" class="class<if condition="in_array(THIS_SCRIPT, array(showthread,forumdisplay))"> active</if>">Threads</a>
<a href="" class="class<if condition="($_REQUEST['do']!='AD')"> active</if>">Highlight if not active</a>

This is the most logical.

Note: Maybe you know, you should always specify the name of that file first in the custom php page.
custom.php

<?php
define('THIS_SCRIPT', 'custom');

Dr.CustUmz
02-19-2021, 06:36 PM
so using if conditions is the way you would do? It just felt like too much, and I got to thinking of alternative ways lol