Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

QueryString Emulation 2

Status
Not open for further replies.

jaredn

Programmer
Sep 1, 1999
1,506
US
In case anyone missed it, you can emulate Request.QueryString functionality (client-side) with this object that I've created:

Code:
//only properly handles querystrings in the following format:
// [URL unfurl="true"]http://thepage.htm?mykey1=value1&mykey2=value2[/URL]
function QSHandler()
{
    var i,j,tmparray,strata,prlen
    strata = window.location.search
    strata = strata.substring(1,strata.length)
    if(strata.indexOf('&')>=0)
    {
        prsarray = strata.split('&')
        prlen = prsarray.length
        tmparray = new Array()
        for(t=j=0;j<prlen;j++)
        {
            tmparray[t++]   = prsarray[j].split('=')[0]
            tmparray[t++]   = prsarray[j].split('=')[1]
        }        
    }
    else
    {
        tmparray = new Array(strata.split('=')[0],strata.split('=')[1])
    }
    tlen = tmparray.length
    this.data = new Array()
    for(i=0;i<tlen;i++)
    {
        this.data[new String(tmparray[i])]=tmparray[++i]
    }
    this.QueryString=function(x)
    {
        return this.data[x]
    }
} 
Request = new QSHandler()
jared@eae.net -
 
a star to you. I recommend you post this as a FAQ with a few examples to engrave in the www! :) Gary Haran
 
I have had a funtion like this for ages in my API:

Code:
[URL unfurl="true"]http://www.mysite.com/page.php?foo=bar[/URL]

qStr.keys = new Array ();
qStr.values = new Array ();

function qsParse () {
  var query = window.location.search.substring (1);
  var pairs = query.split (&quot;&&quot;);
  for (var i = 0; i < pairs.length; i++) {
    var pos = pairs[i].indexOf ('=');
    if (pos >= 0) {
      var argname = pairs[i].substring (0,pos);
      var value = pairs[i].substring (pos+1);
      qStr.keys[qStr.keys.length] = argname;
      qStr.values[qStr.values.length] = sReplace ('+',' ', unescape (value));
    }
  }
}

qsParse ();

function qStr (key) {
  var value = &quot;&quot;;
  for (var i = 0; i < qStr.keys.length; i++) {
    if (qStr.keys[i] == key) {
      value = qStr.values[i];
      break;
    }
  }
  return value;
}

alert (qStr (&quot;foo&quot;));
Regards
David Byng
spider.gif

davidbyng@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top