Click to See Complete Forum and Search --> : Passing Variables


Traffic
03-11-2003, 01:58 PM
hello...

If I put a link together like:

http://whatever.com?name=John&number=2525

How can I retrieve the variables on the resulting page using javascript...?

how can I get the value using javascript of:

name
number

on the resulting page...?


Thank you for your help...

pyro
03-11-2003, 02:10 PM
Try this:

<script language="javascript" type="text/javascript">

querystring = window.location.search;
querystring = querystring.split("&");
part1 = querystring[0];
part1 = part1.split("=")
part2 = querystring[1];
part2 = part2.split("=");
document.write ("Name is "+part1[1]);
document.write ("<br>");
document.write ("Number is "+part2[1]);

</script>

Vladdy
03-11-2003, 02:15 PM
function obj_queryString()
{
qs=document.URL.substring(document.URL.indexOf('?')+1,document.URL.length);
queries=qs.split(/\&/);
for(i=0;i<queries.length;i++)
{ query=queries[i].split(/\=/);
this[query[0]]=unescape(query[1]);
}
}
queryString=new obj_queryString();

khaki
03-11-2003, 02:16 PM
...or...

you can read all about it here:

http://www.htmlgoodies.com/beyond/jspass.html

apparently much lazier than Pyro (and Vladdy)...
k

Vladdy
03-11-2003, 02:30 PM
Form values are passed in a query string anyway. So give me one good reason to create a form when you can just construct a string and attach it to the link. Also, the author of the article referenced needs to learn regular expressions - they can reduce the size of his code exponentially.

pyro
03-11-2003, 02:40 PM
I didn't look at the article, but I'm not sure that RegExp are necessary in this situation. This is due to the fact that all you are doing is spliting the query string twice. Once at the &'s and once at the ='s (or in my case twice at the ='s to make it easier to read) Oh, btw, you can receive the query string easily using window.location.search.

Note: I am not disagreeing with you that the author should learn RegExp, I just don't think that in this situation (the original poster's question) RegExp is needed. With RegExp, you split like this: string.split(/\&/); and without, you just do this string.split("&"); Agree?

khaki
03-11-2003, 02:45 PM
hmmmm....
(Vladdy is being very cranky with me today :( ).

I admit that it has been a while since I actually read that tutorial. I just remember
that I learned something from it, and I've kept the bookmark for it ever since.

But in all fairness....
Traffic already understood the query string part. It was the extract part that he wanted.

I would like to now extract myself from this thread. Sorry for the faulty link.
I was just trying to help.
k

Vladdy
03-11-2003, 03:32 PM
Originally posted by pyro
With RegExp, you split like this: string.split(/\&/); and without, you just do this string.split("&"); Agree?
Agree, it does not matter how you use split method. What I was trying to say was that split is way more efficient than the author's indexOf, lastIndexOf and substr. Also, the code presented in the article would not work on strings that include special characters, as the author does not unescape the value!!!
As far as window.location.search: it would not work in a framed page, while document.URL will.

khaki, I'm not cranky I just think the method presented it the tutorial you posted is about the worst way to accomplish the task at hand.

pyro
03-11-2003, 03:52 PM
Ah, like I said, I didn't read the tutorial, so I didn't know what the author was doing...Sounds like we are on the same side here... lol :D I agree with Vladdy, too. No sence in making extra work for yourself by using <form> tags etc...

khaki
03-11-2003, 03:59 PM
khaki, I'm not cranky I just think the method presented it the tutorial you posted is about the worst way to accomplish the task at hand.Ah, like I said, I didn't read the tutorial, so I didn't know what the author was doing...Sounds like we are on the same side here... lol I agree with Vladdy, too. No sence in making extra work for yourself by using <form> tags etc...Point taken.
I have bannished the link from my bookmarks.
I'll also burn the actual book once it gets dark.
I'll never use the words "HTML" and "Goodies" in the same sentence again.
I'll dis-own all friends with the name of "Joe" (or "Burns").

If you guys can think of anything else, just let me know :rolleyes:

signed:
Miss Information
k