forked from walterg2/parseQueryString
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparseQueryString.js
More file actions
executable file
·48 lines (46 loc) · 1.24 KB
/
parseQueryString.js
File metadata and controls
executable file
·48 lines (46 loc) · 1.24 KB
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
( function() {
function queryStringConstructor() {
this.params = {};
var qs = location.search.substring(1, location.search.length),
i = 0,
args;
if (qs.length > 0) {
// Turn <plus> back to <space>
// See:
// http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
qs = qs.replace(/\+/g, ' ');
// parse out name/value pairs separated via &
args = qs.split('&');
// split out each name=value pair
for ( i = 0; i < args.length; i = i + 1) {
var pair = args[i].split('=');
var name = decodeURIComponent(pair[0]);
var value = (pair.length === 2) ? decodeURIComponent(pair[1]) : null;
this.params[name] = value;
}
}
return this;
}
function parseQueryString() {
return new queryStringConstructor(arguments);
}
queryStringConstructor.prototype = {
getValue : function(key, defaultText) {
var value = this.params[key];
return (value !== null) ? value : defaultText;
},
contains : function(key) {
var value = this.params[key];
return (value !== null);
},
getKeys : function() {
var keys = [],
key;
for ( key in this.params) {
keys.push(key);
}
return keys;
}
}
window.parseQueryString = parseQueryString;
})();