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)

asked Aug 19, 2011 at 23:47
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);
}
answered Aug 20, 2011 at 1:25

Comments

1
answered Aug 20, 2011 at 2:05

1 Comment

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.