Get the href link on href itself

Asked

Viewed 554 times

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>

$current page Give me something like this back parameter=algumacoisa

What I would like to know is if there is a way for the href link in 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 catch the L4 who is in href="? L4"

Is there a way to do that?

  • It’s not giving to understand very well, so I figured you want to take the value of href and compare in if dynamically is this? if it is possible only with javascript

  • exactly: I need to print the value of href="" dynamically in $page Current==LINK_DO_HREF. In Javascript, can tell me how to do?

1 answer

2


Before the answer, a hint, use that if within the element itself a is ugly. In a few cases, when it is necessary to use the method ternary 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: https://sounoob.com.br/if-else-e-ternario-no-php/

As for your question, I don’t understand why you want to do this. It wouldn’t 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 didn’t test the code, but it should work, so of course, you don’t need to have that lot of if´s there in their links.

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

  • Got better. But too bad you can’t do direct with php or css.

  • Not only with them. They both depend on the javascript to read the elements of GIFT. css can see changes in GIFT, but do not read on your own the elements of the page. I recommend a study on Sass, css, or search by pre-compiler css. Here’s an example https://tableless.com.br/usando-conditionis-sass-control-directives

  • Yes, I’ll give a study. Thank you!

  • I did so and it worked: ** <ul class="menu"> <li><a href="? L1">Link 1</a></li> <li><a href="? L2">Link 2</a></li> <li><a href="? L3">Link 3</a></li> <li><a href="? L4">Link 4</a></li> </ul> $(Document). find( "ul.menu a[href='<?php echo "?".$pageCurrent ?>']" ).addClass( 'linkVisited' );

  • It works, but if you go to see this code in a year, or if another programmer comes, they will find it very confusing. But if it worked blz.

  • In verdfade, after ds ul loading the list items I just put $(Document). find( "ul.menu a[href='<? php echo "?". $paginaCorrent ? >']" ).addClass( 'linkVisitado' );

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.