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)

tester
  • 22,441
  • 25
  • 88
  • 128
  • 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. – tester Aug 20 '11 at 00:20

2 Answers2

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);
}
jfriend00
  • 683,504
  • 96
  • 985
  • 979
1

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

Joe
  • 80,724
  • 18
  • 127
  • 145
  • sweet this ended up being the safest method. here is the snippet I took from BBQ: https://gist.github.com/1163405 – tester Aug 22 '11 at 20:16