Your generous donation allows me to continue developing and updating my code!
jQuery BBQ includes a powerful jQuery.deparam method that is capable of fully deserializing not only any params string that jQuery.param can create, but that PHP and Rails (and hopefully everything else) can create. And even though only the query string is being parsed this example, methods for parsing a params string out of the fragment (hash) as well parsing any stand-alone params string are included. jQuery BBQ can also be used to merge params from multiple URLs or objects into a new URL, even within element attributes. See the documentation for a full list of methods!
Sample query strings for you to click:
- Arrays encoded like this won't work as-expected in PHP / Rails, but work in BBQ and many older server-side frameworks (jQuery 1.3.2 or older $.param, jQuery 1.4 with $.param.traditional = true):
?a=1&a=2&a=3&b=4&c=true&d=0 - Arrays encoded like this will work as-expected in PHP / Rails / BBQ (jQuery 1.4 or newer $.param):
?a[]=1&a[]=2&a[]=3&b=4&c=true&d=0 - jQuery BBQ and PHP can handle non-shallow arrays, but Rails (rack) cannot (jQuery 1.4 or newer $.param):
?a[]=0&a[1][]=1&a[1][]=2&a[2][]=3&a[2][1][]=4&a[2][1][]=5&a[2][2][]=6&a[3][b][]=7&a[3][b][1][]=8&a[3][b][1][]=9 &a[3][b][2][0][c]=10&a[3][b][2][0][d]=11&a[3][b][3][0][]=12&a[3][b][4][0][0][]=13&a[3][b][5][e][f][g][]=14 &a[3][b][5][e][f][g][1][]=15&a[3][b][]=16&a[]=17 - Some simple implicitly non-string values that BBQ can optionally coerce:
?a=true&b=false&c=undefined&d=&f=1&g=2&h=hello+world - More nested data structures. Since the arrays are shallow, this works in Rails (rack).
?a[]=4&a[]=5&a[]=6&b[x][]=7&b[y]=8&b[z][]=9&b[z][]=0&b[z][]=true&b[z][]=false&b[z][]=undefined&b[z][]=&c=1
Urldecoded query string
Query string params, as parsed by jQuery BBQ (and converted to JSON)
$.deparam.querystring();
$.deparam.querystring( true );
GET params, as parsed by PHP
print_r( $_GET );
Array ( )
The code
$(function(){ // Values are not coerced. var params = $.deparam.querystring(); debug.log( 'not coerced', params ); $('#deparam_string').text( JSON.stringify( params, null, 2 ) ); // Values are coerced. params = $.deparam.querystring( true ); debug.log( 'coerced', params ); $('#deparam_coerced').text( JSON.stringify( params, null, 2 ) ); // Highlight the current sample query string link var qs = $.param.querystring(); $('li a').each(function(){ if ( $(this).attr( 'href' ) === '?' + qs ) { $(this).addClass( 'current' ); } }); });