Get Query Params as Object

Chris Coyier on

Nicholas Ortenzio wrote this little plugin:

jQuery.extend({ getQueryParameters : function(str) { return (str || document.location.search).replace(/(^\?)/,'').split("&").map(function(n){return n = n.split("="),this[n[0]] = n[1],this}.bind({}))[0]; } });

So if the URL is:

You can do:

var queryParams = $.getQueryParameters();

And queryParams will be an object like:

{ "lunch": "sandwich", "dinner": "stirfry" }