Click to See Complete Forum and Search --> : reading url values


webtekie
07-28-2003, 01:03 PM
Hello,

Is there a way to read parameters that I send to my web page using JavaScript? So, if I have test.jsp?firstName=Mark, can I actually read what is firstName thru JavaScript?

thanks,
Alex

requestcode
07-28-2003, 01:30 PM
You can use location.search to grab the values in the URL. This: myval=location.search.substring(1,location.search.length)

Will grab everything past the question mark and place it in the variable myval. If you want to further break it down to separate out the variable name and value that was passed then you can use the split method of string like this:
mysplit=myval.split("=")


The variable mysplit is now an array that contains the the name of the variable and the value passed. mysplit[0] contains "firstName" and mysplit[1] contains "Mark"

webtekie
07-28-2003, 01:31 PM
thanks a lot.

requestcode
07-28-2003, 01:42 PM
Also here is a link to a tutorial that gives a brief explanation.
http://www.javascriptkit.com/javatutors/send1.shtml

I also found this old article, but it is still relevent to this subject.
http://developer.netscape.com/viewsource/goodman_url_pass/goodman_url_pass.html

pyro
07-28-2003, 01:54 PM
This might also help you: http://www.infinitypages.com/research/querystring.htm

Jeff Mott
07-28-2003, 05:17 PM
This might also help you: http://www.infinitypages.com/research/querystring.htmJust a few comments... ;) if (val != "undefined" && val != undefined) {A string composed of letters that just happen to spell the word "undefined" is still defined. It is possible that a form control would be named undefined, but this would prevent them from retrieving that value.window.location.search.substr(1).split(/&/);The semi-colon is also a valid delimiter for name/value pairs.if (key == val) {
query = pairs[1];
break;
}What about form controls such as checkboxes and multiple select menus? How would they retrieve every value that was selected instead of just the first in the list?else {
query = "undefined key";
}Someone could easily type the string "undefined value" into a textbox. How is the user supposed to be able to tell if the value was not in the query string or if it does exist and has the value "undefined string"?

Also, the values returned by your get() function are still URL encoded.

My own implementation is at 162208.

pyro
07-28-2003, 05:27 PM
Thanks for pointing that out. I realize it has some weeknesses, and will try to fix them, when I get the time. :)