url parsing

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

    #1

    url parsing

    I use the following to parse the url

    var srch = window.location .search.substri ng(1);
    // then split srch at the ampersand:
    var parts = srch.split("&") ;
    // write the parameters into the variables
    for(var i in parts) {
    var temp = parts[i].split("=");
    if (temp[0] == "xx") { xx = 1 * temp[1]; }
    if (temp[0] == "yy") { yy = 1 * temp[1]; }
    if (temp[0] == "ff") { ff = 1 * temp[1]; }
    if (temp[0] == "level") { level = 1 * temp[1]; }
    }

    However, when I parse the url, it consistently chokes when presented
    with an ampersand character. Do I need to escape this character somehow?


    --
    --
    Fabian
    Visit my website often and for long periods!
    Penasaran slot depo 10k di AGAM69? Simak pengalaman main Mahjong Ways dan buy spin modal receh yang berakhir manis. Daftar dan buktikan sendiri.


  • Fabian

    #2
    Re: url parsing

    Fabian hu kiteb:
    [color=blue]
    > I use the following to parse the url
    >
    > var srch = window.location .search.substri ng(1);
    > // then split srch at the ampersand:
    > var parts = srch.split("&") ;[/color]

    This line is definately the big isue. When I use "a" instead of "&", it
    parses multiple substrings correctly.

    var parts = srch.split("a") ;

    Unfortunately, this isn't the standard approach to including data in an
    url, so while it works, it is sub-optimal.



    --
    --
    Fabian
    Visit my website often and for long periods!
    Penasaran slot depo 10k di AGAM69? Simak pengalaman main Mahjong Ways dan buy spin modal receh yang berakhir manis. Daftar dan buktikan sendiri.


    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: url parsing

      "Fabian" <lajzar@hotmail .com> writes:
      [color=blue]
      > I use the following to parse the url
      >
      > var srch = window.location .search.substri ng(1);
      > // then split srch at the ampersand:
      > var parts = srch.split("&") ;[/color]
      ....[color=blue]
      > However, when I parse the url, it consistently chokes when presented
      > with an ampersand character. Do I need to escape this character somehow?[/color]

      That depends.

      If the code is embedded in an HTML page, then the ampersand should be
      escaped (as an HTML entity, "&amp;", not a Javascript string escape
      "\&"). If it is in an external javascript file, then it should not.

      So, probably. Have you tried escaping it?

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Fabian

        #4
        Re: url parsing

        Lasse Reichstein Nielsen hu kiteb:
        [color=blue][color=green]
        >> However, when I parse the url, it consistently chokes when presented
        >> with an ampersand character. Do I need to escape this character
        >> somehow?[/color]
        >
        > That depends.
        >
        > If the code is embedded in an HTML page, then the ampersand should be
        > escaped (as an HTML entity, "&amp;", not a Javascript string escape
        > "\&"). If it is in an external javascript file, then it should not.
        >
        > So, probably. Have you tried escaping it?[/color]

        It is in an external javascript file. I have tried escaping it, and none
        of the escape sequences I know seem to make much different. I've tried
        %26, , &amp;, and \u0026. I'm not aware of other methods for
        escaping the character.


        --
        --
        Fabian
        Visit my website often and for long periods!
        Penasaran slot depo 10k di AGAM69? Simak pengalaman main Mahjong Ways dan buy spin modal receh yang berakhir manis. Daftar dan buktikan sendiri.



        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: url parsing

          Fabian wrote:
          [color=blue]
          > var srch = window.location .search.substri ng(1);
          > // then split srch at the ampersand:
          > var parts = srch.split("&") ;
          > // write the parameters into the variables
          > for(var i in parts) {[/color]

          Not so. If successful, String.split(.. .) returns an Array object.
          Iterate only over the array's elements, not over all the object's
          enumerable properties:

          for (var i = 0; i < parts.length; i++)
          {
          // ...
          }
          [color=blue]
          > var temp = parts[i].split("=");
          > if (temp[0] == "xx") { xx = 1 * temp[1]; }[/color]

          xx = +temp[1];

          is faster (should be available from Netscape 4.06 on.)
          I do hope `xx' aso. are not global variables used in a
          local execution context.
          [color=blue]
          > if (temp[0] == "yy") { yy = 1 * temp[1]; }
          > if (temp[0] == "ff") { ff = 1 * temp[1]; }
          > if (temp[0] == "level") { level = 1 * temp[1]; }[/color]

          Make use of either switch...case

          switch (temp[0])
          {
          case "yy": yy = +temp[1]; break;
          case "ff": ff = +temp[1]; break;
          case "level": level = +temp[1]; break;
          }

          or a container object (here: window):

          window[temp[0]] = temp[1];
          [color=blue]
          > }
          >
          > However, when I parse the url, it consistently chokes when presented
          > with an ampersand character. Do I need to escape this character somehow?[/color]

          Yes, RFC 2396 states that `&' is a special character and must be
          escaped in URIs if not used with that special meaning. Since its
          decimal ASCII code is 38, it can be escaped in URIs with the
          hexadecimal representation of `%26'.

          You may find http://pointedears.de.vu/scripts/search.htm useful.
          (Please pay attention to the license agreement.)


          Merry Christmas! (if you celebrate it)

          PointedEars

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: url parsing

            Fabian wrote:[color=blue]
            > It is in an external javascript file. I have tried escaping it, and none
            > of the escape sequences I know seem to make much different. I've tried
            > %26, , &amp;, and \u0026. I'm not aware of other methods for
            > escaping the character.[/color]

            `%26' and `\u0026' represent characters different from `'. While
            the former are hexadecimal representations of the code of the character,
            the latter is a decimal one. Only `&#x26;' (case-insensitive) and
            `&#x0026;' would represent the same as the former.

            http://www.w3.org/TR/html4/charset.html#h-5.3.1


            PointedEars

            Comment

            • Fabian

              #7
              Re: url parsing

              Thomas 'PointedEars' Lahn hu kiteb:
              [color=blue][color=green]
              >> if (temp[0] == "xx") { xx = 1 * temp[1]; }[/color]
              >
              > xx = +temp[1];[/color]

              I made this change about 5 minutes after posting when I saw a similar
              response to making javascript recognise strings as numbers.
              [color=blue]
              > Make use of either switch...case
              >
              > switch (temp[0])
              > {
              > case "yy": yy = +temp[1]; break;
              > case "ff": ff = +temp[1]; break;
              > case "level": level = +temp[1]; break;
              > }[/color]

              Now using this code. Thanks.
              [color=blue][color=green]
              >> However, when I parse the url, it consistently chokes when presented
              >> with an ampersand character. Do I need to escape this character
              >> somehow?[/color]
              >
              > Yes, RFC 2396 states that `&' is a special character and must be
              > escaped in URIs if not used with that special meaning. Since its
              > decimal ASCII code is 38, it can be escaped in URIs with the
              > hexadecimal representation of `%26'.[/color]

              I tried using srch.split("%26 ") but that would not split on an actual
              question mark, only the literal string "%26". It finally turns out that
              what I need is to split on "\?".

              var srch = window.location .search.substri ng(1);
              var parts = srch.split("\?" );
              for (var i = 0; i < parts.length; i++) {
              var temp = parts[i].split("=");
              switch (temp[0]) {
              case "xx": xx = +temp[1]; break;
              case "yy": yy = +temp[1]; break;
              case "ff": ff = +temp[1]; break;
              case "ll": level = +temp[1]; break;
              }
              }

              Does this look good?


              --
              --
              Fabian
              Visit my website often and for long periods!
              Penasaran slot depo 10k di AGAM69? Simak pengalaman main Mahjong Ways dan buy spin modal receh yang berakhir manis. Daftar dan buktikan sendiri.



              Comment

              • Lasse Reichstein Nielsen

                #8
                Re: url parsing

                Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
                [color=blue]
                > Not so. If successful, String.split(.. .) returns an Array object.
                > Iterate only over the array's elements, not over all the object's
                > enumerable properties:[/color]

                The only enumerable properties of an Array instance are the elements
                of the array by default. All methods of Array.prototype and the length
                property are non-enumerable.
                [color=blue]
                > for (var i = 0; i < parts.length; i++)[/color]

                So unless you have extended Array.prototype or Object.prototyp e, this is
                equivalent to
                for (var i in parts)

                /L
                --
                Lasse Reichstein Nielsen - lrn@hotpop.com
                DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                'Faith without judgement merely degrades the spirit divine.'

                Comment

                • Thomas 'PointedEars' Lahn

                  #9
                  Re: url parsing

                  Lasse Reichstein Nielsen wrote:
                  [color=blue]
                  > Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:[color=green]
                  >> Not so. If successful, String.split(.. .) returns an Array object.
                  >> Iterate only over the array's elements, not over all the object's
                  >> enumerable properties:[/color]
                  >
                  > The only enumerable properties of an Array instance are the elements
                  > of the array by default. All methods of Array.prototype and the length
                  > property are non-enumerable.[/color]

                  Maybe, but I prefer it this way since it is independent
                  of flawed implementations and extensions to the prototype.
                  [color=blue][color=green]
                  >> for (var i = 0; i < parts.length; i++)[/color]
                  >
                  > So unless you have extended Array.prototype or Object.prototyp e, this is
                  > equivalent to
                  > for (var i in parts)[/color]

                  It is not since the order of the retrieved properties is
                  then undefined.


                  PointedEars

                  Comment

                  • Thomas 'PointedEars' Lahn

                    #10
                    Re: url parsing

                    Fabian wrote:
                    [color=blue]
                    > Thomas 'PointedEars' Lahn hu kiteb:[color=green][color=darkred]
                    >>> However, when I parse the url, it consistently chokes when presented
                    >>> with an ampersand character. Do I need to escape this character
                    >>> somehow?[/color]
                    >>
                    >> Yes, RFC 2396 states that `&' is a special character and must be
                    >> escaped in URIs if not used with that special meaning. Since its
                    >> decimal ASCII code is 38, it can be escaped in URIs with the
                    >> hexadecimal representation of `%26'.[/color]
                    >
                    > I tried using srch.split("%26 ") but that would not split on an actual
                    > question mark, only the literal string "%26".[/color]

                    Of course. You need to understand the difference between special
                    characters used as special characters, and special characters used
                    as ordinary characters within URI components. The latter needs to
                    be escaped, the former should not be escaped.
                    [color=blue]
                    > It finally turns out that what I need is to split on "\?".[/color]

                    "\?" is semantically equal to "?" since there is no such escape sequence
                    in _string_ literals. And no, you should not need to split on `?'. If
                    you need a *literal* `?' character, you need to escape it then in the URI.
                    [color=blue]
                    > var srch = window.location .search.substri ng(1);[/color]

                    Location is a host object and in some implementations the question
                    mark character is not part of location.search , so you need to check
                    for it before processing. And for compatibility reasons you should
                    use location.search , not window.location .search.
                    [color=blue]
                    > var parts = srch.split("\?" );[/color]

                    See above.
                    [color=blue]
                    > for (var i = 0; i < parts.length; i++) {
                    > var temp = parts[i].split("=");[/color]

                    `temp' is redefined on every loop which will yield a warning in Mozilla/5.0.

                    Use

                    var temp;
                    for (...)
                    {
                    // ...
                    temp = ...
                    // ...
                    }

                    instead.
                    [color=blue]
                    > switch (temp[0]) {
                    > case "xx": xx = +temp[1]; break;
                    > case "yy": yy = +temp[1]; break;
                    > case "ff": ff = +temp[1]; break;
                    > case "ll": level = +temp[1]; break;
                    > }
                    > }
                    >
                    > Does this look good?[/color]

                    Quite. Using properties of a container object instead of switch...case
                    and global variables would look even better. Why reinventing the wheel,
                    check out JSX:search.js, it's for free. The only thing you are required
                    to respect is the GPL. And please copy and distribute it then.


                    HTH

                    PointedEars

                    Comment

                    • Lasse Reichstein Nielsen

                      #11
                      Re: url parsing

                      Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
                      [color=blue]
                      > Fabian wrote:[/color]
                      [color=blue][color=green]
                      >> for (var i = 0; i < parts.length; i++) {
                      >> var temp = parts[i].split("=");[/color]
                      >
                      > `temp' is redefined on every loop which will yield a warning in Mozilla/5.0.[/color]

                      Are you sure. I don't get any warning in Mozilla Firebird 0.7.

                      I believe that according to ECMA262, a variable declaration only generates
                      a local variable once, even if inside a loop. So the code following examples
                      are completely equivalent:

                      ---
                      for (var i=0;i<4;i++) {
                      var temp = i;
                      }
                      ---
                      and
                      ---
                      var temp;
                      for (var i=0;i<4;i++) {
                      temp = i;
                      }
                      ---
                      and even
                      ---
                      var temp,i;
                      for (i=0;i<4;i++) {
                      temp = i;
                      }
                      ---

                      /L
                      --
                      Lasse Reichstein Nielsen - lrn@hotpop.com
                      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                      'Faith without judgement merely degrades the spirit divine.'

                      Comment

                      • Fabian

                        #12
                        Re: url parsing

                        Thomas 'PointedEars' Lahn hu kiteb:
                        [color=blue]
                        >... Why reinventing the
                        > wheel, check out JSX:search.js, it's for free. The only thing you
                        > are required
                        > to respect is the GPL. And please copy and distribute it then.[/color]

                        The GPL is why I won't use it, but thanks for offering.


                        --
                        Fabian
                        Visit my website often and for long periods!
                        Penasaran slot depo 10k di AGAM69? Simak pengalaman main Mahjong Ways dan buy spin modal receh yang berakhir manis. Daftar dan buktikan sendiri.


                        Comment

                        • Jim Ley

                          #13
                          Re: url parsing

                          On Sat, 27 Dec 2003 05:31:22 +0100, Thomas 'PointedEars' Lahn
                          <PointedEars@we b.de> wrote:
                          [color=blue]
                          > Why reinventing the wheel,
                          >check out JSX:search.js, it's for free. The only thing you are required
                          >to respect is the GPL. And please copy and distribute it then.[/color]

                          Fully agree with not re-inventing the wheel, but I'd strongly
                          recommend finding something other than a GPL solution, all you script
                          would then have to be GPL'd.

                          Jim.
                          --
                          comp.lang.javas cript FAQ - http://jibbering.com/faq/

                          Comment

                          • Thomas 'PointedEars' Lahn

                            #14
                            Re: url parsing

                            Fabian wrote:[color=blue]
                            > Thomas 'PointedEars' Lahn hu kiteb:[color=green]
                            >>... Why reinventing the
                            >> wheel, check out JSX:search.js, it's for free. The only thing you
                            >> are required to respect is the GPL. And please copy and distribute
                            >> it then.[/color]
                            >
                            > The GPL is why I won't use it, [...][/color]

                            What exactly are you afraid of?
                            [color=blue]
                            > but thanks for offering.[/color]

                            You are welcome.


                            PointedEars

                            Comment

                            • Thomas 'PointedEars' Lahn

                              #15
                              Re: url parsing

                              Jim Ley wrote:[color=blue]
                              > Thomas 'PointedEars' Lahn wrote:[color=green]
                              >> Why reinventing the wheel, check out JSX:search.js, it's for free.
                              >> The only thing you are required to respect is the GPL. And please
                              >> copy and distribute it then.[/color]
                              >
                              > Fully agree with not re-inventing the wheel, but I'd strongly
                              > recommend finding something other than a GPL solution, all you script
                              > would then have to be GPL'd.[/color]

                              And the problem is?


                              PointedEars

                              Comment

                              Working...