9

Possible Duplicates:
JavaScript query string
get querystring with jQuery

Is there an object/method in JavaScript to turn a string like this: param1=2&param2=1&param3=5 into some sort of dictionary, so that I can refer to each element as mystring['param1'] or mystring[0]?

Can jQuery help here?

Alex
  • 1,197
  • 1
  • 10
  • 22
niaher
  • 9,205
  • 7
  • 65
  • 85

2 Answers2

8

this is my try.

var a = 'param1=2&param2=1&param3=5';
var b = a.split('&');
var final ={};
$.each(b, function(x,y){
    var temp = y.split('=');
    final[temp[0]] = temp[1];
});
console.log( final);

This returns an object like the dictionary that you needed:

{
    param1 : "2",
    param2 : "1",
    param3 : "5",
}
Elzo Valugi
  • 25,880
  • 14
  • 91
  • 114
2

There's a plugin for this. http://plugins.jquery.com/project/query-object - You can play with an online demo of it at : http://test.blairmitchelmore.com/jquery.query/?name=jonathan&age=26

There's also jqUrl, which allows you to call items form the query string like this:

$.jqURL.get('var2');
Sampson
  • 259,174
  • 73
  • 529
  • 557