Referencing name/values in URL string

Discussion in 'Javascript' started by Jim Adamson, Dec 14, 2004.

  1. Jim Adamson

    Jim Adamson Guest

    I have created a web page that receives names and values from a URL
    string of another page e.g.
    http://hostname/resolve?sublibrary=JMLibrary&collection=Elton&shelfmark=LM
    36TY
    ... and decodes the names/values from the ? onwards, doing all the
    seperation of the ampersands.

    Here is the "decoder" that I found at
    http://www.tek-tips.com/faqs.cfm?fid=5442

    ==========================================
    function getValue(varname)
    {
    // First, we load the URL into a variable
    var url = window.location.href;

    // Next, split the url by the ?
    var qparts = url.split("?");

    // Check that there is a querystring, return "" if not
    if (qparts.length == 0)
    {
    return "";
    }

    // Then find the querystring, everything after the ?
    var query = qparts[1];

    // Split the query string into variables (separates by &s)
    var vars = query.split("&");

    // Initialize the value with "" as default
    var value = "";

    // Iterate through vars, checking each one for varname
    for (i=0;i<vars.length;i++)
    {
    // Split the variable by =, which splits name and value
    var parts = vars.split("=");

    // Check if the correct variable
    if (parts[0] == varname)
    {
    // Load value into variable
    value = parts[1];

    // End the loop
    break;
    }
    }

    // Convert escape code
    value = unescape(value);

    // Convert "+"s to " "s
    value.replace(/\+/g," ");

    // Return the value
    return value;
    }

    // end hide -->
    </script>
    ==============================================


    I would like to know how to make name/value pairs available to an "if,
    then, else statement"
    i.e. how i could expand the getValue function so that it would accept
    something like:

    if(collection == 'Elton')

    ....do such and such e.g. open a pop-up window.

    I suppose what I am asking is how do I reference these name/value
    pairs so I can do something with them.

    thanks a lot
    Jim
     
    Jim Adamson, Dec 14, 2004
    #1
    1. Advertising

  2. Jim Adamson

    VK Guest

    > I suppose what I am asking is how do I reference these name/value
    > pairs so I can do something with them.


    Here we go, Virginias, who still think that THERE IS A HASH (associative
    array) in JavaScript, try to help the guy. No keys - no access to values!

    To Mr. Adamson:

    You should use my freshly made PGH (Pretty Good Hash), see the posting named
    "PGH : Pretty Good Hash v0.1"

    Also the key/value processing block could be much shorter:

    var param = new Hash();
    ....
    function getParameters() {
    var tmp = self.location.search.split('&');
    var pair;
    for (i=0;tmp.length;i++) {
    pair = tmp.split('=');
    param.add(unescape(pair[0]), unescape(pair[1]));
    }
    }
     
    VK, Dec 14, 2004
    #2
    1. Advertising

  3. Jim Adamson

    McKirahan Guest

    "Jim Adamson" <> wrote in message
    news:...
    > I have created a web page that receives names and values from a URL
    > string of another page e.g.
    > http://hostname/resolve?sublibrary=JMLibrary&collection=Elton&shelfmark=LM
    > 36TY
    > .. and decodes the names/values from the ? onwards, doing all the
    > seperation of the ampersands.
    >
    > Here is the "decoder" that I found at
    > http://www.tek-tips.com/faqs.cfm?fid=5442
    >
    > ==========================================
    > function getValue(varname)
    > {
    > // First, we load the URL into a variable
    > var url = window.location.href;
    >
    > // Next, split the url by the ?
    > var qparts = url.split("?");
    >
    > // Check that there is a querystring, return "" if not
    > if (qparts.length == 0)
    > {
    > return "";
    > }
    >
    > // Then find the querystring, everything after the ?
    > var query = qparts[1];
    >
    > // Split the query string into variables (separates by &s)
    > var vars = query.split("&");
    >
    > // Initialize the value with "" as default
    > var value = "";
    >
    > // Iterate through vars, checking each one for varname
    > for (i=0;i<vars.length;i++)
    > {
    > // Split the variable by =, which splits name and value
    > var parts = vars.split("=");
    >
    > // Check if the correct variable
    > if (parts[0] == varname)
    > {
    > // Load value into variable
    > value = parts[1];
    >
    > // End the loop
    > break;
    > }
    > }
    >
    > // Convert escape code
    > value = unescape(value);
    >
    > // Convert "+"s to " "s
    > value.replace(/\+/g," ");
    >
    > // Return the value
    > return value;
    > }
    >
    > // end hide -->
    > </script>
    > ==============================================
    >
    >
    > I would like to know how to make name/value pairs available to an "if,
    > then, else statement"
    > i.e. how i could expand the getValue function so that it would accept
    > something like:
    >
    > if(collection == 'Elton')
    >
    > ...do such and such e.g. open a pop-up window.
    >
    > I suppose what I am asking is how do I reference these name/value
    > pairs so I can do something with them.
    >
    > thanks a lot
    > Jim


    Not sure if this will help:

    http://localhost/pairs.htm?ID=123

    <html>
    <head>
    <title>pairs.htm</title>
    <script type="text/javascript">
    var nam = new Array();
    var val = new Array();
    var xqs = location.search;
    xqs = xqs.replace(/\?/g,"&");
    var xnv = xqs.split("&");
    for (var i=1; i<xnv.length; i++) {
    var xxx = xnv.split("=");
    nam[i-1] = xxx[0];
    val[i-1] = xxx[1];
    }
    for (var j=0; j<nam.length; j++) {
    if (nam[j] == "ID") alert("ID = " + val[j]);
    }
    </script>
    </head>
    <body>
    </body>
    </html>
     
    McKirahan, Dec 14, 2004
    #3
  4. Jim Adamson

    Guest

    VK wrote:
    > > I suppose what I am asking is how do I reference these name/value
    > > pairs so I can do something with them.

    >
    > Here we go, Virginias, who still think that THERE IS A HASH

    (associative
    > array) in JavaScript, try to help the guy. No keys - no access to

    values!
    >


    Well, Petunia, it appears to me that you also have "No keys - no access
    to values!" either, until after you've generated them.

    Oh, wait a minute, I see what you're saying -- you do:

    alert( param.getValue( "constructor" ))

    ==> function Object() {
    [native code]
    }

    > To Mr. Adamson:
    >
    > You should use my freshly made PGH (Pretty Good Hash), see the

    posting named
    > "PGH : Pretty Good Hash v0.1"
    >


    Perhaps Mr. Adamson should. But only after he's been provided with
    appropriate quailfication on the use of PGH?

    ... /rh
     
    , Dec 14, 2004
    #4
  5. Jim Adamson

    VK Guest

    > Perhaps Mr. Adamson should. But only after he's been provided with
    > appropriate quailfication on the use of PGH?


    Well, I have a Christmas special this year: free PGH qualification :)

    Can be done without PGH also, as this particular case doesn't involve real
    hash manipulations (sorting, key/value change/delete etc.)

    var keys = new Array();
    var values = new Array();
    ....
    function getParameters() {
    var tmp = self.location.search.split('&');
    var pair;
    for (i=0;tmp.length;i++) {
    pair = tmp.split('=');
    keys.push(unescape(pair[0]));
    values.push(unescape(pair[1]));
    }
    }
    ...
    for (i=0;keys.length;i++) {
    if (keys // meets your criteria) {
    // use values in the way you want
    }
    }
     
    VK, Dec 15, 2004
    #5
  6. Jim Adamson

    Guest

    McKirahan

    Thankyou for your reply to my question. I have substituted the third
    line of the javascript

    var xqs = unescape(location.search);

    Is this the best way to rid the %20's from the URL ?
    I have another question if I may. If the URL is say:

    http://localhost/pairs.htm?ID=123&name=john&street=Brook

    ....how do you adapt this for the if-then-else statement ? i.e. if ID is
    equal to 123 AND name is equal to John AND street is equal to Brook,
    alert ( Hello John ;)

    This doesn't work:

    if (nam[j] == "ID" && val[j]=="123" && nam[j] == "nam" &&
    val[j]=="John" && nam[j] == "street" && val[j]=="Brook")

    One last thing - if I know that the name is always going to be one of
    three text strings (i.e. ID, Name and street),
    would it be worth changing the script so that you're not always
    querying:

    if (nam[j] == "ID"

    but rather if

    ("ID" =="123"

    How could you change this ?

    Many thanks in advance.

    Jim
     
    , Dec 15, 2004
    #6
  7. Jim Adamson

    McKirahan Guest

    <> wrote in message
    news:...
    > McKirahan
    >
    > Thankyou for your reply to my question. I have substituted the third
    > line of the javascript
    >
    > var xqs = unescape(location.search);
    >
    > Is this the best way to rid the %20's from the URL ?
    > I have another question if I may. If the URL is say:
    >
    > http://localhost/pairs.htm?ID=123&name=john&street=Brook
    >
    > ...how do you adapt this for the if-then-else statement ? i.e. if ID is
    > equal to 123 AND name is equal to John AND street is equal to Brook,
    > alert ( Hello John ;)
    >
    > This doesn't work:
    >
    > if (nam[j] == "ID" && val[j]=="123" && nam[j] == "nam" &&
    > val[j]=="John" && nam[j] == "street" && val[j]=="Brook")



    1) Typo? nam[j] == "nam"; shouldn't it be: nam[j] == "name".


    > One last thing - if I know that the name is always going to be one of
    > three text strings (i.e. ID, Name and street),
    > would it be worth changing the script so that you're not always
    > querying:



    2) Will it be "Name" (as above) or "name" (as in the URL)?


    >
    > if (nam[j] == "ID"
    >
    > but rather if
    >
    > ("ID" =="123"
    >
    > How could you change this ?
    >
    > Many thanks in advance.
    >
    > Jim


    Try assigning the value to a variable when it's found.

    var xID = "";
    var xNM = "";
    var xST = "";
    for (var j=0; j<nam.length; j++) {
    var what = nam[j];
    if (what == "ID") {
    xID = val[j];
    } else if (what == "name") {
    xNM = val[j];
    } else if (what == "street") {
    xST = val[j];
    }
    }
    if (xID == "123" && xNM == "John" && xST == "Brook") alert("!");


    Might case-sensitivity be an issue?

    Could the QueryString be:

    http://localhost/pairs.htm?Id=123&Name=john&Street=Brook

    or other variation (as suggested above)? If so, then use this:

    for (var j=0; j<nam.length; j++) {
    var what = nam[j].toLowerCase();
    if (what == "id") {
    xID = val[j];
    } else if (what == "name") {
    xNM = val[j];
    } else if (what == "street") {
    xST = val[j];
    }
    }
     
    McKirahan, Dec 15, 2004
    #7
  8. Jim Adamson

    Guest

    McKirahan

    This has done the trick ! Many thanks for your help.

    Jim
     
    , Dec 16, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon paugh
    Replies:
    1
    Views:
    947
  2. Amelyan
    Replies:
    5
    Views:
    449
    Karl Seguin
    May 3, 2005
  3. anonymous
    Replies:
    1
    Views:
    4,657
    Francisco Padron
    May 8, 2005
  4. Just D.
    Replies:
    0
    Views:
    553
    Just D.
    Aug 11, 2004
  5. nick
    Replies:
    1
    Views:
    513
    David Mark
    Feb 13, 2011
Loading...

Share This Page