Concatenate string to all page href

0

I would like to know if there is any function in jQuery that can concatenate to the page's hrefs a string.

My page behaves differently if my url contains #es or #pr , so I wanted to keep this comment at the end of each link.

Example:

<a href="/pagina/">Página</a>

would become if the #pr comment is in the url

<a href="/pagina/#pr">Página</a>

But I wanted to do this with all the links on the page.

A function that captures all href 's and that can put them after treated in proper places would also work.

    
asked by anonymous 02.05.2017 / 15:34

2 answers

1

To change the value can be something like:

$('a').each(function(index,item){
  var link = $(this).prop('href') + "#pr";
  $(this).prop('href', link);  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><ahref="/pagina/">Página 1</a>
<a href="/pagina/">Página 2</a>
<a href="/pagina/">Página 3</a>

In this way it is possible to iterate through all the links and change the ownership of each one.

    
02.05.2017 / 15:46
0

I did a brief search and found this.

You can use this Jquery code: $(window).on('hashchange', function(e){}); It detects change in the url, you can capture the new url and concatenate your code before the "original url". Adding your tags.

Topic with JSfiddle: StackOverflow Topic

Hug and succeed on your journey.

    
02.05.2017 / 15:52