Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2003
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question cookie with multible values?

    is it possible?
    i'm making a text based RPG and need to save a lot of variables

    i have a cookie to save the current city that you're in. this is it
    Code:
    function save(){
    d=new Date();
    d.setFullYear(d.getFullYear() + 1);
    expires="expires=" + d.toGMTString();
    document.cookie="currentcity=" + currentcity + "; "  + expires;
    }
    
    function load(){
    cookies=document.cookie;
    startpos=cookies.indexOf("currentcity") + 12;
    endpos=cookies.indexOf(";",startpos);
    if (endpos==-1) endpos=cookies.length;
    currentcity = cookies.substring(startpos,endpos);
    }
    there is a limit of 20 cookies per domain
    is there any way i could also add other values to this cookie?
    i need to be able to save:

    currentcity
    maxhealth
    health
    strength
    defence
    exp
    expntl
    gold
    mname
    mhealth
    mstrength
    mdefence
    mgold
    mexp
    level
    currentmonster
    quest

    thats under 20, but i may need to add more. also, having that many cookies would be hard to manage

    any help would be greatly appreciated

  • #2
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    I think it is possible... you create the one cookie per user and have multiple values inside it, possibly as an array?

    I'm not 100% sure, but you could have

    Code:
    var myCookie = array()
    myCookie[0] = "manchester"; //city
    myCookie[1] = "96" //health
    
    etc
    then set "myCookie" as the cookie...... that might work - but someone else might be able to suggest something better.....!!

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Flint, Michigan, USA
    Posts
    620
    Thanks
    1
    Thanked 20 Times in 20 Posts
    Check out the "Samples of my work > Cookies > Subdivided cookies" area of my site at:

    http://spruce.flint.umich.edu/~jalarie/


  •  

    Posting Permissions

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