-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqs.js
99 lines (99 loc) · 4.2 KB
/
qs.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*!
* qsHelper: Helper to read search and hash values from URL
*
* Version: 0.2
*
* Author: Wulf Weich
* Web: https://github.com/wweich/qsHelper
*
* Licensed under
* MIT License http://www.opensource.org/licenses/mit-license
* GPL v3 http://opensource.org/licenses/GPL-3.0
*
*/
qsHelper = {
qsObject: null,
parseQS: function () {
this.qsObject = {};
if (window.location.search && window.location.search.indexOf('=') > -1) {
this.qsObject['search'] = {};
if (window.location.search.indexOf('&') == -1) {
var tmpSearch = window.location.search.substr(1);
var searchItem = tmpSearch.split('=');
if (searchItem.length == 2) {
this.qsObject['search'][searchItem[0].toUpperCase()] = searchItem[1];
}
}
else {
var tmpSearch = window.location.search.substr(1).split('&');
for (var i = 0; i < tmpSearch.length; i++) {
var searchItem = tmpSearch[i].split('=');
if (searchItem.length == 2) {
this.qsObject['search'][searchItem[0].toUpperCase()] = searchItem[1];
}
}
}
}
if (window.location.hash && window.location.hash.indexOf('=') > -1) {
this.qsObject['hash'] = {};
if (window.location.hash.indexOf('&') == -1) {
var tmpHash = window.location.hash.substr(1);
var hashItem = tmpHash.split('=');
if (hashItem.length == 2) {
this.qsObject['hash'][hashItem[0].toUpperCase()] = hashItem[1];
}
}
else {
var tmpHash = window.location.hash.substr(1).split('&');
for (var i = 0; i < tmpHash.length; i++) {
var hashItem = tmpHash[i].split('=');
if (hashItem.length == 2) {
this.qsObject['hash'][hashItem[0].toUpperCase()] = hashItem[1];
}
}
}
}
},
search: function (name) {
if (!this.qsObject) this.parseQS();
if (this.qsObject['search'] && this.qsObject['search'][name.toUpperCase()]) return this.qsObject['search'][name.toUpperCase()];
return null;
},
hash: function (name) {
if (!this.qsObject) this.parseQS();
if (this.qsObject['hash'] && this.qsObject['hash'][name.toUpperCase()]) return this.qsObject['hash'][name.toUpperCase()];
return null;
},
hasSearch: function (name) {
if (!this.qsObject) { this.parseQS(); }
if (this.qsObject['search'] && this.qsObject['search'][name.toUpperCase()]) { return true; }
return false;
},
hasSearchValue: function (name) {
if (!this.qsObject) { this.parseQS(); }
if (this.qsObject['search'] && this.qsObject['search'][name.toUpperCase()] && this.qsObject['search'][name.toUpperCase()] != '') { return true; }
return false;
},
hasHash: function (name) {
if (!this.qsObject) { this.parseQS(); }
if (this.qsObject['hash'] && this.qsObject['hash'][name.toUpperCase()]) { return true; }
return false;
},
hasHashValue: function (name) {
if (!this.qsObject) { this.parseQS(); }
if (this.qsObject['hash'] && this.qsObject['hash'][name.toUpperCase()] && this.qsObject['hash'][name.toUpperCase()] != '') { return true; }
return false;
},
getSearchValue: function (name, altValue) {
if (!this.qsObject) { this.parseQS(); }
if (this.qsObject['search'] && this.qsObject['search'][name.toUpperCase()] && this.qsObject['search'][name.toUpperCase()] != '') { return this.qsObject['search'][name.toUpperCase()]; }
if (typeof altValue != 'undefined') { return altValue; }
return '';
},
getHashValue: function (name, altValue) {
if (!this.qsObject) this.parseQS();
if (this.qsObject['hash'] && this.qsObject['hash'][name.toUpperCase()] && this.qsObject['hash'][name.toUpperCase()] != '') { return this.qsObject['hash'][name.toUpperCase()]; }
if (typeof altValue != 'undefined') { return altValue; }
return '';
}
}