Parse querystring using javascript?

E

Erwin Moller

Owen said:
If my url is, say,
http://mysite/mypage.asp?fruit=apples&veg=cabbages&sweet=cake, then how do
I get the value of "fruit", "veg" and "sweet" params using clientside
javascript?

In ASP I can use Request.Querystring("fruit") etc, but I need to get it
using clientside javascript this time.

Help?

Hi,

document.location return the current url.

Get the part after ?
split it on &

all pieces you have left are of the form:
name=value

Good luck.

Regards,
Erwin Moller
 
M

Mick White

Owen said:
If my url is, say,
http://mysite/mypage.asp?fruit=apples&veg=cabbages&sweet=cake, then how do I
get the value of "fruit", "veg" and "sweet" params using clientside
javascript?

if(location.search){
vals=
unescape((location.search.substring(1).replace(/\+/g," "))).split("&");
for(i=0;i<vals.length;i++){
vals=vals.split("=");
window[vals[0]]=vals[1];
}

In your case, the script creates three global variables:
fruit="apples"
veg="cabbages"
sweet="cake"

Mick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top