How To Turn Query String Parameters Into A JavaScript Object

I recently was tasked at work to automatically grab some query string parameters on page load insert as hidden field data into a form to be submitted along with other info when the user submits the form.

The query string parameter data is sent along with the user info to the CRM for offline sales conversion tracking.

I ran across this code written by Travis Horn and so graciously gifted via this Github repo. It helps you to turn the query string parameter data into a JavaScript object, which makes it rather easy to work with and I only had to write these 3 lines of code to implement after importing the files from the Github repo:

const qstring = qs.get();
const gclid = qstring.gclid;
document.getElementById("gclidInput").value = gclid;

After my programming buddy looked at what I did though, considering I only needed the 'gclid' query string parameter, I could've accomplished the same with less code by just using the Location Search Property of the Location Object.

I'll probably end up doing that and when I do I'll be sure to let you know the code I wrote to achieve this.

Show Comments

var.pop( )

Web Development Tips & Tricks