Javascript Querystring Class Version 1.4 - Danie Bruwer

Javascript Querystring Class Version 1.4

Published 26 August 08 10:58 AM | danieb

The updated version of the original JavaScript Querystring class.

   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 = argsIdea.split('=');
  37:         var name = decodeURIComponent(pair[0]);
  38:         
  39:         var value = (pair.length==2)
  40:             ? decodeURIComponent(pair[1])
  41:             : name;
  42:             
  43:         this.keysIdea = 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.keysIdea == 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.keysIdea)
  82:             ret += ((i == 0) ? "?" : "&") + this.keysIdea + "=" + this.params[this.keysIdea];
  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: }

New in Version 1.4. Chainable :

 

   1: alert($Querystring().set("search", "changed").toString());
   2: alert($Querystring().set("search", "changed").set("page", "1").indexOfKey("page"));
   3: alert($Querystring().set("search", "changed").set("page", "1").set("search", "changed again").toAbsoluteUrl());
   4: alert($Querystring("http://www.test.com?search=searchtext&page=1").set("search", "changed").set("page", "2").toAbsoluteUrl());

 

 

 

Download the source.

Comments

# gOODiDEA.NET said on August 29, 2008 02:46 PM:

.NET Garbage Collection Notifications in .NET 3.5 SP1 Testplan Checklist for Asp.Net .NET Framework 3

# wldpxkthko said on November 16, 2008 03:11 PM:

zwzLSB  <a href="yladvleivzkx.com/.../a>, [url=http://wjapzmlnqvot.com/]wjapzmlnqvot[/url], [link=http://lribswghviet.com/]lribswghviet[/link], http://cqbkogesxdqe.com/

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: