name/value pair in the current url

J

jadamson60

From http://developer.irt.org/script/992.htm

=============================================

Q992 How can I accept a name/value pair in the current url and access
it as a variable and value?

If the URL is something like:

http://www.irt.org/index.htm?number=5

Then the following will create a JavaScript variable called number,
with the value of 5:

<script language="JavaScript"><!--
if (location.search.length > 0)
eval('var ' + location.search.substring(1));
//--></script>

=============================================

How can I expand this to cope with say
http://www.irt.org/index.htm?number=5&name=john&age=41
putting john into a variable called name and 41 into a variable called
age ?

Also, how can I unescape the URL if it's got spaces ?

thanks a lot

Jim
 
M

McKirahan

=============================================

Q992 How can I accept a name/value pair in the current url and access
it as a variable and value?

If the URL is something like:

http://www.irt.org/index.htm?number=5

Then the following will create a JavaScript variable called number,
with the value of 5:

<script language="JavaScript"><!--
if (location.search.length > 0)
eval('var ' + location.search.substring(1));
//--></script>

=============================================

How can I expand this to cope with say
http://www.irt.org/index.htm?number=5&name=john&age=41
putting john into a variable called name and 41 into a variable called
age ?

Also, how can I unescape the URL if it's got spaces ?

thanks a lot

Jim

Check this thread that started yesterday in comp.lang.javascript:
"Referencing name/values in URL string"
 

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
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top