Skip to content

Instantly share code, notes, and snippets.

@cschmidt
Created October 21, 2010 20:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cschmidt/639301 to your computer and use it in GitHub Desktop.
Save cschmidt/639301 to your computer and use it in GitHub Desktop.
How to pass the Google Adwords gclid parameter onto your form confirmation page
<script>
<!--
(function() {
function getQueryParam(paramName) {
var query = window.location.search;
var paramValue = null;
var pos = query.indexOf(paramName + '=');
if (pos > -1) {
var start = pos + paramName.length + 1;
var end = query.indexOf('&', start);
end = end > -1 ? end : query.length;
paramValue = query.substring(start, end);
}
return paramValue;
}
var gclid = getQueryParam("gclid");
if (gclid != null) {
var previous_form_url =
window.module.lp.form.data['confirmData'];
window.module.lp.form.data['confirmData'] =
previous_form_url + "?gclid=" + gclid;
}
}) ();
-->
</script>
@cgilchrist
Copy link

A shorter alternative to getQueryParam that I use sometimes is:

var getQueryParam = function(name){
  var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
  return results ? (results[1] || null) : null;
}

@cschmidt
Copy link
Author

Yeah, that's certainly more compact and covers more cases. Is window.location.href more widely supported than window.location.search?

@cgilchrist
Copy link

Good question - I can't see any reason not to use location.search instead of location.href -- no difference in support across browsers as far as I know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment