1

Possible Duplicate:
What is the “best” way to get and set a single cookie value using JavaScript

I am working on a project where it requires to check cookie and tell whether it is 20 minutes old or not. So I have written once code which is like this. This is only javascript code I have pasted.

function checkcookie()
{
    var difftime= getcookie();
    // further operation comes here
}

var cookieminutes;

function getcookie()
{
    var start = document.cookie.indexOf("expires");
    var cookiedate;

    if(start==-1)
    {
        cookiedate = new Date();
        document.write("Start equal to -1");
        document.cookie="expires="+cookiedate+",path=0,domain=0";
        cookieminutes= cookiedate.getMinutes();
    }
    else
    {
        document.write("Start not equal to -1");

        var date =  new Date();
        var minutes = date.getMinutes();

        document.write("The difference is "+minutes);
        document.write("<br />Cookie minutes is "+cookieminutes);
        return (minutes-cookieminutes);

    }
}

In function getcookie the variable cookieminutes is coming as undefined. But as I know since it is a global variable it should have the value.

Can anybody please tell what is the solution for this.?

Community
  • 1
  • 1
Chaithra
  • 1,130
  • 3
  • 14
  • 22
  • It looks like if getcookie is being called for the first time on a given page load, and the cookie exists (start != -1), cookieminutes is never set. – Matthew Flaschen May 12 '09 at 09:59

2 Answers2

1

You're only setting a value for cookieminutes in the top section of the if statement, so any references in the else section will be null.

Try this:

function getcookie()
{
    var start = document.cookie.indexOf("expires");
    var cookiedate;

    cookiedate = new Date();
    cookieminutes = cookiedate.getMinutes();

    if(start==-1)
    {    
        document.write("Start equal to -1");
        document.cookie="expires="+cookiedate+",path=0,domain=0";    
    }
    else
    {
        document.write("Start not equal to -1");

        var date =  new Date();
        var minutes = date.getMinutes();

        document.write("The difference is "+minutes);
        document.write("<br />Cookie minutes is "+cookieminutes);
        return (minutes-cookieminutes);

    }
}
jim0thy
  • 2,095
  • 1
  • 18
  • 27
  • Ur code is working... But if I use that cookieminutes and minutes both will be same value. I have to get a 20 mins differnece between the 2 because of my project requirement. so can u pls post some other solution thank u .. – Chaithra May 12 '09 at 10:51
  • I don't understand the reference to 20 minutes. As jim0thy and I said, your original code was using cookieminutes before setting it. You need to initialize it properly, probably by reading the cookie. If you don't know how to read cookies see http://www.quirksmode.org/js/cookies.html – Matthew Flaschen May 12 '09 at 11:12
  • Ok, after looking into this a bit, it would seem that the expiration date of a cookie is write-only, so JS can't read it. document.cookie.indexOf("expires") looks for a cookie NAMES "expires", and does not retrieve the expiration date. It looks like the only way around this is to create a 2nd cookie, and set it's value to the expiration date of the 1st cookie. Sorry. – jim0thy May 12 '09 at 12:59
-4

If you want to use global variables (generally bad design) set and access them explicitly with window. E.g.:

window.cookieminutes = cookiedate.getMinutes();

and later:

document.write("Cookie minutes is "+window.cookieminutes);

And drop the var cookieminutes;

As I said in my comment, it looks like if getcookie is being called for the first time on a given page load, and the cookie exists (start != -1), cookieminutes is never set. You need to make sure you don't use undefined variables.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • Can you provide a link that supports this point of view? I haven't come across this before. – Steve Harrison May 12 '09 at 09:31
  • Which point of view are you referring to? Global variables are a well-known code smell. – Matthew Flaschen May 12 '09 at 09:33
  • I assume it is the point of view of using globals by explicitly setting/getting them using the window object (instead of not explicitly using window). – stpe May 12 '09 at 09:50
  • Yes, I know that global variables should be avoided if possible. I'm curious about your statement "set and access them explicitly with window". I know that implied globals are bad, but why should you always use "window" when referring to or setting global variables in JavaScript? – Steve Harrison May 12 '09 at 09:51
  • 1
    I never said you always had to. I just think it's better to be explicit in this case. – Matthew Flaschen May 12 '09 at 09:53