User:Matthew Yeager/include/querystring.js

function QueryString(qString){ this.string = qString; this.params = {}; if(qString.length == 0){return;}

qString.replace(/\+/, ' '); var args = qString.split('&'); for(var i = 0;i < args.length; ++i){ var pair = args[i].split( '=' ); var key = decodeURIComponent( pair[0] ), value = key; if(pair.length == 2){value = decodeURIComponent(pair[1]);} this.params[key] = value; } } QueryString.static = null; QueryString.staticInit = function{ if(QueryString.static == null){ QueryString.static = new QueryString(location.search.substring(1)); } } QueryString.get = function(key){ QueryString.staticInit; return QueryString.static.get(key); }; QueryString.prototype.get = function(key){ return this.params[key] ? this.params[key] : null; }; QueryString.exists = function(key) { QueryString.staticInit; return QueryString.static.exists(key); } QueryString.prototype.exists = function(key) { return this.params[key] ? true : false; } QueryString.equals = function(key, value) { QueryString.staticInit; return QueryString.static.equals(key, value); } QueryString.prototype.equals = function(key, value) { return this.params[key] == value ? true : false; } QueryString.toString = function { QueryString.staticInit; return QueryString.static.toString; } QueryString.prototype.toString = function { return this.string ? this.string : null; } QueryString.create = function(arr){ var resarr = Array; for(var i in arr){ if(typeof arr[i] == 'undefined'){continue;} if(arr[i] instanceof Array){ var v = Array; for(var j = 0; j < arr[i].length; ++j){ v[j] = encodeURIComponent(arr[i][j]); }     resarr.push(encodeURIComponent(i) + '=' +  v.join('|')); }else{ resarr.push(encodeURIComponent(i) + '=' + encodeURIComponent(arr[i])); } }  return resarr.join('&'); }

QueryString.prototype.create = QueryString.create;