Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 2.50 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-04-2003, 08:15 PM   PM User | #1
Jeff Mott
Regular Coder

 
Join Date: Sep 2003
Posts: 290
Thanks: 0
Thanked 0 Times in 0 Posts
Jeff Mott is an unknown quantity at this point
Parse Query String

http://www.webdeveloper.com/forum/sh...d.php?t=162208

Last edited by joh6nn; 10-04-2007 at 12:03 AM.. Reason: updated url at user's request
Jeff Mott is offline   Reply With Quote
Old 09-04-2003, 08:31 PM   PM User | #2
MotherNatrsSon
Senior Coder

 
Join Date: Mar 2003
Location: OHIO
Posts: 1,438
Thanks: 1
Thanked 0 Times in 0 Posts
MotherNatrsSon is an unknown quantity at this point
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."
MotherNatrsSon is offline   Reply With Quote
Old 09-04-2003, 08:33 PM   PM User | #3
Vladdy
Senior Coder

 
Join Date: Jun 2002
Location: Nashua, NH
Posts: 1,724
Thanks: 0
Thanked 0 Times in 0 Posts
Vladdy is an unknown quantity at this point
Been there, done that:
http://www.codingforums.com/showthre...&threadid=4555
__________________
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"
Vladdy is offline   Reply With Quote
Old 09-04-2003, 10:49 PM   PM User | #4
Jeff Mott
Regular Coder

 
Join Date: Sep 2003
Posts: 290
Thanks: 0
Thanked 0 Times in 0 Posts
Jeff Mott is an unknown quantity at this point
Quote:
Been there, done that
...and done incorrectly.

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.
Jeff Mott is offline   Reply With Quote
Old 09-09-2003, 06:15 AM   PM User | #5
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
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.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 09-09-2003, 07:30 AM   PM User | #6
Caffeine
Regular Coder

 
Join Date: Mar 2003
Posts: 241
Thanks: 0
Thanked 0 Times in 0 Posts
Caffeine is an unknown quantity at this point
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.
Caffeine is offline   Reply With Quote
Old 09-09-2003, 07:39 AM   PM User | #7
Jeff Mott
Regular Coder

 
Join Date: Sep 2003
Posts: 290
Thanks: 0
Thanked 0 Times in 0 Posts
Jeff Mott is an unknown quantity at this point
Quote:
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..
Jeff Mott is offline   Reply With Quote
Old 09-09-2003, 07:42 AM   PM User | #8
Jeff Mott
Regular Coder

 
Join Date: Sep 2003
Posts: 290
Thanks: 0
Thanked 0 Times in 0 Posts
Jeff Mott is an unknown quantity at this point
Quote:
I would prefer this line to return null instead of an empty string:
alert(qs.params('foo')) //alerts an empty string
It only appeared to be an empty string. In fact what was alerted was [].

Last edited by Jeff Mott; 02-17-2004 at 04:23 AM..
Jeff Mott is offline   Reply With Quote
Old 09-09-2003, 08:56 PM   PM User | #9
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
Quote:
Originally posted by Jeff Mott
...and done incorrectly.
You obviously didn't read to the end of the thread, as some of those issues were fixed as time progressed.

Nice bit of code, just the same

I acutally use this latest revision these days.
__________________
My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
“Minds are like parachutes. They don't work unless they are open”
“Maturity is simply knowing when to not be immature”
beetle is offline   Reply With Quote
Old 09-09-2003, 11:26 PM   PM User | #10
Jeff Mott
Regular Coder

 
Join Date: Sep 2003
Posts: 290
Thanks: 0
Thanked 0 Times in 0 Posts
Jeff Mott is an unknown quantity at this point
Quote:
You obviously didn't read to the end of the thread
I did. Some bugs were fixed. New ones were introduced. And ultimately there was not a single algorithm in that thread which was not buggy.
Quote:
I acutally use this latest revision these days
Also buggy.
Jeff Mott is offline   Reply With Quote
Old 09-09-2003, 11:48 PM   PM User | #11
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
Then please enlighten me with your infinite wisdom.

I can handle being ignorant, but I don't particularly care for others being pretentious.
__________________
My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
“Minds are like parachutes. They don't work unless they are open”
“Maturity is simply knowing when to not be immature”
beetle is offline   Reply With Quote
Old 09-10-2003, 12:13 AM   PM User | #12
Jeff Mott
Regular Coder

 
Join Date: Sep 2003
Posts: 290
Thanks: 0
Thanked 0 Times in 0 Posts
Jeff Mott is an unknown quantity at this point
Quote:
this latest revision
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..
Jeff Mott is offline   Reply With Quote
Old 09-10-2003, 01:14 AM   PM User | #13
jkd
Senior Coder

 
jkd's Avatar
 
Join Date: May 2002
Location: metro DC
Posts: 3,163
Thanks: 1
Thanked 18 Times in 18 Posts
jkd will become famous soon enough
Quote:
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.
__________________
jasonkarldavis.com
jkd is offline   Reply With Quote
Old 09-10-2003, 01:22 AM   PM User | #14
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
Quote:
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.

http://www.peterbailey.net/dhtml/form_preview.htm
__________________
My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
“Minds are like parachutes. They don't work unless they are open”
“Maturity is simply knowing when to not be immature”
beetle is offline   Reply With Quote
Old 09-10-2003, 01:25 AM   PM User | #15
Jeff Mott
Regular Coder

 
Join Date: Sep 2003
Posts: 290
Thanks: 0
Thanked 0 Times in 0 Posts
Jeff Mott is an unknown quantity at this point
Quote:
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)
Jeff Mott is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:09 PM.


Advertisement
Log in to turn off these ads.