Skip to content

Instantly share code, notes, and snippets.

@mpezzi
Created August 25, 2011 14:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpezzi/1170858 to your computer and use it in GitHub Desktop.
Save mpezzi/1170858 to your computer and use it in GitHub Desktop.
jQuery Query Plugin
/**
* jQuery Query Plugin by M. Pezzi
* Version: 1.0 (08/25/11)
* https://gist.github.com/1170858
* Dual licensed under the MIT and GPL licences:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
* Requires: jQuery v1.4.2 or later
*/
(function($){
$.query = function(key, value) {
var e, a = /\+/g, r = /([^&=]+)=?([^&]*)/g,
d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
q = window.location.search.substring(1),
urlParams = {};
while ( e = r.exec(q) )
urlParams[d(e[1])] = d(e[2]);
if ( typeof key !== 'undefined' && typeof value == 'undefined' ) {
return urlParams[key];
} else if ( typeof key !== 'undefined' && typeof value !== 'undefined' ) {
return ( urlParams[key] == value );
} else {
return urlParams;
}
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment