Parsing URL parameters - the easy way

Posted by Krzysztof Zbiciński on 2015-10-15.

I can’t believe that I couldn’t find a jQuery plugin or a vanilla-JS snippet that would parse the URL parameters infallibly. All of the solutions I stumbled upon were inconvenient to use or, probably surprisingly for their authors, failed when a parameter’s value contained the equal sign.

It felt like a right time to save the world myself, therefore I came up with the following code:

The snippet transforms this:

1
http://example.com/lorem?foo=bar&foo2=baz&foo3=

into this (mind the missing foo3 parameter):

1
2
3
4
{
foo: "bar",
foo2: "baz"
}

The code has been tested on major browsers and seems to work flawlessly. However if anything goes wrong (naah, it won’t happen), let me know about it.


Comments: