Get part of URL and add to specific links in page

Hi there,

I have the following URL:

http://www.website.com/?code=code1&ref=myref&utm_source=email&utm_medium=direct&utm_campaign=test

What I would like to do is add the

?code=code1&ref=myref&utm_source=email&utm_medium=direct&utm_campaign=test

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?

Thanks!

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 = window.location.search.slice(1)

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

1 Like

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