0

Possible Duplicate:
How can I get query string values?

ive a a function call like:

var ex = _.where(obj, {param1:0,param2:1});

which works fine, all the code behind.

I check my URL and take out the parameters and save this information into a array:

    var query = window.location.search.substring(1);

    var vars = query.split("&");

    var testParamter = vars.toString().replace( /=/g,':' );

that return by using console.log(testParamter); the following result:

param1:0,param2:1

but i cant insert yet the var testParamter into my function call:

var ex = _.where(obj, {testParamter});

because it is a string and i cant handel it with eval(). so can anyone tell me please the right way to solve it?

thanks

Community
  • 1
  • 1
JohnDoo
  • 103
  • 1
  • 2
  • 12
  • 2
    You've got a [XY-problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). See duplicate [How can I get query string values?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values) – Bergi Feb 04 '13 at 20:42
  • What do you want testParamter to be? – Aaron Kurtzhals Feb 04 '13 at 20:46

1 Answers1

0

Iterate over the values from the querystring and create an object from those values :

var query  = window.location.search.substring(1),
    vars   = query.split("&"),
    params = {};

$.each(vars, function(k,v) {
    var arr = v.split('=');
    params[ decode(arr[0]) ] = decode(arr[1]);
});

var ex = _.where(obj, params);

function decode(s) { 
    return decodeURIComponent(s.replace(/\+/g, " ")); 
}
adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Don't forget to [decode](http://stackoverflow.com/a/2880929/1048572) the parameter name and value. – Bergi Feb 04 '13 at 20:47
  • @Bergi - Sounds like a good idea, and nice link! Don't want to copy the answer, and the link is there for anyone interested, but I'll add the decodeURI without the regex replace etc. – adeneo Feb 04 '13 at 20:53
  • The replacing of `+` with spaces is quite importand, I'd say. – Bergi Feb 04 '13 at 21:11
  • hmm thanks a lot but the issue remains. if i print the params.toSource() i see double qoutes around the values. this is not accepted by the underscore.js ._where() function so i try in the each to remove the qoutes with no effect. if i inser these key/value pair directly everthings works. – JohnDoo Feb 05 '13 at 15:31