1

I am trying to pull URL parameters to pass to a prebuilt HTML form.

The example URL is:

?state=CA&**period**=PERIOD_FIXED_30YEARS&**loan**=200000&ltv=80&**transaction**=54&property_type=34&fico=740&occupancy=49&cashout=0&rate=4.125&fees=510&points=0.204&trackingID=1445830871741638005

The text in bold is what I would need to pull.

I was trying to do this through:

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, " "));
}

//Gathering the URL Parameters
var period = getParameterByName('period');
var loan = getParameterByName('loan');
var ltv = getParameterByName('ltv');
var transaction = getParameterByName('transaction');
var property_type = getParameterByName('property_type');
var fico = getParameterByName('fico');
var occupancy = getParameterByName('occupancy');
var cashout = getParameterByName('cashout');
var rate = getParameterByName('rate');
var fees = getParameterByName('fees');
var points = getParameterByName('points');
var trackingID = getParameterByName('trackingID');

And then assign to the form using

document.getElementById("tfa_49").value = period;
document.getElementById("tfa_50").value = loan;

I keep getting a null error and I think it is the regex.

Any help would be appreciated. I searched similar questions on the site and none of the answers worked in my situation.

UPDATING WITH WORKING CODE:

<script type="text/javascript">


//Used to pull URL
  
const link = window.location.href;
const url = new URL(link);


//Function to get the URL parameters
function getParameterByName(name) {
  return url.searchParams.get(name);
}

//Gathering the URL Parameters and writing to console to check if parse is accurate -- Note the console.log can be removed if needed
var period = getParameterByName('period');
console.log(period);

var loan = getParameterByName('loan');
console.log(loan);

var ltv = getParameterByName('ltv');
console.log(ltv);

var transaction = getParameterByName('transaction');
console.log(transaction);

var property_type = getParameterByName('property_type');
console.log(property_type);

var fico = getParameterByName('fico');
console.log(fico);

var occupancy = getParameterByName('occupancy');
console.log(occupancy);

var cashout = getParameterByName('cashout');
console.log(cashout);

var rate = getParameterByName('rate');
console.log(rate);

var fees = getParameterByName('fees');
console.log(fees);

var points = getParameterByName('points');
console.log(points);

var trackingID = getParameterByName('trackingID');
console.log(trackingID);




//On load function to prefill the forms hidden fields
$( document ).ready(function() {
  document.getElementById("tfa_49").value = period;
  document.getElementById("tfa_50").value = loan;
  document.getElementById("tfa_51").value = ltv;
  document.getElementById("tfa_53").value = transaction;
  document.getElementById("tfa_54").value = property_type;
  document.getElementById("tfa_55").value = fico;
  document.getElementById("tfa_56").value = occupancy;
  document.getElementById("tfa_57").value = cashout;
  document.getElementById("tfa_58").value = rate;
  document.getElementById("tfa_59").value = fees;
  document.getElementById("tfa_60").value = points;
  document.getElementById("tfa_61").value = trackingID;
});


</script>

1 Answers1

1

You can create an URL object and then use the url.searchParams.get(param) method to get the desired parameter. More information.

Working example:

const link = "http://www.example.com/test.html?state=CA&period=PERIOD_FIXED_30YEARS&loan=200000&ltv=80&transaction=54&property_type=34&fico=740&occupancy=49&cashout=0&rate=4.125&fees=510&points=0.204&trackingID=1445830871741638005";
const url = new URL(link);

function getParameterByName(name) {
  return url.searchParams.get(name);
}

//Gathering the URL Parameters
var period = getParameterByName('period');
console.log(period);

The hardcoded url is just for the example with your URL, in your code use:

const url = window.location.href
Tamas Szoke
  • 5,426
  • 4
  • 24
  • 39
  • I think the confusion I have here is how to assign that variable to form and then for the "const link" how does that generate automatically? – Tristan Nohrer May 08 '20 at 20:00
  • You can use `window.location.href ` for that, I just hardcoded it for the example. – Tamas Szoke May 08 '20 at 20:05
  • This is the error I am getting when I try this: '''Uncaught TypeError: Cannot read property 'get' of undefined at getParameterByName (55?state=CA&period=PERIOD_FIXED_30YEARS&loan=200000&ltv=80&transaction=54&property_type=34&fico=740&occupancy=49&cashout=0&rate=4.125&fees=510&points=0.204&trackingID=1445830871741638005:73) at 55?state=CA&period=PERIOD_FIXED_30YEARS&loan=200000&ltv=80&transaction=54&property_type=34&fico=740&occupancy=49&cashout=0&rate=4.125&fees=510&points=0.204&trackingID=1445830871741638005:77''' – Tristan Nohrer May 08 '20 at 20:23