1

Use Case: I want to have dynamic button on a landing page that links to a different URL based on the parameters in the landing page URL.

For example: landing page link = www.testpage.com/?dc=secondtestpage.com

Desired Button Link = 'https://www.' + dc + '?etxratrackingparameters' with the outcome = "https://www.secondpagetest.com?extratrackingparameters"

I found this code to pull data from parameters:

// Parse the URL parameter
function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}
// Give the parameter a variable name
var dynamicContent = getParameterByName('dc');

This allows me to pull the dc parameter into a variable. Now I need to know how to use that variable to create URL by concatenating it to other variables or strings.

Hopefully this easy for any Javascript experts (which I am not!). Any help is appreciated.

Thanks!

Atung9801
  • 13
  • 1
  • 4
  • Please read [ask]. Key phrases: "Search, and research" and "Explain ... any difficulties that have prevented you from solving it yourself". – Heretic Monkey May 02 '17 at 16:20
  • Are there multiple parameters in the URL or just one? As it stands, this question is very broad and difficult to answer. – styfle May 02 '17 at 16:22
  • Related: [get querystring params](http://stackoverflow.com/q/901115) and [make hyperlink](http://stackoverflow.com/q/8005694) and [add param to url](http://stackoverflow.com/q/486896) – styfle May 02 '17 at 16:25

1 Answers1

2

Building on what you already have. Just use string concatenation to build the url, and use jQuery's .html() to create a link:

var dynamicContent = "secondpagetest.com";
var url = "https://www."+dynamicContent+"?extratrackingparameters";
$('#container').html('<a href="'+url+'">link</a>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container"></div>
Fabricator
  • 12,722
  • 2
  • 27
  • 40