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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query String in Javascript

Status
Not open for further replies.

greyone

Programmer
Dec 14, 2000
200
CA
I want to read 2 values passed as a query string which means the values passed after the ? sign. I'm able to read one as follows

ex:if the url is the followong will alert smily

var varName=location.search.substr(1)
alert(varName)

but dunno how to read 2 or more.

ex:
how do i read happy

Please help
 

is not standard form for a querystring. standard form is:


if you use standard form, you can use this object to get the values:

/only properly handles querystrings in the following format:
// 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)]=tmparray[++i]
}
this.QueryString=function(x)
{
return this.data[x]
}
}
Request = new QSHandler()
//alert(document.cookie)
//document.cookie=&quot;I%%%%Ama}||||cookie!&quot;
</script>
Name:
<script>
//example
document.write(Request.QueryString(&quot;name&quot;))
</script> jared@eae.net -
 
Hi jaredn
Thanks a lot for that quick reply. I just wanted to know if i could find some sort of reference or help in writing similar functions. I mean some sites which will explain how to create objects and pass them to user defined functions.
Thanks once again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top