How Do You Get the URL Parameters and Append to Href or Src of Another Element?


How Do You Get the URL Parameters and Append to Href or Src of Another Element?

Do you want to grab the url parameters and pass them to a button href or iframe src? This is common when you are running a paid ad campaign to a landing page that links to a form on another page. You want to track the source of the form submission by duplicating the url parameters on the form page. Below is the code to help you do that dynamicly.

Table of Contents

HTML

<a href="/" id="registerBtn">Register Now</a>

JavaScript

// Apply URL parameters to Register Button if present
var changeTarget = document.getElementById('registerBtn'); 
var params = window.location.search;
var _href = changeTarget.href; 

// Does the page have url parameters ?
if (params) {
    // yes - add them to the iframe src
    changeTarget.href = _href+params;
    console.log("url params = " + params);
} else {
    // no - use a generic iframe src
    changeTarget.href = _href;
}

 



Written by: Jake Lett
Jake Lett is a results-driven B2B marketing consultant with 15+ years of experience helping businesses boost online visibility, generate qualified leads, and increase sales. Specializing in SEO, HubSpot, and PPC campaign management, Jake leverages proven strategies to deliver measurable growth. As a Certified Google Ads Specialist and HubSpot Developer, he is committed to driving impactful results for his clients.

Related posts

Tags:

Want to Get Email Updates of New Articles?

Join My Email Newsletter