SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    May 2011
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Writing/reading objects in cookies

    Hey,

    So I need to make a webshop for a school project.
    I wrote the javascript for my shopping cart page.

    I made an object Artikel and an object ArtikelList which contains an array of Artikel objects. Then I save the ArtikelList object into a cookie.
    But when I try to read the data from the cookie again I can't get it to work.

    Let me know if you need more info/details..

    Could someone please point out any mistakes that could solve my problem?

    Regards
    John

    Links with the javascript:
    [JavaScript] artikelToevoegenAanWinkelkarretje.js - Pastebin.com
    [JavaScript] winkelkarretjeInlezen.js - Pastebin.com

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you checked that the cookie is actually being written?

    In any case your cookie reader should return a string not type object, which is what your notes suggest you're getting.
    You could try the function below instead.

    Remember that the value returned is still a string until converted using JSON.parse();

    Code:
    function getCookie( naam ) 
    { 
      var cookieArray = document.cookie.split( /;\s*/ ),
          cookie,
          cValue = ""; 
          
      for( var teller = 0; teller < cookieArray.length && !cValue; teller++ ) 
        if( ( cookie = cookieArray[ teller ] ).indexOf( naam ) == 0 ) 
          cValue = cookie.split( "=" )[ 1 ]; 
     
      return decodeURIComponent( cValue ); 
    }
    Tab-indentation is a crime against humanity.


Tags for this Thread

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
  •