Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

marked as duplicate by Shog9, John Kugelman, Jonathan Lonowski, Jonathan Sampson, John Saunders Jul 23 '09 at 1:05

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
1  
    
Oh, and: stackoverflow.com/questions/150404/… –  Shog9 Jul 22 '09 at 2:43
    
    
interesting that the question is duplicate but the solutions are different. so equal question but not identical content == != === –  Elzo Valugi Aug 20 '09 at 7:32
add comment

2 Answers 2

up vote 5 down vote accepted

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",
}
share|improve this answer
1  
Perfect. Thanks. –  niaher Jul 22 '09 at 14:53
add comment

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');
share|improve this answer
    
While this is what I need, I think plugin is an overkill for such a simple problem. –  niaher Jul 22 '09 at 14:52
    
That would depend on the size of the plugin. These aren't large. –  Jonathan Sampson Jul 22 '09 at 15:12
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.