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!

3
  • Please read How to Ask. Key phrases: "Search, and research" and "Explain ... any difficulties that have prevented you from solving it yourself". Commented May 2, 2017 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. Commented May 2, 2017 at 16:22
  • Related: get querystring params and make hyperlink and add param to url Commented May 2, 2017 at 16:25

1 Answer 1

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.