forked from rapportive-oss/jquery-parsequery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.parsequery.js
42 lines (38 loc) · 1.41 KB
/
jquery.parsequery.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Copyright (c) 2010 Conrad Irwin <[email protected]> MIT license.
* Based loosely on original: Copyright (c) 2008 mkmanning MIT license.
*
* Parses CGI query strings into javascript objects.
*
* See the README for details.
**/
(function ($) {
$.parseQuery = function (options) {
var config = {query: window.location.search || ""},
params = {};
if (typeof options === 'string') {
options = {query: options};
}
$.extend(config, $.parseQuery, options);
config.query = config.query.replace(/^\?/, '');
$.each(config.query.split(config.separator), function (i, param) {
var pair = param.split('='),
key = config.decode(pair.shift(), null).toString(),
value = config.decode(pair.length ? pair.join('=') : null, key);
if (config.array_keys.test ? config.array_keys.test(key) : config.array_keys(key)) {
params[key] = params[key] || [];
params[key].push(value);
} else {
params[key] = value;
}
});
return params;
};
$.parseQuery.decode = $.parseQuery.default_decode = function (string) {
return decodeURIComponent((string || "").replace(/\+/g, ' '));
};
$.parseQuery.array_keys = function () {
return false;
};
$.parseQuery.separator = "&";
}(window.jQuery || window.Zepto));