Star God: Fu Star God: Lu Star God: Shou


This function will take data in the form of a URL query string, separate it into its constituent name=value pairs and assign them all to an object with the names being properties of the object. Aside from the obvious use of getting the data out of an actual URL querystring, you can also use this to pass a lot of information from one function to another.


queryString = "firstname=Dan&lastname=Delaney&profession=Computer%20Programmer"
var result = parseQueryString(queryString);

Object looks like:

Add a querystring to the URL of this page and see it parsed below:

result = parseQueryString(location.search);


You can copy and paste any and all of these functions, or just download them all in one file.

 * Parses a querystring into a object consisting of the name=value pairs
 * Splits the querystring up and returns an object with properties that
 * correspond to the names of the variables in the querystring.
 * @author  Dan Delaney     http://fluidmind.org/
 * @param   queryString     Obviously, the query string to parse
function parseQueryString(queryString) {
    if(queryString == 'undefined' || queryString == '') {
        return false;
    } else {
        // Get rid of a leading '?' so that you can pass 'location.search' to this function
        if(queryString.substr(0, 1) == '?') { queryString = queryString.substr(1); }

        // Split up the querystring
        var components = queryString.split('&');

        // Assign each variable of the querystring to a new property of the final object
        var finalObject = new Object();
        var parts;
        for (var i = 0; i < components.length; i++) {
            parts = components[i].split('=');
            finalObject[parts[0]] = decodeURI(parts[1]);

        return finalObject;