← Back to blog
·1 min read
How to obtain url parameters with JQuery
We'll write a quick plugin in JQuery with a little bit of our beloved REGEX to obtain the URL parameters.
(function($) {
$.get = function(key) {
key = key.replace(/[\[]/, '\\[');
key = key.replace(/[\]]/, '\\]');
var pattern = "[\\?&]" + key + "=([^&#]*)";
var regex = new RegExp(pattern);
var url = unescape(window.location.href);
var results = regex.exec(url);
if (results === null) {
return null;
} else {
return results[1];
}
}
})(jQuery);
Now, to use it just execute the code below
// for this url http://somehost.com/folder/?id=xx&other=param
var a = $.get("id");
var b = $.get("other");