-
Notifications
You must be signed in to change notification settings - Fork 24
/
querystring.js
54 lines (53 loc) · 1.82 KB
/
querystring.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
43
44
45
46
47
48
49
50
51
52
53
54
/* this independent library is looking for a home
it provides round-trip query string parsing & generating
Copyright 2016 AT&T Intellectual Property
License: Apache v2
*/
var querystring = (function() {
var listsep_ = '|';
return {
listsep: function(s) {
if(!arguments.length)
return listsep_;
listsep_ = s;
return this;
},
parse: function(opts) {
opts = opts || {};
return (function(a) {
if (a == "") return {};
var b = {};
for (var i = 0; i < a.length; ++i)
{
var p=a[i].split('=', 2);
if (p.length == 1)
b[p[0]] = opts.boolean ? true : "";
else
b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
}
return b;
})(window.location.search.substr(1).split('&'));
},
generate: function(m, encode) {
if(encode===undefined) encode = true;
var parts = [];
for(var k in m)
parts.push(k + '=' + (encode ? encodeURIComponent(m[k]) : m[k]));
return parts.length ? parts.join('&') : '';
},
get_url: function(m, encode) {
var url = window.location.protocol + '//' + window.location.host + window.location.pathname;
var params = this.generate(m, encode);
if(params)
url += '?' + params;
return url;
},
update: function(m, encode) {
window.history.pushState(null, null, this.get_url(m, encode));
return this;
},
option_tracker: function() {
throw new Error('use independent url_options library');
}
};
})();