allmarkedup/jQuery-URL-Parser — Issue #16: Return multiple querystrings as list
Author: buchuki
Created: 2011-10-31 11:30:22+00:00
State: open

I am using jQuery.url with a form submitted via GET with several checkboxes having the same name but different value. The url looks like this:

http://example.com?box=1&box=2&box=3

the URL parser is currently clobbering the first two values, as such:

$.url('http://example.com/?box=1&box=2&box=3').param('box')
"3"

It would be better if it automatically detected multiple values and returned the collection as a list, thus:

$.url('http://example.com/?hello=1&hello=2&hello=3').param('hello')
["1", "2", "3"]

The following diff should do the trick, although I haven't tested it thoroughly.

diff --git a/nicepub/site_static/jquery.url.js b/nicepub/site_static/jquery.url.js
index 69e0921..ff24efd 100644
--- a/nicepub/site_static/jquery.url.js
+++ b/nicepub/site_static/jquery.url.js
@@ -47,7 +47,15 @@
uri.attr['query'].replace( querystring_parser, function ( $0, $1, $2 ){
if ($1)
{
- uri.param['query'][$1] = $2;
+ if (!uri.param['query'][$1]) {
+ uri.param['query'][$1] = $2;
+ }
+ else if ($.isArray(uri.param['query'][$1])){
+ uri.param['query'][$1].push($2);
+ }
+ else {
+ uri.param['query'][$1] = [uri.param['query'][$1], $2];
+ }
}
});

@@ -156,4 +164,4 @@

};