views:

512

answers:

3

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.?

A: 

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
Can you provide a link that supports this point of view? I haven't come across this before.
Steve Harrison
Which point of view are you referring to? Global variables are a well-known code smell.
Matthew Flaschen
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).
stefpet
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
I never said you always had to. I just think it's better to be explicit in this case.
Matthew Flaschen
+2  A: 

Please refer to this similar SO post:
what-is-the-best-way-to-get-and-set-a-single-cookie-value-using-javascript

ichiban
+1  A: 

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
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
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
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