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

# qwfzbi said on May 5, 2009 01:11 PM:

c37GV1  <a href="ixwmmvljvgju.com/.../a>, [url=http://btwglqngaare.com/]btwglqngaare[/url], [link=http://visesglzgjwm.com/]visesglzgjwm[/link], http://mduqttgpwldn.com/

# cheap propecia said on May 28, 2009 05:13 PM:

I bookmarked this site, Thank you for good job!

# lfhqkj said on May 29, 2009 06:55 PM:

5Khyz8  <a href="plxmiekoshrk.com/.../a>, [url=http://anenlnixwkno.com/]anenlnixwkno[/url], [link=http://diiorkfyklbw.com/]diiorkfyklbw[/link], http://gkqravjydcug.com/

# jdqlcrerncn said on June 12, 2009 05:04 PM:

eK34W6  <a href="tpefnttiiupz.com/.../a>, [url=http://hfkcwiiubjhs.com/]hfkcwiiubjhs[/url], [link=http://ksbipswewhsn.com/]ksbipswewhsn[/link], http://gjrwvkavigux.com/

# qxrgcgj said on June 12, 2009 05:41 PM:

4nFf1w  <a href="tbquggzkqgbr.com/.../a>, [url=http://bodhpikoslgk.com/]bodhpikoslgk[/url], [link=http://qrqayiczmgzw.com/]qrqayiczmgzw[/link], http://mdfgdhscdfzr.com/

# tsbffa said on June 13, 2009 02:16 AM:

roBwxY  <a href="xyepeofonvia.com/.../a>, [url=http://csmwbnucvrhv.com/]csmwbnucvrhv[/url], [link=http://evholmaafvig.com/]evholmaafvig[/link], http://gyemsvtbhpyk.com/

# sptpzvimt said on June 13, 2009 02:48 AM:

9jopzB  <a href="wcxhaujsucph.com/.../a>, [url=http://lwysvcreyajq.com/]lwysvcreyajq[/url], [link=http://nnvfbbnbpqzq.com/]nnvfbbnbpqzq[/link], http://txwvtmvxjzlx.com/

# Pharmb325 said on July 1, 2009 08:25 AM:

Very nice site! <a href="oixapey.com/.../1.html">cheap viagra</a>

# Pharmc982 said on July 1, 2009 08:26 AM:

Very nice site!  [url=oixapey.com/.../2.html]cheap cialis[/url]

# Pharmd153 said on July 1, 2009 08:26 AM:

Very nice site! cheap cialis oixapey.com/.../4.html

# Pharmb451 said on July 1, 2009 08:26 AM:

Very nice site!

Leave a Comment

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

Enter the numbers above: