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
    Regular Coder
    Join Date
    Dec 2003
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Cookie program not working

    I wrote this cookie script to see if I knew how, but it's not working. That yellow exclamation mark appears in the IE status bar, but there are no error messages, so I tried running it through a debugger, which returned the error that a '}' was expected at line 62 of the script, that's the last line, which is present. I think the error is in the checkcookie() function, which is called by an onLoad in the body tag, as I made an alert in the setcookie() function, which is supposed to load, as its the first time I run it, to see if I got all the variables right, which doesn't pop up.

    Code:
     function fixtime(number) //function to fix minutes and seconds
      {
       if (number < 10)
        {
         number = "0" + number;
         }
       return number;
      }
    
     function setcookie() //function that sets cookie
      {
        //vars for cookie info
       var name = prompt("What's your name?", "");
       var date = new Date();
       var year = date.getFullYear();
       var num_month = date.getMonth();
       var day_of_month = date.getDate()
       var num_day = date.getDays();
       var hour = date.geHours();
       var min = date.getMinutes();
       var fixed_min = fixtime(min);
       var sec = date.getseconds();
       var fixed_sec = fixtime(sec);
       var days = new Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
       var months = new Array("January", "Februari", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
       var month = months[num_month]
       var day_of_week = days[num_day]
       
       alert (hour + ":" + fixed_min + ":" + fixed_sec + ", " + day_of_week + " " + month + " " + day_of_month + ", " + year)
       
        //vars for cookie expiration date
       var expire_date = new Date("Januari 1, 2050");
       var gmt_expire_date = expire_date.toGMTstring();
       
        //build and save cookie
       var cookie = "rpglyrics=" + escape(name + "/" + hour + ":" + fixed_min + ":" + fixed_sec + "/" + day_of_week + " " + month + " " + day_of_month + ", " + year);
       cookie = cookie + ";expires=" + gmt_expire_date;
       document.cookie = cookie;
      }
    
     function readcookie() //function that reads cookie and greets returning user
      {
       // take cookie, unescape and split name/time/date
       var cookie = document.cookie;
       var cookie = unescape(cookie);
       var split_cookie = cookie.split("/")
       
       // message for returning user
       alert ("Hello, " + split_cookie[0] + ", the last time you visited was on " + split_cookie[3] + ", at " + split_cookie[2] + ".")
      }
    
     function checkcookie()
      {
       if (document.cookie.length == 0)
        {
         setcookie();
        }
       else
        {
         readcookie();
        {
      }

  • #2
    Regular Coder COBOLdinosaur's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    at the end after readcookie() you have { instead of }
    100% standards compliant code is 100% correct 100% of the time.
    one of my toys from my repository and perhaps some help getting help

    Cd&

  • #3
    Regular Coder
    Join Date
    Dec 2003
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks. Strange it directed me to line 62 instead of 61 though.

    Now there aren't any errors anymore, but the script still isn't working like I want it to. It runs but doesn't save anything. Now my question, does the page need to be on a server for cookies to work? I kind of forgot about that when I wrote it.


  •  

    Posting Permissions

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