1: //
2: // ------------------------------------------------------------------------
3: // Modified by Danie Bruwer 2008/08/26
4: // http://dotnet.org.za/danieb
5: // New in Version 1.4
6: // * Chainable
7: // ------------------------------------------------------------------------
8: // Original File :
9: // http://adamv.com/dev/javascript/querystring
10: // Client-side access to querystring name=value pairs
11: // Version 1.3
12: // 28 May 2008
13: // License (Simplified BSD):
14: // http://adamv.com/dev/javascript/qslicense.txt
15:
16: function $Querystring(qs) {
17: return new Querystring(qs);
18: }
19: function Querystring(qs) { // optionally pass a querystring to parse
20: this.params = {};
21: this.keys = new Array();
22: this.path = (qs && qs.indexOf('?') > 0) ? qs.split('?')[0] : location.protocol +location.host + location.pathname;
23:
24: if (qs == null) qs = location.search.substring(1, location.search.length);
25: if (qs.length == 0) return;
26: if (qs.indexOf('?') > 0)
27: qs = qs.split('?')[1];
28:
29: // Turn <plus> back to <space>
30: // See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
31: qs = qs.replace(/\+/g, ' ');
32: var args = qs.split('&'); // parse out name/value pairs separated via &
33:
34: // split out each name=value pair
35: for (var i = 0; i < args.length; i++) {
36: var pair = args
.split('=');
37: var name = decodeURIComponent(pair[0]);
38:
39: var value = (pair.length==2)
40: ? decodeURIComponent(pair[1])
41: : name;
42:
43: this.keys
= name;
44: this.params[name] = value;
45: }
46: }
47:
48: Querystring.prototype.get = function(key, default_) {
49: var value = this.params[key];
50: return (value != null) ? value : default_;
51: }
52: Querystring.prototype.set = function(key, value) {
53: value = encodeURI(value);
54: this.params[key] = value;
55: var index = this.indexOfKey(key);
56: if (index < 0)
57: this.keys[this.keys.length] = key;
58:
59: return this;
60: }
61: Querystring.prototype.indexOfKey = function(key) {
62: for (var i = 0; i < this.keys.length; i++)
63: {
64: if(this.keys
== key)
65: return i;
66: }
67: return -1;
68: }
69: Querystring.prototype.remove = function(key) {
70: var index = this.indexOfKey(key);
71: if (index >= 0) {
72: this.params[key] = null;
73: this.keys.splice(index, 1);
74: }
75: return this;
76: }
77: Querystring.prototype.toString = function() {
78: var ret = "";
79: for (var i = 0; i < this.keys.length; i++)
80: {
81: if(this.keys
)
82: ret += ((i == 0) ? "?" : "&") + this.keys
+ "=" + this.params[this.keys
];
83: }
84: return ret;
85: }
86: Querystring.prototype.getAbsolutePath = function() {
87: return this.path;
88: }
89: Querystring.prototype.toAbsoluteUrl = function() {
90: return this.path + this.toString();
91: }
92: Querystring.prototype.contains = function(key) {
93: var value = this.params[key];
94: return (value != null);
95: }