Get url parameters with javascript

There are times you will need to grab url parameters, then update some of your form components accordingly. In my case, it was a search form that passes search terms using GET.

I largely got the javascript function from here, but I added a few lines to decode/unescape values.

Here it is.

function getUrlParameter(sParam) {
  var sPageURL =;
  var sURLVariables = sPageURL.split('&');
  for (var i = 0; i < sURLVariables.length; i++) {
    var sParameterName = sURLVariables[i].split('=');
    if (sParameterName[0] == sParam) {
      var val = sParameterName[1];
      //console.log("before: " + val)
      val = decodeURIComponent(val);
      val = val.replace(/+/g, " ");
      //console.log("after: " + val)
      return val;

$(function() {
  // pre-set our search control components based on the URL params
  // more fields...

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s