Get part of URL and add to specific links in page

Hi there,

I have the following URL:

What I would like to do is add the


part of the URL to specific links on the page based on their ID’s. The ?code= and ?ref= are dynamic, so I would need to get the URL as a dynamic URL.

What would be the best way to do this?


Get everything after the ? and add those other links. The only care you need to take is appending an ? or & before the params depending on if the links already have a query string.

var params = window.location.href.split('?')[1]

// get links somehow and then
links.forEach(function(link) {
  var href = link.href
  href += (href.indexOf('?') == -1) ? '?' : '&'
  href += params
  link.href = href

is the same as

var params =

If you don’t need to drop the ? then you can simply reference

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.