DEV Community

Darpan Wani
Darpan Wani

Posted on

How to fetch full URL and store in hidden field on pardot form

This is my code added to the Pardot form below section,

// Parse the URL

function getParameterByName(name) {
name = name.replace(/[[]/, "\[").replace(/[]]/, "\]");
var regex = new RegExp("[\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/+/g, " "));
}

// Give the URL parameters variable names

var source = getParameterByName('utm_source');
var medium = getParameterByName('utm_medium');
var campaign = getParameterByName('utm_campaign');
var content = getParameterByName('utm_content');
var term = getParameterByName('utm_term');
var campaignID = getParameterByName('URL_Campaign_Id');

// GET the URL of the Parent Page

var url = window.location.href;

// Put the variable names into the hidden fields in the form. selector should be "p.YOURFIELDNAME input"

document.querySelector("p.utm_source input").value = source;
document.querySelector("p.utm_medium input").value = medium;
document.querySelector("p.utm_campaign input").value = campaign;
document.querySelector("p.utm_content input").value = content;
document.querySelector("p.utm_term input").value = term;
document.querySelector("p.URL_Campaign_Id input").value = campaignID;
document.querySelector('input[name="Source_URL"]').value = url;

Image description
I want to fetch the URL of the form and store it in the hidden field(Source_URL) when someone submits the Pardot form the whole code works fine but just the part of fetching the URL and storing to "Source_URL" hidden field is not working.

Does anyone have a solution what change do I have to make here?

Adding image of code also to clearly understand the code

Top comments (0)