Click to See Complete Forum and Search --> : how do i read a query string?
slyfox
11-17-2003, 10:28 AM
Hi
Trying to read the value after the "?" sign..
eg. ......com/index.htm?123
i need to use that "123"... can someone please help me here:confused:
pixelmech
11-17-2003, 10:47 AM
Originally posted by slyfox
Hi
Trying to read the value after the "?" sign..
eg. ......com/index.htm?123
i need to use that "123"... can someone please help me here:confused:
http://evolt.jeffhowden.com/jeff/code/js_url_variables/index.cfm?foo=bar&far=boo&cfid=1235908&cftoken=14598715
HTH
Tom
Jeff Mott
11-17-2003, 12:25 PM
Unfortunately that code has several bugs associated with it. I'd recommend 162208 instead.
pixelmech
11-17-2003, 12:40 PM
Originally posted by Jeff Mott
Unfortunately that code has several bugs associated with it. I'd recommend us this (attached) instead.
Really? What bugs? That code has worked for many people in the past. Can you please elaborate?
Tom
Jeff Mott
11-17-2003, 01:34 PM
Try submitting this example form then let me know if you still think it works correctly.<form action="" method="get">
<input name="text" value="Hello World!">
<label><input type="checkbox" name="selected" value="A" checked>A</label>
<label><input type="checkbox" name="selected" value="B" checked>B</label>
<label><input type="checkbox" name="selected" value="C" checked>C</label>
<input type="submit">
</form>In addition, the W3C recommends ";" be supported in place of "&" (http://www.w3.org/TR/html401/appendix/notes.html#h-B.2.2), which the other script does not do.
pixelmech
11-17-2003, 01:58 PM
Well, I assumed a tad bit of work on the user end here.
#1 - Your form has the same name for all three checkboxes. Therefore only one value would ever be returned, which I fixed.
#2 - How can you expect any values if you don't ask the URL object what they are?
#3 - As for the W3C recommendation, that's all fine and dandy but just because the W3C recommends it doesn't mean you should do it. & will work fine 10 years from now. It could be url encoded, but that's a different issue. If you want to do it, go nuts. It doesn't make the code wrong or right in either case.
The below works just fine:
<html>
<head>
<script>
var qs = location.search.substring(1);
var nv = qs.split('&');
var url = new Object();
for(i = 0; i < nv.length; i++)
{
eq = nv[i].indexOf('=');
url[nv[i].substring(0,eq).toLowerCase()] = unescape(nv[i].substring(eq + 1));
}
function foo()
{
// note watch the line breaks here - for readability in forum
alert("Selected= " + url['selected'] + ";
Selected2= " + url['selected2'] + ";
Selected3= " + url['selected3'] + ";
text1= " + url['text1']);
}
</script>
</head>
<body>
<form action="test.html" method="get">
<input name="text1" value="">
<label><input type="checkbox" name="selected" value="A">A</label>
<label><input type="checkbox" name="selected2" value="B">B</label>
<label><input type="checkbox" name="selected3" value="C">C</label>
<input type="submit">
</form>
<input type="button" onclick="foo()" value="test" />
</body>
</html>
Also, this isn't meant for production code. This is meant for testing. I would never implement production code using JS for GET variables. Should the user have JS turned off, your application would be completely useless. Server side code should be used for any querystring manipulation.
Both scripts serve the purpose, and they both work. Not sure of the capabilities of the person who asked the question, but either should be fine for them. In fact, if the person is new to JS and/or programming, the regular expressions would surely be intimidating to say the least.
Cheers
Tom
Jeff Mott
11-17-2003, 02:26 PM
#1 - Your form has the same name for all three checkboxes...which I fixed.There was nothing to fix. They were supposed to have the same name, and is perfectly legal in HTML. It has the same effect as using a multiple select menu. Every browser I've come across has correctly placed multiple values for a given control name into the query string. The parser you referred to simply fails to store and return those values.#2 - How can you expect any values if you don't ask the URL object what they are?Ask the URL object what they are? I don't understand what you mean by that.As for the W3C recommendation, that's all fine and dandy but just because the W3C recommends it doesn't mean you should do it.True that the semi-colon was only a recommendation, so you could view that as merely an absent feature rather than a bug. It is still a good idea to support it none-the-less.It could be url encoded, but that's a different issue. If you want to do it, go nuts. It doesn't make the code wrong or right in either case.Generally, you use functions so that the small details associated with each small job are organized and structured. URL decoding should really be performed within the parser. In this particular case, however, it is most certainly a bug. Since the author attempted to decode the string. Ultimately what is returned to the user is a string that is partially URL decoded.The below works just fineOnly because you have avoided the situations that are buggy.Also, this isn't meant for production code. This is meant for testingWhatever it is meant for, the original poster now has equal access to two implementations and is able to choose the one they think works best.Both scripts serve the purpose, and they both workHow do you know if the original poster will be using checkboxes or multiple select menus, or not? How do you konw if the original poster expects the returned value to be decoded or not? You don't. You cannot guarentee that the script you referred to will in fact serve its purpose.if the person is new to JS and/or programming, the regular expressions would surely be intimidating to say the leastMy goal was to develop the best solution for the job when writing the code, it was not to develop the most newbie friendly code.
pixelmech
11-17-2003, 02:48 PM
Well, like I said its not my code so I'm not really here to defend it. I think it works fine for testing purposes, which was my interpretation of what the poster needed. If your implementation is better, so be it.
I'll politely disagree with you on the checkboxes. If you want a multiple select, use one. Just my opinion.
Tom
Jeff Mott
11-17-2003, 04:06 PM
I'll politely disagree with you on the checkboxesWhether or not you prefer to use the same name or not, it is a fact that you can do so, and that others do so. And the parser should not break under those circumstances.If you want a multiple select, use oneI do. e.g.,<select name="selected" multiple>
<option>A
<option>B
<option>C
</select>Selecting all three options will produce selected=A&selected=B&selected=C, which is exactly the same as what the three checkboxes with the same name produce.