JavaScript [RESOLVED] How Do You Read/Write Mult-value cookies in Javascript?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: [RESOLVED] How Do You Read/Write Mult-value cookies in Javascript?

  1. #1
    Join Date
    Dec 2009
    Posts
    596

    [RESOLVED] How Do You Read/Write Mult-value cookies in Javascript?

    Good morning gang.

    I'm having a hard time figuring out how to do this. I'm trying to write a cookie that has a name for example "Order" in which this "Order" cookie will have values associated with it for example:Color, Location, Type and Style. I see that this is pretty simple to do in .net but it's during a jquery grid row select event that I have the row's values available for processing. In other words in a bit of jquery code is where I am able to deal with the data to be passed around in the web app. If I could get that data out to a .net routine that would be ideal but to me it looks like I can't do that. So anyway does anyone have a little code snippet that they could share with me on how to do this? Please.

    -Juan

  2. #2
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,870

    Re: How Do You Read/Write Mult-value cookies in Javascript?

    Are you just trying to read a cookie through JavaScript? If so...there are plenty examples on such.

    Code:
    function getCookie(cookie_name) {
      var cookies = document.cookie.split(";");
      for (var cookie = 0; cookie < cookies.length; cookie++) {
        var currentCookieName = cookies[cookie].substr(0, cookies[cookie].indexOf("="));
        var currentCookieValue = cookies[cookie].substr(cookies[cookie].indexOf("=") + 1);
        currentCookieName = currentCookieName.replace(/^\s+|\s+$/g,"");
        if (currentCookieName == cookie_name) {
          return unescape(currentCookieValue);
        }
      }
    }
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,870

    Re: How Do You Read/Write Mult-value cookies in Javascript?

    Are you just trying to read a cookie through JavaScript? If so...there are plenty examples on such.

    Code:
    function getCookie(cookie_name) {
      var cookies = document.cookie.split(";");
      for (var cookie = 0; cookie < cookies.length; cookie++) {
        var currentCookieName = cookies[cookie].substr(0, cookies[cookie].indexOf("="));
        var currentCookieValue = cookies[cookie].substr(cookies[cookie].indexOf("=") + 1);
        currentCookieName = currentCookieName.replace(/^\s+|\s+$/g,"");
        if (currentCookieName == cookie_name) {
          return unescape(currentCookieValue);
        }
      }
    }
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  4. #4
    Join Date
    Dec 2009
    Posts
    596

    Re: How Do You Read/Write Mult-value cookies in Javascript?

    Hello PeejAvery,

    I also need to write such a cookie. Any chance you have the javacript function that writes the type of multi-valued cookie I described in my post? Especially can you help me out with the js that writes the mult-valued cookie that can easily be read by the Read js code you provided?

  5. #5
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,870

    Re: How Do You Read/Write Mult-value cookies in Javascript?

    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  6. #6
    Join Date
    Dec 2009
    Posts
    596

    Re: How Do You Read/Write Mult-value cookies in Javascript?

    Thanks PeejAvery,

    I've seen this code at w3schools before. As-is the code creates a cookie in which the cookie will have a name and have one value asscoicated with it and not the multivalue type problem I wrote about that resemble this:"Order" in which this "Order" cookie will have values associated with it for example:Color, Location, Type and Style.

    Or it could be that I'm a dum dum and don't see that the code as-is just needs a little tweaking. But I don't how to do get the code to support a multi value cookie. I don't undertand what the syntax needs to look like for a cookie that has more than one value.

    Or It could be that I'm a super duper dum dum and don't see that the code as-is already supports the multi-value type cookie that I'm trying to do.

  7. #7
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,870

    Re: How Do You Read/Write Mult-value cookies in Javascript?

    Cookies only store as strings. So, if you want multiple datatypes or values in 1 cookie, you will have to make it an object and serialize it. Or you can make it character delimited data and just split it into an array.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  8. #8
    Join Date
    Dec 2009
    Posts
    596

    Re: How Do You Read/Write Mult-value cookies in Javascript?

    Oh ok, so the value will be have to be a put together thingie. I'll start trying to put this together and post my work of art when I'm done. Thanks for the direction. Stay tuned...

  9. #9
    Join Date
    Dec 2009
    Posts
    596

    Re: How Do You Read/Write Mult-value cookies in Javascript?

    ...So I have it working now. I used pipes for the delimination. Here's the js/jq:
    Code:
     // display selected row index.
                            $("#jqxgrid").bind('rowselect', function (event) {
                                var row = $("#jqxgrid").jqxGrid('getrowdata', event.args.rowindex);
                                //alert(row.ORG_ID + " " + row.Organization_Name);
                                //document.getElementById("<%=HiddenField1.ClientID %>").value = row.Organization_Name;
                                //alert(document.getElementById("<%=HiddenField1.ClientID %>").value);
                                //Document.cookie = 'MickeyCookie=' + row.Organization_Name; + "path=/; expires=Wed, 1 Jan 2020 00:00:00 GMT";
                                var mySelectedRow = row.ORG_ID + '|' + row.Organization_Name + '|' + row.address_street;
                                var exdate = new Date();
                                exdate.setDate(exdate.getDate() + 365);
                                //var c_value = escape("TestValue") + ((365 == null) ? "" : "; expires=" + exdate.toUTCString());
                                //var c_value = escape(row.Organization_Name) + ((365 == null) ? "" : "; expires=" + exdate.toUTCString());
                                var c_value = mySelectedRow + ((365 == null) ? "" : "; expires=" + exdate.toUTCString());
                                document.cookie = "TestCookie" + "=" + c_value;
                            });
    and here's the asp.net page load:
    Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            'HttpCookie(cookie = Response.Cookies.Get("myCookieName"));
            'HttpCookie(InlineAssignHelper(cookie, Response.Cookies.[Get]("myCookieName")))
            'Label1.Text = Request.Cookies("TestCookie").Value
            Dim rawRow As String = Request.Cookies("TestCookie").Value
            'txtName.Text = Request.Cookies("TestCookie").Value
            Dim rawRowSplit() As String = Split(rawRow, "|")
            txtOrgID.Text = rawRowSplit(0)
            txtName.Text = rawRowSplit(1)
            txtStreet.Text = rawRowSplit(2)
    
        End Sub
    super thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center