Work with Query String Data

The query string (everything after the question mark in the URL) can be parsed with a few simple string functions. The code below automatically parses the query string and stores it in a variable called query. Given a URL of the form kittens.html?x=1&y=2, the query object will contain an X and Y property with the proper values.

var query = (function() {
    function decode(string) {
        return decodeURIComponent(string.replace(/\+/g, " "));
    var result = {};
    if ( {'&').forEach(function(pair) {
            pair = pair.split('=');
            result[decode(pair[0])] = decode(pair[1]);
    return result;

Author: Jakub Jankiewicz
Tags: misc

comments powered by Disqus