4
var obj = {
    'key': 'value',
    'cheese':'bacon',
    '&':'>'
};    
var params = $.param(obj)

console.log(params); // key=value&cheese=bacon&%26=%3E

how do I turn params back into an object? (exactly what it was before)

1
  • all of those methods help you find a single param in the string. I want a single object with keys and values of the querystring. Commented Aug 20, 2011 at 0:20

2 Answers 2

5

You could use something like this. I'm not aware of a jQuery built-in for this.

function getUrlVars() {
    if (!window.location.search) {
        return({});   // return empty object
    }
    var parms = {};
    var temp;
    var items = window.location.search.slice(1).split("&");   // remove leading ? and split
    for (var i = 0; i < items.length; i++) {
        temp = items[i].split("=");
        if (temp[0]) {
            if (temp.length < 2) {
                temp.push("");
            }
            parms[decodeURIComponent(temp[0])] = decodeURIComponent(temp[1]);        
        }
    }
    return(parms);
}
Sign up to request clarification or add additional context in comments.

Comments

1

jQuery BBQ has a deparam function: https://github.com/cowboy/jquery-bbq/blob/master/jquery.ba-bbq.js line 466

1 Comment

sweet this ended up being the safest method. here is the snippet I took from BBQ: gist.github.com/1163405

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.