Skip to content

Instantly share code, notes, and snippets.

@isogram
Forked from kares/jquery.parseparams.js
Created January 11, 2016 10:34
Show Gist options
  • Save isogram/0dd076dc55e1e9349dfe to your computer and use it in GitHub Desktop.
Save isogram/0dd076dc55e1e9349dfe to your computer and use it in GitHub Desktop.
jQuery.parseParams - parse query string paramaters into an object
/**
* $.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);
@isogram
Copy link
Author

isogram commented Jan 11, 2016

var url = 'www.example.com?ferko=suska&ee=huu';
$.parseParams( url.split('?')[1] || '' ); // object { ferko: 'suska', ee: 'huu' }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment