/** | |
* $.parseParams - parse query string paramaters into an object. | |
*/ | |
(function($) { | |
var re = /([^&=]+)=?([^&]*)/g; | |
var decodeRE = /\+/g; // Regex for replacing addition symbol with a space | |
var decode = function (str) {return decodeURIComponent( str.replace(decodeRE, " ") );}; | |
$.parseParams = function(query) { | |
var params = {}, e; | |
while ( e = re.exec(query) ) { | |
var k = decode( e[1] ), v = decode( e[2] ); | |
if (k.substring(k.length - 2) === '[]') { | |
k = k.substring(0, k.length - 2); | |
(params[k] || (params[k] = [])).push(v); | |
} | |
else params[k] = v; | |
} | |
return params; | |
}; | |
})(jQuery); |
This comment has been minimized.
This comment has been minimized.
it's the reverse of
|
This comment has been minimized.
This comment has been minimized.
Would be interesting to handle arrays as well like : var url = '..?ferko=suska&ee=huu&topics[]=seo&topics[]=php'; // object { ferko: 'suska', ee: 'huu', topics:['seo', 'php'] } |
This comment has been minimized.
This comment has been minimized.
Excellent, thank you very much |
This comment has been minimized.
This comment has been minimized.
Here's a version that parses duplicate keys into an array without using a [] suffix on the key, and also handles a leading "?" so you can pass "document.location.search" as an argument.
|
This comment has been minimized.
This comment has been minimized.
Very useful! Been looking for something like this. |
This comment has been minimized.
This comment has been minimized.
very good, its really useful thanks |
This comment has been minimized.
This comment has been minimized.
I have upgraded the script, it now handles nested objects, array with and without index ...
|
This comment has been minimized.
This comment has been minimized.
And also, a function that converts object to URL is handy :-)
|
This comment has been minimized.
This comment has been minimized.
$.parseParams('http://mysite.com/?form[name]=test'); // form[0].NaN = test |
This comment has been minimized.
This comment has been minimized.
If this is used this in a project, do you have any licence or attribution requirements? |
This comment has been minimized.
This comment has been minimized.
@DimitryPHP have you solved that? |
This comment has been minimized.
This comment has been minimized.
Thanks Kares, it helps. |
This comment has been minimized.
This comment has been minimized.
Using this to fake up a $.serializeObject() for want of one. There are serializeObject() implements, but the first code example here is small and easily pasted in-line.
Thanks! |
This comment has been minimized.
This comment has been minimized.
Yeah, the upgraded version doesn't work for me at all. Firstly it requires a leading "?" and I just get data[Nan]: "1". The original was better. |
This comment has been minimized.
This comment has been minimized.
Solved data[NaN] problem, change line 51 from params[key][parseInt(index)] = value; To
|
This comment has been minimized.
This comment has been minimized.
dont work with more complex url, example:
Returns:
Please check this function:
|
This comment has been minimized.
This comment has been minimized.
There is a larger debate going on here - http://stackoverflow.com/q/1131630/104380 |
This comment has been minimized.
This comment has been minimized.
one line deParams :)
|
This comment has been minimized.
This comment has been minimized.
PHPstorm inspection says this part is not quite ideal: Same example in mozilla developer page: RegExp.prototype.exec() |
This comment has been minimized.
This comment has been minimized.
A jQuery plugin https://github.com/AceMetrix/jquery-deparam |
This comment has been minimized.
Could you provide some example? ... doesn't work for me...