url parsing

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

    url parsing

    I want to be able to open a window with an url that has parameters like
    so:

    <a href="foo.html? xx=5&yy=6&ff=1& level=0">..</a>

    And then javascript will enter these paramters as global variables.
    However, if one or more of these variables are not set, it should use
    default values for these variables, read from the start of the
    javascript file. How do I parse and set these variables?


    --
    --
    Fabian
    Visit my website often and for long periods!
    AGAM69 menghadirkan inspirasi desain kreatif, solusi digital, pengembangan teknologi, serta inovasi modern untuk kebutuhan bisnis dan profesional.


  • Fox

    #2
    Re: url parsing



    Fabian wrote:[color=blue]
    >
    > I want to be able to open a window with an url that has parameters like
    > so:
    >
    > <a href="foo.html? xx=5&yy=6&ff=1& level=0">..</a>
    >
    > And then javascript will enter these paramters as global variables.
    > However, if one or more of these variables are not set, it should use
    > default values for these variables, read from the start of the
    > javascript file. How do I parse and set these variables?[/color]

    since you want global defaults, and to cover the possibility that some
    data may be missing from the url, declare the variables beforehand:

    var xx = 0, yy=0, ff=0, level=0;


    window.location .search will have everything after and including the "?".

    var srch = window.location .search.substri ng(1); // cuts off the question mark

    then split srch at the ampersand:

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

    for(var i in parts)
    {
    var temp = parts[i].split("=");

    window[temp[0]] = temp[1];
    }


    or you could simply use eval(parts[i]); (depending on how you feel about
    using eval) [*this would be equivalent to eval("xx=5"), for example].


    You don't really need to initialize the globals, you can "collect" them
    at runtime and test for their existance before using a default value, as in:

    if(!someVar) someVar = initialValue;

    etc...

    there are also a number of other ways to do this... serverside solutions
    are pretty cool (they'll allow you to use POST without showing any data
    in the URL, and it supports much greater amounts of data).



    [color=blue]
    >
    > --
    > --
    > Fabian
    > Visit my website often and for long periods!
    > http://www.lajzar.co.uk[/color]

    Comment

    • Fabian

      #3
      Re: url parsing

      Fox hu kiteb:
      [color=blue]
      > window.location .search will have everything after and including the
      > "?".
      >
      > var srch = window.location .search.substri ng(1);
      > // cuts off the question mark
      >
      > // then split srch at the ampersand:
      >
      > var parts = srch.split("&") ;
      > for(var i in parts) {
      > var temp = parts[i].split("=");
      > window[temp[0]] = temp[1];
      > }
      >
      >
      > or you could simply use eval(parts[i]); (depending on how you feel
      > about using eval) [*this would be equivalent to eval("xx=5"), for
      > example].[/color]

      Thats pretty much what I had, except it was not working, despite my
      alerts telling me that the variables had been set with the intended
      values. My using those variables later was coming up with all manner of
      errors. Then I had an inspiration...

      window[temp[0]] = 1 * temp[1];

      I realised javascript doesnt really know teh difference between text and
      numbers unless you tell it. So I told it. Now, it works. But your
      producing the same (more or less) code I had did tell me that I was
      looking in the wrong place for the problem. Thanks.


      --
      --
      Fabian
      Visit my website often and for long periods!
      AGAM69 menghadirkan inspirasi desain kreatif, solusi digital, pengembangan teknologi, serta inovasi modern untuk kebutuhan bisnis dan profesional.


      Comment

      • Fabian

        #4
        Re: url parsing

        Fabian hu kiteb:
        [color=blue]
        > window[temp[0]] = 1 * temp[1];[/color]

        Espirit d'escalier:

        if (temp[0] == "xx") { xx = 1 * temp[1]; }

        Instead of the line I wrote earlier. That should stop any enterprising
        hacker from writing random nonsense into the script.


        --
        --
        Fabian
        Visit my website often and for long periods!
        AGAM69 menghadirkan inspirasi desain kreatif, solusi digital, pengembangan teknologi, serta inovasi modern untuk kebutuhan bisnis dan profesional.



        Comment

        • Fox

          #5
          Re: url parsing



          Fabian wrote:[color=blue]
          >
          > Fox hu kiteb:
          >[color=green]
          > > window.location .search will have everything after and including the
          > > "?".
          > >
          > > var srch = window.location .search.substri ng(1);
          > > // cuts off the question mark
          > >
          > > // then split srch at the ampersand:
          > >
          > > var parts = srch.split("&") ;
          > > for(var i in parts) {
          > > var temp = parts[i].split("=");
          > > window[temp[0]] = temp[1];
          > > }
          > >
          > >
          > > or you could simply use eval(parts[i]); (depending on how you feel
          > > about using eval) [*this would be equivalent to eval("xx=5"), for
          > > example].[/color]
          >
          > Thats pretty much what I had, except it was not working, despite my
          > alerts telling me that the variables had been set with the intended
          > values. My using those variables later was coming up with all manner of
          > errors. Then I had an inspiration...
          >
          > window[temp[0]] = 1 * temp[1];[/color]

          oops -- you're right -- but only if you require number types... with
          mixed types, this gets a little dicey, so you should probably test isNaN
          too (if your data is mixed).

          you might find that window[temp[0]] = +temp[1]; a little more efficient
          -- it saves from having to execute a multiply (note that there is no
          space between the '+' operator and the variable).

          [color=blue]
          >
          > I realised javascript doesnt really know teh difference between text and
          > numbers unless you tell it. So I told it. Now, it works. But your
          > producing the same (more or less) code I had did tell me that I was
          > looking in the wrong place for the problem. Thanks.
          >
          > --
          > --
          > Fabian
          > Visit my website often and for long periods!
          > http://www.lajzar.co.uk[/color]

          Comment

          • Dr John Stockton

            #6
            Re: url parsing

            JRS: In article <3FD8623F.EB87C 9B6@spam.com>, seen in
            news:comp.lang. javascript, Fox <dont@spam.co m> posted at Thu, 11 Dec
            2003 06:25:36 :-[color=blue]
            >
            >you might find that window[temp[0]] = +temp[1]; a little more efficient
            >-- it saves from having to execute a multiply (note that there is no
            >space between the '+' operator and the variable).[/color]

            The absence of a space is unnecessary.

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> Jsc maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics, links.

            Comment

            Working...