Get the href link in the href itself

0

I have the code below

<?php $paginaCorrente = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY); ?>

<ul class="menu">
  <li><a href="?l1" <?php if ($paginaCorrente=="l1") echo class='linkVisitado'";?>>Link 1</a></li>
  <li><a href="?l2" <?php if ($paginaCorrente=="l2") echo "class='linkVisitado'";?>>Link 2</a></li>
  <li><a href="?l3" <?php if ($paginaCorrente=="l3") echo "class='linkVisitado'";?>>Link 3</a></li>
  <li><a href="?l4" <?php if ($paginaCorrente=="l4") echo "class='linkVisitado'";?>>Link 4</a></li>
</ul>

$ pageCurrent returns something like this parameter = something

What I would like to know is if there is a way to link the href in the href itself so that instead of doing

<a href="?l4" <?php if ($paginaCorrente=="l4") echo "class='linkVisitado'";?>>Link 4</a></li>

I do:

<a href="?l4" <?php if ($paginaCorrente==LINK_DO_HREF) echo "class='linkVisitado'";?>>Link 4</a></li>

In the above case I need to get the l4 that is in href="? l4"

Is there a way to do this?

    
asked by anonymous 27.09.2017 / 14:17

1 answer

2

Before the answer, one tip, using this if within the a element becomes ugly. In a few cases, when it is necessary to use the ternary method as in this example:

<a href="?l4" <?= ($paginaCorrente=="l4")?"class='linkVisitado'":"class=''"?>>Link 4</a></li>

is known more as if of a line , you can see how it works here: link

As for your question, I do not understand why you want to do this. It would not be easier to do something like this:

$(document).ready(function(){ //ao carregar a pagina var x = <?= $paginacorrente ?>; //pega valor do get //alert(x); //da um alert aqui pra conferir se está com o valor correto $(document).find("a[href='"+x+"']").addClass('linkVisitado'); //procura por elementos a com href igual o get e adiciona a classe });

I did not test the code, but it should work, of course, you do not have to have that lot of if´s there in your links .

<li><a href="?l1">Link 1</a></li>

    
27.09.2017 / 15:03