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)
asked Aug 19, 2011 at 23:47
-
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– tester2011年08月20日 00:20:52 +00:00Commented Aug 20, 2011 at 0:20
2 Answers 2
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);
}
answered Aug 20, 2011 at 1:25
Comments
jQuery BBQ has a deparam function: https://github.com/cowboy/jquery-bbq/blob/master/jquery.ba-bbq.js line 466
answered Aug 20, 2011 at 2:05
1 Comment
tester
sweet this ended up being the safest method. here is the snippet I took from BBQ: gist.github.com/1163405
lang-js