Results 1 to 4 of 4

Thread: Manipulating Cookies with JS

  1. #1
    Join Date
    Mar 2006
    Posts
    41
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Manipulating Cookies with JS

    I am new to JavaScript, so there is probably a better way to do this...

    Suppose I have a cookie consisting of a series of numbers, and a have one of those numbers I want to delete from the cookie...

    Cookie (example): 12|56|23|67

    Number to delete ( idToDelete ): 23

    Output: 12|56|67
    Code:
    function delfslist() 
      {
        var cookiename = "fslist"; 
        var idToDelete = document.getElementById('fsid').value; 
        var idArray = null;
        var idArrayLen = null;
        var cookiecontents = null;
        var newcookiecontents = null;
        // Get the cookie
        cookiecontents = get_cookie ( cookiename );
        // If there is a cookie, do this...
        if (cookiecontents != null) {
          // Split the cookie into an array.
          idArray = cookiecontents.split("|");	
          // Get the array count.
          idArrayLen = idArray.length;
          // If there is only ONE, delete the whole cookie.
          if (idArrayLen == 1)
          {	
            // If only one ID in cookie, delete it.
            delete_cookie ( cookie_name )
          } else {
            // Loop through array...
            for (i=0; i < idArrayLen; i++) {
              if (( idArray[i] != idToDelete ) && ( newcookiecontents != null )) {
                newcookiecontents = newcookiecontents+"|"+idArray[i];
              } else if ( idArray[i] != idToDelete ) {
                newcookiecontents = idArray[i];
              }
            }
          }
            set_cookie(cookiename, newcookiecontents);
          }
       }
    Set_cookie and get_cookie are functions that do what they sound like.

    So, my question is: Is there a better way to do this?

  2. #2
    Join Date
    Mar 2006
    Posts
    41
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    I'm wondering if there might be a way to apply Regular Expressions to this problem?

  3. #3
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Try this one

    Code:
    function delfslist() {
        var cookieName = "fslist";
        var idToDelete = document.getElementById('fsid').value;
        var cookiecontents = null;
        var idArray;
        cookiecontents = get_cookie(cookiename); // If there is a cookie, do this...
        if (cookiecontents !== null) {
            idArray = cookicontent.split("|");
            for (var i = 0; i < idArray.length; i++) {
                if (idArray[i] === idToDelete) {
                    idArray.splice(i, 1);
                }
            }
            cookiecontents = idArray.join("|");
            if (cookiecontents !== "") {
                set_cookie(cookiename, cookiecontents);
            }
        }
    }

  4. #4
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    posted in error

Bookmarks

Posting Permissions

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