1

I am trying to pass url parameters through to the next pages by appending them to all the links on the page. The code I have works fine for this purpose.

I am encountering trouble when having anchor links on the page, as it puts the parameters after the URL and after the anchor as well, preventing the link from going to the anchor point.

Ex. website.com/?param1=test/#anchor?param1=test

Here is my code:

<script type="text/javascript">

jQuery(document).ready( function($) {

var params = window.location.search.replace(/\+/g,'%20'); 
var button = $('a[href]').each( function(i) { 
  this.href = this.href + params; 
}); 

});

</script>

Any help would be greatly appreciated!

  • 1
    See: [Adding a parameter to the URL with JavaScript](http://stackoverflow.com/questions/486896/adding-a-parameter-to-the-url-with-javascript) – AlliterativeAlice Nov 14 '13 at 21:08

1 Answers1

0

There you go:

var button = $('a[href]').each( function(i) { 
  var clearUrl = this.href.split("#");
  clearUrl[0] += params;
  this.href = clearUrl.join("#");
});
silicakes
  • 6,364
  • 3
  • 28
  • 39
  • Thanks for taking the time! Sorry for the late response, StackOverflow did not notify me that you answered my question. – user2993903 Dec 06 '13 at 01:16