JavaScript query string [closed]

JavaScript query string [closed]

Is there any JavaScript library that makes a dictionary out of the query string, ASP.NET style?
Something which can be used like:
var query = window.location.querystring[“query”]?

Is “query string” called something else outside the .NET realm? Why isn’t broken into a key/value collection ?
EDIT: I have written my own function, but does any major JavaScript library do this?


Solution 1:


This is the fork of it

Solution 2:

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

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

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

  return result;

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

Solution 3:

tl;dr solution on a single(ish) line of code using vanilla javascript

var queryDict = {}"&").forEach(function(item) {
    queryDict[item.split("=")[0]] = item.split("=")[1]

For querystring ?a=1&b=2&c=3&d&eit returns:

> queryDict
a: "1"
b: "2"
c: "3"
d: undefined
e: undefined

multi-valued keys and encoded characters?

See the original answer at How can I get query string values in JavaScript?

> queryDict
a: ["1", "5", "t e x t"]
b: ["2"]
c: ["3"]
d: [undefined]
e: [undefined, " test.asp?name=ståle&car=saab"]

Solution 4:

After finding this post, when looking myself I thought I should add that I don’t think the most up-voted solution is the best. It doesn’t handle array values (such as ?a=foo&a=bar – in this case I would expect getting a to return [‘foo’, ‘bar’]). It also as far as I can tell doesn’t take into account encoded values – such as hex character encoding where %20 represents a space (example: ?a=Hello%20World) or the plus symbol being used to represent a space (example: ?a=Hello+World).

Node.js offers what looks like a very complete solutions to querystring parsing. It would be easy to take out and use in your own project as its fairly well isolated and under a permissive licence.

The code for it can be viewed here:

The tests that Node has can be seen here: I would suggest trying some of these with the popular answer to see how it handles them.

There is also a project that I was involved in to specifically add this functionality. It is a port of the Python standard lib query string parsing module. My fork can be found here:

Solution 5:

Or you could use the library sugar.js.


Object.fromQueryString ( str , deep = true )

Converts the query string of a URL into an object. If deep is false,
conversion will only accept shallow params (ie. no object or arrays
with [] syntax) as these are not universally supported.

Object.fromQueryString('foo=bar&broken=wear') >{"foo":"bar","broken":"wear"}
Object.fromQueryString('foo[]=1&foo[]=2') >{"foo":[1,2]}


var queryString = Object.fromQueryString(;
var foo =;

Solution 6:

If you have the querystring on hand, use this:

 * @param qry the querystring
 * @param name name of parameter
 * @returns the parameter specified by name
 * @author

function getQueryStringParameter(qry,name){
    if(typeof qry !== undefined && qry !== ""){
        var keyValueArray = qry.split("&");
        for ( var i = 0; i < keyValueArray.length; i++) {
                return keyValueArray[i].split("=")[1];
    return "";