How about a little info for us that can't read the code or do not understand exactly what this does? I am a "newbie" to js and have not got into it far enough to know what it does.
MNS
__________________
[size=1]"If you want to be "in the biz" you are going to have to roll with the changes or get out, basically."
__________________ Vladdy | KL "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"
1) URL decoding is not performed correctly. Plus signs are not transposed to spaces.
2) The partial URL decoding that is done is performed before the the string is parsed. So if any name or value contained an encoded ampersand, equal sign or percent symbol the results will be skewed.
3) The semi-colon is also a valid name/value pair delimiter, but completely ignored.
4) Multiple values under the same name are lost. This turns checkboxes and multiple select menus into useless form controls.
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
Did you test it?
Shouldn't these functions
Code:
function param(name)
{
return this[name] != undefined ? this[name][0] : undefined;
}
function params(name)
{
if (arguments.length)
return this[name];
else {
var pnames = [];
for (var p in this)
pnames.push(p);
return pnames;
}
}
declared like this?
Code:
ParsedQueryString.prototype.param = function (name)
{
...
}
ParsedQueryString.prototype.params = function (name)
{
...
}
And when I corrected it and ran your commented samples, the results were:
Code:
/* e.g., QUERY-STRING: "hello=world&hello=hi&foo&barn=yard"*/
var qs = new ParsedQueryString();
alert(qs.params('hello')) //alerts world,hi
alert(qs.params('foo')) //alerts an empty string
alert(qs.params('barn')) //alerts yard
alert(qs.params('not there')) //alerts undefined
alert(qs.params()) //alerts hello,foo,barn,param,params in NS7 and param,hello,foo,barn,params in IE5.5
IMO, it's better to use null instead of undefined if the parameter is not found.
I agree with Jeff's decision to use undefined if a querystring-value has not been defined, because if it is not defined, it makes sense to return undefined
I don't know but the rest of you, but in glenn's example, I would prefer this line to return null instead of an empty string:
Code:
/* e.g., QUERY-STRING: "hello=world&hello=hi&foo&barn=yard"*/
var qs = new ParsedQueryString();
alert(qs.params('foo')) //alerts an empty string
It's a nice script indeed, but it could use a few corrections if glenn's testresults are valid, which I'm sure they are.
Shouldn't these functions [snip] declared like this? [snip]
Absolutely they should.
I don't use JavaScript all that often anymore.
Quote:
And when I corrected it and ran your commented samples, the results were:
alert(qs.params()) //alerts hello,foo,barn,param,params in NS7 and param,hello,foo,barn,params in IE5.5
You didn't quite follow through all the way with the corrections.
Quote:
IMO, it's better to use null instead of undefined if the parameter is not found
Since param returns string literals, it should continue to return undefined. params, however, returns an array object, so for that method you are correct.
Last edited by Jeff Mott; 02-17-2004 at 04:22 AM..
Multiple values are not retained. This means that if more than one item was checked/selected in a set of checkboxes or a multiple select menu then that input is lost.
Also, if you were to submit of value of e.g. "var four = 2 + 2", the value that the script would return is "var four = 2 2".
While the latter can be fixed relatively easily, the former requires a different approach to how the data is stored and how the API should return that data to the user.
[Edit] And just noticed another. The name of the name/value pair is not URL decoded at all.
The constant usage of typeof to check if a value is undefined is redundant and only adds extra operations that do not need to be there. Although this last detail will not affect the function's ultimate result.
Last edited by Jeff Mott; 09-10-2003 at 12:28 AM..
Originally posted by Jeff Mott The constant usage of typeof to check if a value is undefined is redundant and only adds extra operations that do not need to be there.
Actually, for "correct" Javascript, converting non-existant properties into Boolean()'s is a no-no.
if (myobject.notAProp) {}
may run, but it will throw a strict warning in Mozilla and run slightly slower as a result.
Originally posted by Jeff Mott
Multiple values are not retained (...)
Fixed. Thank you
Also, if you were to submit of value of e.g. "var four = 2 + 2", the value that the script would return is "var four = 2 2".
Fixed. Thank you
(...) requires a different approach to how the data is stored and how the API should return that data to the user.
I didn't have to change this part. Am I missing something?
[Edit] And just noticed another. The name of the name/value pair is not URL decoded at all.
I was under the impression there should never be any URL-encoded characters in the name anyways. I must be mistaken?
The constant usage of typeof to check if a value is undefined is redundant and only adds extra operations that do not need to be there. Although this last detail will not affect the function's ultimate result.
What jkd said
Submit any GET method form to this url, or just append a query string to it.
converting non-existant properties into Boolean()'s is a no-no
Fortunately that's not what I said. I was referring to the fact that (typeof x == 'undefined') is more properly, and more condensly written (x == undefined)