Nowadays when most of you do the heavy lifting in JS, chances are you want to lookup URL parameters. I definitely need it often, and I have a little snippet that converts params in your URL to a nice JS object.

Here it goes:

// this object gets filled with params
var params = {};

// split and map parameters'&').forEach(function(p) {
	p = p.split('=');
	params[p[0]] = decodeURIComponent(p[1] || 1);

There’s fancier solutions that try to assert types, but this is the most simple snippet I could come up with. Enjoy!

Update: As several pointed out, decodeURIComponent is probably a fair addition. Also, I replaced map with forEach, as it is the semantically correct way to loop (map saves bytes, ha!).For those who wonder, I’m returning “1” in the case of no value, as I want “truish” as standard for no-value. This is a purely personal, per-project decision.

Reply with a tweet or leave a Webmention. Both will appear here.