Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 29
  1. #1
    Regular Coder
    Join Date
    Sep 2003
    Posts
    290
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Parse Query String

    Last edited by joh6nn; 10-04-2007 at 12:03 AM. Reason: updated url at user's request

  • #2
    Senior Coder
    Join Date
    Mar 2003
    Location
    OHIO
    Posts
    1,438
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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."

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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"

  • #4
    Regular Coder
    Join Date
    Sep 2003
    Posts
    290
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 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.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #6
    Regular Coder
    Join Date
    Mar 2003
    Posts
    241
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #7
    Regular Coder
    Join Date
    Sep 2003
    Posts
    290
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Shouldn't these functions [snip] declared like this? [snip]
    Absolutely they should.
    I don't use JavaScript all that often anymore.
    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.
    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.

  • #8
    Regular Coder
    Join Date
    Sep 2003
    Posts
    290
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #9
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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”

  • #10
    Regular Coder
    Join Date
    Sep 2003
    Posts
    290
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    I acutally use this latest revision these days
    Also buggy.

  • #11
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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”

  • #12
    Regular Coder
    Join Date
    Sep 2003
    Posts
    290
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #13
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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.

  • #14
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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”

  • #15
    Regular Coder
    Join Date
    Sep 2003
    Posts
    290
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •