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 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2004
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Storing values in a JavaScript Cookie

    I'm trying to store values in a cookie for a Virtual Pet - and I've got some problems.

    I'm posting my current code. My pseudocode runs like this:
    if cookie exists, "welcome back username"
    Plus Use the values stored in the cookie and put them in the text boxes

    .....
    close game
    store values in cookie for next time
    I still have some major errors with the cookie. Can all the variables be stored in one cookie?

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    
    <head>
    
    <script>
    
    var exp = new Date();     //create new date object
    exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 30));     //set it 30 days ahead
    
    function getCookie(NameOfCookie){
    
        if (document.cookie.length > 0) {              
        begin = document.cookie.indexOf(NameOfCookie+"=");       
        if (begin != -1) {           
          begin += NameOfCookie.length+1;       
          end = document.cookie.indexOf(";", begin);
          if (end == -1) end = document.cookie.length;
            return unescape(document.cookie.substring(begin, end));
        } 
      }
      return null;
    }
    
    function setCookie() {
    //get form data
    	var health = document.form1.health.value;
    	var hunger = document.form1.hunger.value;
    	var age = document.form1.age.value;
    	setCookie1("myHealth",health, exp); 
    setCookie2("myHunger",hunger, exp); 
    setCookie3("myAge",age, exp);   
    }
    
    function delCookie (NameOfCookie) {
      if (getCookie(NameOfCookie)) {
        document.cookie = NameOfCookie + "=" +
        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
      }
    }
    
    function DoTheCookieStuff()
    {
    Preference=getCookie();
    if (Preference!=null) 
         {alert('Hi - '+ "your health is " + health + "your hunger is " + hunger +"your age is " + age}
    document.form1.health.value =getCookie1(myHealth);
    	document.form1.hunger.value=getCookie2(myHunger);
    document.form1.age.value =getCookie1(myAge);
    }
    
    </script>
    
    <title>Cookies</title>
    </head>
    <body onLoad="DoTheCookieStuff()">
    <script language="JavaScript">
    function getState(){
    var health=Math.floor(Math.random()*2)+1;
    var hunger=Math.floor(Math.random()*2)+1;
    var age=Math.floor(Math.random()*2)+1;
    document.pet.health.value = (health);
    document.pet.hunger.value = (hunger);
    document.pet.age.value = (age);
    var username=prompt('Hi - this is your first visit to my page - please enter your name.',"");
    }
    </script>
    
    
    <p align="center"><a href="#" onClick="delCookie('username')">Click here to reset the
    cookie.</a></p>
    
    <p align="center"><a href="cookie4.html" onClick="DoTheCookieStuff()">Click here to reload the page.</a></p>
    
    <p align="center">&nbsp;</p>
    
    </script>
    <form name="pet">
    <input type="button" value = "Get State" onclick="getState()">
    <h4>Health :</h4>
    <input type="text" size="4" name="health"><br>
    <h4>Hunger :</h4>
    <input type="text" size="4" name="hunger"><br>
    <h4>Age :</h4>
    <input type="text" size="4" name="age"><br>
    
    <input type="button" value="Exit game" onClick="setCookie()">
    </form>
    </body>
    </body>
    </html>

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    onload you call getCookie() with no argument,
    and getCookie tries to get the document.cookie.indexOf(NameOfCookie+"=")-
    where NameOfCookie refers to an argument.

    That's an error.


  •  

    Posting Permissions

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