Skip to main content
Respect the original answerer and also respect other answers, which already utilize what was suggested in the update.
Source Link

You can extract the key/value pairs from the [location.search][1]location.search property, this property has the part of the URL that follows the ?? symbol, including the ?? symbol.

function queryObjgetQueryString() {
    var result = {}, queryString = location.search.slice(1),
        re = /([^&=]+)=([^&]*)/g, m;
    
    while (m = re.exec(queryString)) {
        result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
    }
    
    return result;
}

// Usage:
var myParam = queryObj()["myParam"];

##Update:

No need to use regex:

function queryObj() {
    var result = {}, keyValuePairs = location.search.slice(1).split('&');

    keyValuePairs.forEach(function(keyValuePair) {
        keyValuePair = keyValuePair.split('=');
        result[keyValuePair[0]] = keyValuePair[1] || '';
    });
    
    return result;
}

For IE8- support include this tiny Array.forEach* polyfill:

if ( !Array.prototype.forEach ) {
    Array.prototype.forEach = function(fn, scope) {
        for(var i = 0, lenmyParam = this.length; i < len; ++i) {
            fn.callgetQueryString(scope, this[i], i, this);
        }
    }
}["myParam"];

From: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach

*Array.forEach aka the better for loop [1]: https://developer.mozilla.org/en/DOM/window.location

You can extract the key/value pairs from the [location.search][1] property, this property has the part of the URL that follows the ? symbol, including the ? symbol.

function queryObj() {
    var result = {}, queryString = location.search.slice(1),
        re = /([^&=]+)=([^&]*)/g, m;
    
    while (m = re.exec(queryString)) {
        result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
    }
    
    return result;
}

// Usage:
var myParam = queryObj()["myParam"];

##Update:

No need to use regex:

function queryObj() {
    var result = {}, keyValuePairs = location.search.slice(1).split('&');

    keyValuePairs.forEach(function(keyValuePair) {
        keyValuePair = keyValuePair.split('=');
        result[keyValuePair[0]] = keyValuePair[1] || '';
    });
    
    return result;
}

For IE8- support include this tiny Array.forEach* polyfill:

if ( !Array.prototype.forEach ) {
    Array.prototype.forEach = function(fn, scope) {
        for(var i = 0, len = this.length; i < len; ++i) {
            fn.call(scope, this[i], i, this);
        }
    }
}

From: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach

*Array.forEach aka the better for loop [1]: https://developer.mozilla.org/en/DOM/window.location

You can extract the key/value pairs from the location.search property, this property has the part of the URL that follows the ? symbol, including the ? symbol.

function getQueryString() {
  var result = {}, queryString = location.search.slice(1),
      re = /([^&=]+)=([^&]*)/g, m;

  while (m = re.exec(queryString)) {
    result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
  }

  return result;
}

// ...
var myParam = getQueryString()["myParam"];
added 694 characters in body
Source Link
Web_Designer
  • 75k
  • 93
  • 210
  • 268

You can extract the key/value pairs from the location.search[location.search][1] property, this property has the part of the URL that follows the ?? symbol, including the ?? symbol.

function getQueryStringqueryObj() {
    var result = {}, queryString = location.search.slice(1),
        re = /([^&=]+)=([^&]*)/g, m;
    
    while (m = re.exec(queryString)) {
        result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
    }
    
    return result;
}

// Usage:
var myParam = queryObj()["myParam"];

##Update:

No need to use regex:

function queryObj() {
    var result = {}, keyValuePairs = location.search.slice(1).split('&');

    keyValuePairs.forEach(function(keyValuePair) {
        keyValuePair = keyValuePair.split('=');
        result[keyValuePair[0]] = keyValuePair[1] || '';
    });
    
    return result;
}

For IE8- support include this tiny Array.forEach* polyfill:

if ( !Array.prototype.forEach ) {
    Array.prototype.forEach = function(fn, scope) {
        for(var myParami = getQueryString0, len = this.length; i < len; ++i) {
            fn.call(scope, this[i], i, this)["myParam"];;
        }
    }
}

From: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach

*Array.forEach aka the better for loop [1]: https://developer.mozilla.org/en/DOM/window.location

You can extract the key/value pairs from the location.search property, this property has the part of the URL that follows the ? symbol, including the ? symbol.

function getQueryString() {
  var result = {}, queryString = location.search.slice(1),
      re = /([^&=]+)=([^&]*)/g, m;

  while (m = re.exec(queryString)) {
    result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
  }

  return result;
}

// ...
var myParam = getQueryString()["myParam"];

You can extract the key/value pairs from the [location.search][1] property, this property has the part of the URL that follows the ? symbol, including the ? symbol.

function queryObj() {
    var result = {}, queryString = location.search.slice(1),
        re = /([^&=]+)=([^&]*)/g, m;
    
    while (m = re.exec(queryString)) {
        result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
    }
    
    return result;
}

// Usage:
var myParam = queryObj()["myParam"];

##Update:

No need to use regex:

function queryObj() {
    var result = {}, keyValuePairs = location.search.slice(1).split('&');

    keyValuePairs.forEach(function(keyValuePair) {
        keyValuePair = keyValuePair.split('=');
        result[keyValuePair[0]] = keyValuePair[1] || '';
    });
    
    return result;
}

For IE8- support include this tiny Array.forEach* polyfill:

if ( !Array.prototype.forEach ) {
    Array.prototype.forEach = function(fn, scope) {
        for(var i = 0, len = this.length; i < len; ++i) {
            fn.call(scope, this[i], i, this);
        }
    }
}

From: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach

*Array.forEach aka the better for loop [1]: https://developer.mozilla.org/en/DOM/window.location

Updated code to use .slice() instead of .substring()
Source Link
Web_Designer
  • 75k
  • 93
  • 210
  • 268

You can extract the key/value pairs from the location.search property, this property has the part of the URL that follows the ? symbol, including the ? symbol.

function getQueryString() {
  var result = {}, queryString = location.search.substringslice(1),
      re = /([^&=]+)=([^&]*)/g, m;

  while (m = re.exec(queryString)) {
    result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
  }

  return result;
}

// ...
var myParam = getQueryString()["myParam"];

You can extract the key/value pairs from the location.search property, this property has the part of the URL that follows the ? symbol, including the ? symbol.

function getQueryString() {
  var result = {}, queryString = location.search.substring(1),
      re = /([^&=]+)=([^&]*)/g, m;

  while (m = re.exec(queryString)) {
    result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
  }

  return result;
}

// ...
var myParam = getQueryString()["myParam"];

You can extract the key/value pairs from the location.search property, this property has the part of the URL that follows the ? symbol, including the ? symbol.

function getQueryString() {
  var result = {}, queryString = location.search.slice(1),
      re = /([^&=]+)=([^&]*)/g, m;

  while (m = re.exec(queryString)) {
    result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
  }

  return result;
}

// ...
var myParam = getQueryString()["myParam"];
improved function
Source Link
Christian C. Salvadó
  • 831k
  • 185
  • 929
  • 845
Loading
added usage example
Source Link
Christian C. Salvadó
  • 831k
  • 185
  • 929
  • 845
Loading
deleted 12 characters in body
Source Link
Christian C. Salvadó
  • 831k
  • 185
  • 929
  • 845
Loading
Source Link
Christian C. Salvadó
  • 831k
  • 185
  • 929
  • 845
Loading