Splitting cookie

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

    Splitting cookie

    Can anyone help with this problem. I am attempting to dynamically draw a
    graph using data from a cookie. I have written the script to actually draw
    the graph, for which I hav created two arrays with the data I required
    preset into them. What I wish to do is split the cookie in which the data
    is stored and create two array from it.

    The function used to create the cookie is as follows

    :function SetCookie(name, value) {
    document.cookie = name + "=" + escape(value);
    }

    which is called with the following script:

    <body onload="SetCook ie('sales','pan 1=23,pan3=153,p an4=21,pan9=5') ">

    I wish to create the following arrays by splitting the cookie:

    array1=("sales" ,"pan1","pan3", "pan4","pan 9")
    array2=(23,153, 21,5)

    Can anyone help? Many thanks in advance.





  • McKirahan

    #2
    Re: Splitting cookie

    "Steve Darby" <ramesses_the_g reat@ntlworld.c om> wrote in message
    news:cYSGd.114$ ga2.42@newsfe6-win.ntli.net...[color=blue]
    > Can anyone help with this problem. I am attempting to dynamically draw a
    > graph using data from a cookie. I have written the script to actually[/color]
    draw[color=blue]
    > the graph, for which I hav created two arrays with the data I required
    > preset into them. What I wish to do is split the cookie in which the data
    > is stored and create two array from it.
    >
    > The function used to create the cookie is as follows
    >
    > :function SetCookie(name, value) {
    > document.cookie = name + "=" + escape(value);
    > }
    >
    > which is called with the following script:
    >
    > <body onload="SetCook ie('sales','pan 1=23,pan3=153,p an4=21,pan9=5') ">
    >
    > I wish to create the following arrays by splitting the cookie:
    >
    > array1=("sales" ,"pan1","pan3", "pan4","pan 9")
    > array2=(23,153, 21,5)
    >
    > Can anyone help? Many thanks in advance.[/color]

    What does your function "SetCookie( )" look like?


    Comment

    • Steve Darby

      #3
      Re: Splitting cookie

      Like this:

      function SetCookie(name, value) {
      document.cookie = name + "=" + escape(value);
      }



      Comment

      • McKirahan

        #4
        Re: Splitting cookie

        "Steve Darby" <ramesses_the_g reat@ntlworld.c om> wrote in message
        news:InUGd.147$ ga2.130@newsfe6-win.ntli.net...[color=blue]
        > Like this:
        >
        > function SetCookie(name, value) {
        > document.cookie = name + "=" + escape(value);
        > }[/color]

        If you do:
        alert(unescape( document.cookie ));

        do you get this?
        sales=pan1=23,p an3=153,pan4=21 ,pan9=5



        Comment

        • McKirahan

          #5
          Re: Splitting cookie

          "Steve Darby" <ramesses_the_g reat@ntlworld.c om> wrote in message
          news:InUGd.147$ ga2.130@newsfe6-win.ntli.net...[color=blue]
          > Like this:
          >
          > function SetCookie(name, value) {
          > document.cookie = name + "=" + escape(value);
          > }[/color]


          Are you sure that you don't want:

          array1=("pan1", "pan3","pan4"," pan9")
          array2=(23,153, 21,5)

          isntead of:

          array1=("sales" ,"pan1","pan3", "pan4","pan 9")
          array2=(23,153, 21,5)

          thus array1[0] and array2[0] will correspond to "pan1" and "23".


          Comment

          • Steve Darby

            #6
            Re: Splitting cookie

            I need 'sales' to be in array1 as well as the rest as it is used to form the
            heading of the bar chart


            Comment

            • McKirahan

              #7
              Re: Splitting cookie

              "Steve Darby" <ramesses_the_g reat@ntlworld.c om> wrote in message
              news:aHUGd.151$ ga2.16@newsfe6-win.ntli.net...[color=blue]
              > I need 'sales' to be in array1 as well as the rest as it is used to form[/color]
              the[color=blue]
              > heading of the bar chart[/color]


              Will this work for you?

              <html>
              <head>
              <title>namevals .htm</title>
              <script type="text/javascript">
              var array1 = new Array();
              var array2 = new Array();
              function GetCookie() {
              var what = unescape(docume nt.cookie);
              array1[0] = what.substr(0,w hat.indexOf("=" ));
              what = what.substr(wha t.indexOf("=")+ 1);
              var pair = what.split(",") ;
              var j = 1;
              var k = 0;
              for (var i=0; i<pair.length; i++) {
              var item = pair[i].split("=");
              array1[j++] = '"' + item[0] + '"';
              array2[k++] = item[1];
              }
              // display contents of both arrays:
              what = unescape(docume nt.cookie);
              what += "\n\n array1 = " + array1.join("," );
              what += "\n\n array2 = " + array2.join("," );
              alert(what);
              }
              function SetCookie(name, value) {
              document.cookie = name + "=" + escape(value);
              }
              </script>
              </head>
              <body onload="SetCook ie('sales','pan 1=23,pan3=153,p an4=21,pan9=5') ">
              <a href="javascrip t:GetCookie()"> Get Cookie</a>
              </body>
              </html>


              Comment

              • Steve Darby

                #8
                Re: Splitting cookie

                Will try it and let you know

                "McKirahan" <News@McKirahan .com> wrote in message
                news:ILydnTqIS6 7KhXHcRVn-rA@comcast.com. ..[color=blue]
                > "Steve Darby" <ramesses_the_g reat@ntlworld.c om> wrote in message
                > news:aHUGd.151$ ga2.16@newsfe6-win.ntli.net...[color=green]
                >> I need 'sales' to be in array1 as well as the rest as it is used to form[/color]
                > the[color=green]
                >> heading of the bar chart[/color]
                >
                >
                > Will this work for you?
                >
                > <html>
                > <head>
                > <title>namevals .htm</title>
                > <script type="text/javascript">
                > var array1 = new Array();
                > var array2 = new Array();
                > function GetCookie() {
                > var what = unescape(docume nt.cookie);
                > array1[0] = what.substr(0,w hat.indexOf("=" ));
                > what = what.substr(wha t.indexOf("=")+ 1);
                > var pair = what.split(",") ;
                > var j = 1;
                > var k = 0;
                > for (var i=0; i<pair.length; i++) {
                > var item = pair[i].split("=");
                > array1[j++] = '"' + item[0] + '"';
                > array2[k++] = item[1];
                > }
                > // display contents of both arrays:
                > what = unescape(docume nt.cookie);
                > what += "\n\n array1 = " + array1.join("," );
                > what += "\n\n array2 = " + array2.join("," );
                > alert(what);
                > }
                > function SetCookie(name, value) {
                > document.cookie = name + "=" + escape(value);
                > }
                > </script>
                > </head>
                > <body onload="SetCook ie('sales','pan 1=23,pan3=153,p an4=21,pan9=5') ">
                > <a href="javascrip t:GetCookie()"> Get Cookie</a>
                > </body>
                > </html>
                >
                >[/color]


                Comment

                • Steve Darby

                  #9
                  Re: Splitting cookie

                  Have run the script and the alert returns the following:

                  array1=,"","pan 3","pan4","pan9 "
                  array2=,153,21, 5;style

                  And I should have mention that I am using script which allows me to change
                  text size on the page and saves the prefered values as a cookie, hence the
                  appearance of style in the second array. I am trying only to the
                  information listed for sales, if that makes sense


                  Comment

                  • McKirahan

                    #10
                    Re: Splitting cookie

                    "Steve Darby" <ramesses_the_g reat@ntlworld.c om> wrote in message
                    news:iYUGd.156$ ga2.153@newsfe6-win.ntli.net...[color=blue]
                    > Have run the script and the alert returns the following:
                    >
                    > array1=,"","pan 3","pan4","pan9 "
                    > array2=,153,21, 5;style
                    >
                    > And I should have mention that I am using script which allows me to change
                    > text size on the page and saves the prefered values as a cookie, hence the
                    > appearance of style in the second array. I am trying only to the
                    > information listed for sales, if that makes sense[/color]


                    Would you change
                    alert(what);
                    to
                    document.write( what);
                    and post the results here.


                    Comment

                    • McKirahan

                      #11
                      Re: Splitting cookie

                      "McKirahan" <News@McKirahan .com> wrote in message
                      news:JZWdnYa_0c B5unHcRVn-hQ@comcast.com. ..[color=blue]
                      > "Steve Darby" <ramesses_the_g reat@ntlworld.c om> wrote in message
                      > news:iYUGd.156$ ga2.153@newsfe6-win.ntli.net...[color=green]
                      > > Have run the script and the alert returns the following:
                      > >
                      > > array1=,"","pan 3","pan4","pan9 "
                      > > array2=,153,21, 5;style
                      > >
                      > > And I should have mention that I am using script which allows me to[/color][/color]
                      change[color=blue][color=green]
                      > > text size on the page and saves the prefered values as a cookie, hence[/color][/color]
                      the[color=blue][color=green]
                      > > appearance of style in the second array. I am trying only to the
                      > > information listed for sales, if that makes sense[/color]
                      >
                      >
                      > Would you change
                      > alert(what);
                      > to
                      > document.write( what);
                      > and post the results here.[/color]


                      Rather just add the following after the alert():

                      what = unescape(docume nt.cookie);
                      what += "<br><br> array1 = " + array1.join("," );
                      what += "<br><br> array2 = " + array2.join("," );
                      document.write( what);


                      Here's what I get:

                      sales=pan1=23,p an3=153,pan4=21 ,pan9=5

                      array1 = sales,"pan1","p an3","pan4","pa n9"

                      array2 = 23,153,21,5


                      Comment

                      • McKirahan

                        #12
                        Re: Splitting cookie

                        "McKirahan" <News@McKirahan .com> wrote in message
                        news:JZWdnYa_0c B5unHcRVn-hQ@comcast.com. ..[color=blue]
                        > "Steve Darby" <ramesses_the_g reat@ntlworld.c om> wrote in message
                        > news:iYUGd.156$ ga2.153@newsfe6-win.ntli.net...[color=green]
                        > > Have run the script and the alert returns the following:
                        > >
                        > > array1=,"","pan 3","pan4","pan9 "
                        > > array2=,153,21, 5;style
                        > >
                        > > And I should have mention that I am using script which allows me to[/color][/color]
                        change[color=blue][color=green]
                        > > text size on the page and saves the prefered values as a cookie, hence[/color][/color]
                        the[color=blue][color=green]
                        > > appearance of style in the second array. I am trying only to the
                        > > information listed for sales, if that makes sense[/color][/color]

                        I forgot the first element was supposed to quoted; try this:

                        <html>
                        <head>
                        <title>namevals .htm</title>
                        <script type="text/javascript">
                        var array1 = new Array();
                        var array2 = new Array();
                        function GetCookie() {
                        var what = unescape(docume nt.cookie);
                        array1[0] = '"' + what.substr(0,w hat.indexOf("=" )) + '"';
                        what = what.substr(wha t.indexOf("=")+ 1);
                        var pair = what.split(",") ;
                        var j = 1;
                        var k = 0;
                        for (var i=0; i<pair.length; i++) {
                        var item = pair[i].split("=");
                        array1[j++] = '"' + item[0] + '"';
                        array2[k++] = item[1];
                        }
                        what = unescape(docume nt.cookie);
                        what += "<br><br> array1 = " + array1.join("," );
                        what += "<br><br> array2 = " + array2.join("," );
                        document.write( what);
                        }
                        function SetCookie(name, value) {
                        document.cookie = name + "=" + escape(value);
                        }
                        </script>
                        </head>
                        <body onload="SetCook ie('sales','pan 1=23,pan3=153,p an4=21,pan9=5') ">
                        <a href="javascrip t:GetCookie()"> Get Cookie</a>
                        </body>
                        </html>


                        You should get this:

                        sales=pan1=23,p an3=153,pan4=21 ,pan9=5

                        array1 = "sales","pan1", "pan3","pan4"," pan9"

                        array2 = 23,153,21,5


                        Comment

                        Working...