Referencing name/values in URL string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Adamson

    Referencing name/values in URL string

    I have created a web page that receives names and values from a URL
    string of another page e.g.

    36TY
    ... and decodes the names/values from the ? onwards, doing all the
    seperation of the ampersands.

    Here is the "decoder" that I found at


    =============== =============== ============
    function getValue(varnam e)
    {
    // 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.len gth;i++)
    {
    // Split the variable by =, which splits name and value
    var parts = vars[i].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
  • VK

    #2
    Re: Referencing name/values in URL string

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

    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.s earch.split('&' );
    var pair;
    for (i=0;tmp.length ;i++) {
    pair = tmp[i].split('=');
    param.add(unesc ape(pair[0]), unescape(pair[1]));
    }
    }


    Comment

    • McKirahan

      #3
      Re: Referencing name/values in URL string

      "Jim Adamson" <jadamson60@go. com> wrote in message
      news:c6d577e8.0 412140604.21bd6 b07@posting.goo gle.com...[color=blue]
      > I have created a web page that receives names and values from a URL
      > string of another page e.g.
      > http://hostname/resolve?sublibrary=J...n&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(varnam e)
      > {
      > // 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.len gth;i++)
      > {
      > // Split the variable by =, which splits name and value
      > var parts = vars[i].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[/color]

      Not sure if this will help:



      <html>
      <head>
      <title>pairs.ht m</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[i].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>


      Comment

      • codeHanger@yahoo.ca

        #4
        Re: Referencing name/values in URL string

        VK wrote:[color=blue][color=green]
        > > I suppose what I am asking is how do I reference these name/value
        > > pairs so I can do something with them.[/color]
        >
        > Here we go, Virginias, who still think that THERE IS A HASH[/color]
        (associative[color=blue]
        > array) in JavaScript, try to help the guy. No keys - no access to[/color]
        values![color=blue]
        >[/color]

        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( "constructo r" ))

        ==> function Object() {
        [native code]
        }
        [color=blue]
        > To Mr. Adamson:
        >
        > You should use my freshly made PGH (Pretty Good Hash), see the[/color]
        posting named[color=blue]
        > "PGH : Pretty Good Hash v0.1"
        >[/color]

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

        ... /rh

        Comment

        • VK

          #5
          Re: Referencing name/values in URL string

          > Perhaps Mr. Adamson should. But only after he's been provided with[color=blue]
          > appropriate quailfication on the use of PGH?[/color]

          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.s earch.split('&' );
          var pair;
          for (i=0;tmp.length ;i++) {
          pair = tmp[i].split('=');
          keys.push(unesc ape(pair[0]));
          values.push(une scape(pair[1]));
          }
          }
          ...
          for (i=0;keys.lengt h;i++) {
          if (keys[i] // meets your criteria) {
          // use values[i] in the way you want
          }
          }


          Comment

          • jadamson60@go.com

            #6
            Re: Referencing name/values in URL string

            McKirahan

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

            var xqs = unescape(locati on.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:



            ....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

            Comment

            • McKirahan

              #7
              Re: Referencing name/values in URL string

              <jadamson60@go. com> wrote in message
              news:1103120210 .965041.175850@ z14g2000cwz.goo glegroups.com.. .[color=blue]
              > McKirahan
              >
              > Thankyou for your reply to my question. I have substituted the third
              > line of the javascript
              >
              > var xqs = unescape(locati on.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&na...n&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")[/color]


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

              [color=blue]
              > 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:[/color]


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

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

              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:



              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];
              }
              }


              Comment

              • jadamson60@go.com

                #8
                Re: Referencing name/values in URL string

                McKirahan

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

                Jim

                Comment

                Working...