getting variable from query string

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

    getting variable from query string

    hello, I've put in my time searching, so I'll ask now<G>

    I need to look at the url, if a variable is present in the query string.

    so, if my address bar looks like:
    https://www.domain.com?THIS=yes&that...otherthing=154

    I need it to look for THIS
    if THIS is present, and THIS= yes
    then MyVar = "my string here"
    if THIS does not = yes
    then MyVar = "not yes string here"
    if THIS is not present
    then MyVar = "not yes string here"


    --

    thanks for your time,
    juglesh B>{)}



  • RobB

    #2
    Re: getting variable from query string


    juglesh wrote:[color=blue]
    > hello, I've put in my time searching, so I'll ask now<G>
    >
    > I need to look at the url, if a variable is present in the query[/color]
    string.[color=blue]
    >
    > so, if my address bar looks like:
    > https://www.domain.com?THIS=yes&that...otherthing=154
    >
    > I need it to look for THIS
    > if THIS is present, and THIS= yes
    > then MyVar = "my string here"
    > if THIS does not = yes
    > then MyVar = "not yes string here"
    > if THIS is not present
    > then MyVar = "not yes string here"
    >
    >
    > --
    >
    > thanks for your time,
    > juglesh B>{)}[/color]

    function getQval(name)
    {
    var m, Q = window.location .search.substri ng(1);
    if ('' != Q)
    {
    var re = new RegExp(escape(n ame) + '=([^&$]+)');
    if (m = Q.match(re))
    return m[1];
    else return '';
    }
    return '';
    }

    var MyVar = (getQval('THIS' ) == 'yes') ?
    'my string here' : 'not yes string here';

    Comment

    • Michael Winter

      #3
      Re: getting variable from query string

      On Tue, 21 Dec 2004 16:09:41 GMT, juglesh <juglesh@nospam RadioKDUG.com>
      wrote:

      [snip]
      [color=blue]
      > https://www.domain.com?THIS=yes&that...otherthing=154
      >
      > I need it to look for THIS
      > if THIS is present, and THIS= yes
      > then MyVar = "my string here"
      > if THIS does not = yes
      > then MyVar = "not yes string here"
      > if THIS is not present
      > then MyVar = "not yes string here"[/color]

      var url = document.URL,
      i = url.indexOf('TH IS='),
      j = url.indexOf('&' , i);
      if(-1 == j) {j = url.length;}
      if((-1 == i) || ('yes' != url.substring(i + 5, j))) {
      /* 'THIS' doesn't exist or is not equal to 'yes' */
      }

      or for something more generic:

      /* Returns the value associated with a key in the query string.
      *
      * If the key doesn't exist or doesn't have a value, undefined
      * is returned.
      */
      function getQueryValue(n ame) {
      var url = document.URL,
      i = url.indexOf(nam e += '='),
      j = url.indexOf('&' , i);
      if(-1 == j) {j = url.length;}
      if(-1 != i) {return url.substring(i + name.length, j);}
      }

      if('yes' != getQueryValue(' THIS')) {
      /* 'THIS' doesn't exist or is not equal to 'yes' */
      }

      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • juglesh

        #4
        Re: getting variable from query string


        "RobB" <ferndoc9@hotma il.com> wrote in message
        news:1103648254 .379190.325870@ c13g2000cwb.goo glegroups.com.. .[color=blue]
        >
        > juglesh wrote:[color=green]
        >> hello, I've put in my time searching, so I'll ask now<G>
        >>
        >> I need to look at the url, if a variable is present in the query[/color]
        > string.[color=green]
        >>
        >> so, if my address bar looks like:
        >> https://www.domain.com?THIS=yes&that...otherthing=154
        >>
        >> I need it to look for THIS
        >> if THIS is present, and THIS= yes
        >> then MyVar = "my string here"
        >> if THIS does not = yes
        >> then MyVar = "not yes string here"
        >> if THIS is not present
        >> then MyVar = "not yes string here"
        >>
        >>
        >> --
        >>
        >> thanks for your time,
        >> juglesh B>{)}[/color]
        >
        > function getQval(name)
        > {
        > var m, Q = window.location .search.substri ng(1);
        > if ('' != Q)
        > {
        > var re = new RegExp(escape(n ame) + '=([^&$]+)');
        > if (m = Q.match(re))
        > return m[1];
        > else return '';
        > }
        > return '';
        > }
        >
        > var MyVar = (getQval('THIS' ) == 'yes') ?
        > 'my string here' : 'not yes string here';[/color]

        thanks, this is working fine.




        Comment

        • juglesh

          #5
          Re: getting variable from query string


          "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
          news:opsjdatha5 x13kvk@atlantis ...[color=blue]
          > On Tue, 21 Dec 2004 16:09:41 GMT, juglesh <juglesh@nospam RadioKDUG.com>
          > wrote:
          >
          > [snip]
          >[color=green]
          >> https://www.domain.com?THIS=yes&that...otherthing=154
          >>
          >> I need it to look for THIS
          >> if THIS is present, and THIS= yes
          >> then MyVar = "my string here"
          >> if THIS does not = yes
          >> then MyVar = "not yes string here"
          >> if THIS is not present
          >> then MyVar = "not yes string here"[/color]
          >
          > var url = document.URL,
          > i = url.indexOf('TH IS='),
          > j = url.indexOf('&' , i);
          > if(-1 == j) {j = url.length;}
          > if((-1 == i) || ('yes' != url.substring(i + 5, j))) {
          > /* 'THIS' doesn't exist or is not equal to 'yes' */
          > }
          >
          > or for something more generic:
          >
          > /* Returns the value associated with a key in the query string.
          > *
          > * If the key doesn't exist or doesn't have a value, undefined
          > * is returned.
          > */
          > function getQueryValue(n ame) {
          > var url = document.URL,
          > i = url.indexOf(nam e += '='),
          > j = url.indexOf('&' , i);
          > if(-1 == j) {j = url.length;}
          > if(-1 != i) {return url.substring(i + name.length, j);}
          > }
          >
          > if('yes' != getQueryValue(' THIS')) {
          > /* 'THIS' doesn't exist or is not equal to 'yes' */
          > }
          >[/color]

          ok, thanks for the reply, i'm using RobB's solution, one reason is i dont
          understand yours. where is MyVar?



          Comment

          • Michael Winter

            #6
            Re: getting variable from query string

            On Tue, 21 Dec 2004 18:47:22 GMT, juglesh <juglesh@nospam RadioKDUG.com>
            wrote:

            [snip]
            [color=blue]
            > [...] i dont understand yours.[/color]

            What's to understand?

            /* Get the address of the document. */
            var url = document.URL,
            /* Find the location of the string, 'THIS=', within the URL.
            *
            * If the sub-string doesn't exist, i will be -1.
            */
            i = url.indexOf('TH IS='),
            /* Find the first ampersand (&) after the sub-string. This
            * will indicate where the value ends.
            */
            j = url.indexOf('&' , i);
            /* If no ampersand was found, assume the name/value pair is the
            * last in the URL. That is, the end of the value is the end of
            * the string.
            */
            if(-1 == j) {j = url.length;}
            if((-1 == i) || ('yes' != url.substring(i + 5, j))) {
            /* 'THIS' doesn't exist or is not equal to 'yes' */
            }

            I think that's simpler than Rob's (no offense Rob :P).
            [color=blue]
            > where is MyVar?[/color]

            I assumed that you would be capable of adding a variable declaration and
            assignment yourself. Was I mistaken?

            Mike

            --
            Michael Winter
            Replace ".invalid" with ".uk" to reply by e-mail.

            Comment

            • Chris Riesbeck

              #7
              Re: getting variable from query string

              In article <opsjdit6cgx13k vk@atlantis>,
              "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote:
              [color=blue]
              > On Tue, 21 Dec 2004 18:47:22 GMT, juglesh <juglesh@nospam RadioKDUG.com>
              > wrote:
              >
              > [snip]
              >[color=green]
              > > [...] i dont understand yours.[/color]
              >
              > What's to understand?
              >
              > /* Get the address of the document. */
              > var url = document.URL,
              > /* Find the location of the string, 'THIS=', within the URL.
              > *
              > * If the sub-string doesn't exist, i will be -1.
              > */
              > i = url.indexOf('TH IS='),
              > /* Find the first ampersand (&) after the sub-string. This
              > * will indicate where the value ends.
              > */
              > j = url.indexOf('&' , i);
              > /* If no ampersand was found, assume the name/value pair is the
              > * last in the URL. That is, the end of the value is the end of
              > * the string.
              > */
              > if(-1 == j) {j = url.length;}
              > if((-1 == i) || ('yes' != url.substring(i + 5, j))) {
              > /* 'THIS' doesn't exist or is not equal to 'yes' */
              > }
              >
              > I think that's simpler than Rob's (no offense Rob :P).[/color]

              I'm with Rob. I'd rather maintain a one-line match over
              two indexOf() calls, a check against length, and a substring.

              I would however change his code slightly, to avoid getting
              the wrong parameter for a URL like

              foo.html?NOTTHI S=yes&THIS=no


              function getQval(name)
              {
              var re = new RegExp('[?&]' + escape(name) + '=([^&$]+)'; // added [?&]
              var m = re.exec(window. location.search ); // no substr
              return m ? m[1] : '';
              }

              Comment

              • Michael Winter

                #8
                Re: getting variable from query string

                On Wed, 22 Dec 2004 13:34:19 -0600, Chris Riesbeck <criesbeck@yaho o.com>
                wrote:

                [snip]
                [color=blue]
                > function getQval(name)
                > {
                > var re = new RegExp('[?&]' + escape(name) + '=([^&$]+)';[/color]

                Shouldn't the dollar ($) be a hash (#)? A dollar has no significance in a
                HTTP URL. Also the escape call shouldn't necessary; you know what you're
                looking for in the URL.

                [snip]

                Mike

                --
                Michael Winter
                Replace ".invalid" with ".uk" to reply by e-mail.

                Comment

                • Chris Riesbeck

                  #9
                  Re: getting variable from query string

                  In article <opsjfdvgmvx13k vk@atlantis>,
                  "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote:
                  [color=blue]
                  > On Wed, 22 Dec 2004 13:34:19 -0600, Chris Riesbeck <criesbeck@yaho o.com>
                  > wrote:
                  >
                  > [snip]
                  >[color=green]
                  > > function getQval(name)
                  > > {
                  > > var re = new RegExp('[?&]' + escape(name) + '=([^&$]+)';[/color]
                  >
                  > Shouldn't the dollar ($) be a hash (#)?[/color]

                  Actually, I meant to delete the $ - it was in Rob's code but it's
                  not relevant here.

                  Why a # though? Is one legal at the point in a URL?
                  [color=blue]
                  > Also the escape call shouldn't necessary; you know what you're
                  > looking for in the URL.[/color]

                  Again, that was from Rob's code -- but isn't that correct? If
                  the parameter was "my param" I'd want to be looking for
                  my%20param, no?

                  Comment

                  • Michael Winter

                    #10
                    Re: getting variable from query string

                    On Thu, 23 Dec 2004 15:53:07 -0600, Chris Riesbeck <criesbeck@yaho o.com>
                    wrote:

                    [snip]
                    [color=blue]
                    > Why a # though? Is one legal at the point in a URL?[/color]

                    The fragment identifier (used to target anchors). If one's present, the
                    last value will be followed immediately by a hash.

                    [MW:][color=blue][color=green]
                    >> Also the escape call shouldn't necessary; you know what you're looking
                    >> for in the URL.[/color]
                    >
                    > Again, that was from Rob's code -- but isn't that correct? If the
                    > parameter was "my param" I'd want to be looking for my%20param, no?[/color]

                    Yes, but you know the space will escaped as %20. Moreover, the escape
                    function works in the context of an entire URL, which means that some
                    characters would be allowed unescaped, such as a forward slash (/). The
                    newer encodeURICompon ent function is better, but it's only supported on
                    newer browsers. Of course, it's trivial to write a substitute:

                    if('function' != typeof encodeURICompon ent) {
                    encodeURICompon ent = function(uri) {
                    var x = /[\w.!~*'()-]/, r = '';
                    for(var c, i = 0, n = uri.length; i < n; ++i) {
                    if(x.test(c = uri.charAt(i))) {r += c;}
                    else {r += '%' + uri.charCodeAt( i).toString(16) .toUpperCase(); }
                    }
                    return r;
                    };
                    }

                    That doesn't completely follow the specification, but that's only a
                    problem if you're escaping Unicode strings.

                    I'm assuming that Number.prototyp e.toString is well supported - it was
                    defined in early versions of JavaScript and JScript so other browsers
                    should have implemented it too.

                    Mike

                    --
                    Michael Winter
                    Replace ".invalid" with ".uk" to reply by e-mail.

                    Comment

                    • Michael Winter

                      #11
                      Re: getting variable from query string

                      On Thu, 23 Dec 2004 22:57:45 GMT, Michael Winter
                      <M.Winter@bluey onder.co.invali d> wrote:
                      [color=blue]
                      > if('function' != typeof encodeURICompon ent) {
                      > encodeURICompon ent = function(uri) {
                      > var x = /[\w.!~*'()-]/, r = '';
                      > for(var c, i = 0, n = uri.length; i < n; ++i) {
                      > if(x.test(c = uri.charAt(i))) {r += c;}
                      > else {
                      > r += '%' + uri.charCodeAt( i).toString(16) .toUpperCase();
                      > }
                      > }
                      > return r;
                      > };
                      > }[/color]

                      That would give incorrect results for characters less than 0x10.

                      if('function' != typeof encodeURICompon ent) {
                      this.encodeURIC omponent = function(uri) {
                      var x = /[\w.!~*'()-]/, r = '';
                      for(var c, i = 0, n = uri.length; i < n; ++i) {
                      if(x.test(c = uri.charAt(i))) {r += c;}
                      else {
                      c = uri.charCodeAt( i).toString(16) .toUpperCase();
                      r += ((c.length == 1) ? '%0' : '%') + c;
                      }
                      }
                      return r;
                      };
                      }

                      Mike

                      --
                      Michael Winter
                      Replace ".invalid" with ".uk" to reply by e-mail.

                      Comment

                      Working...