-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryString.js
80 lines (69 loc) · 2.13 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
(function () {
function QueryString(query) {
var self = this;
var queryObject = {};
//Private Methods
var updateQueryObject = function () {
var queryString = '';
if (query) {
queryString = query;
} else {
queryString = window.location.search.substring(1);
}
var params = {}, queries, temp, i, l;
queries = queryString.split('&');
for (i = 0, l = queries.length; i < l; i++) {
if (queries[i].split('=')[0] != '' && queries[i].split('=')[1] != '') {
temp = queries[i].split('=');
queryObject[temp[0]] = temp[1];
}
}
};
var assembleQueryString = function (queryObject) {
var qString = '?';
for (var key in queryObject) {
if (
(typeof (queryObject[key]) != 'function') &&
(typeof (queryObject[key]) != 'boolean') &&
queryObject[key]
) {
qString += key + '=' + queryObject[key] + '&';
}
};
return qString.slice(0, qString.length - 1);
};
//Public Properties
this.autoUpdate = true;
//Public Methods
this.get = function (name) {
if (typeof name == 'string')
return queryObject[name];
else
console.error('Get method requires a string forthe name argument');
};
this.set = function (name, value) {
if (typeof name == 'string' && typeof value == 'string')
queryObject[name] = value;
if (self.autoUpdate) self.update();
else
console.error('Set method requires a string for both name and value arguments');
};
this.getQueryString = function () {
return assembleQueryString(queryObject);
};
this.getFullUrl = function () {
var newUrl = location.protocol + '//' + location.host +
location.pathname + assembleQueryString(queryObject);
return newUrl;
};
this.update = function() {
history.replaceState('', '', self.getFullUrl());
};
this.go = function () {
location.href = getFullUrl();
};
//Initial Setup
updateQueryObject();
}
window.QueryString = new QueryString();
})();