views:

151

answers:

2

I am learning, please be kind if it is an obvious mistake.

/*set cookie*/
function setCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    setCookie(name,"",-1);
}


$(window).load(function() {
//check for the cookie
    var language = readCookie('language');
//if  null show red
    if (language!=null)
    {
        $('.blue').hide();
        alert('No cookie set'); 
    }
//if  null show red
    else if (language!=red)
    {
        $('.blue').hide();
        alert('The cookie is red'); 
    }
//if blue show blue
    else {
        $('.red').hide();
        alert('The cookie is set to blue'); 
    }


    $('#blue').click(function() {
            $('.red').hide();
            $('.blue').show();
            setCookie('language','blue', 30);
            alert('The cookie is set to blue'); 
        });

    $('#red').click(function() {
            $('.red').show();
            $('.blue').hide();
            setCookie('language','red', 30);                
            alert('The cookie is set to red'); 
        });
});

Thank-you.

+1  A: 

If you're learning the best answer to your question here would probably be some debugging tips. What are you using for debugging?

Have you tried Firebug in Firefox. http://getfirebug.com/ This will let you step through your code one line at a time and inspect exactly what it is doing, what values are really being passed to functions, and exactly what string you are assigning to document.cookie.

Andy Hume
Yes, I am using Firebug. Thanks.
laura
Firebug tutorials: http://www.evotech.net/blog/2007/06/introduction-to-firebug/, http://www.webmonkey.com/tutorial/Build_Better_Pages_With_Firebug, http://www.digitalmediaminute.com/screencast/firebug-js/
outis
+1  A: 

Learning to use Firebug is the best advice. Another helpful tool is a colorizing editor. Even SO provides a little help of this kind. Note the line:

    else if (language!=red)

"red" wasn't colored as a string, so it stood out by not standing out. Is there a variable named "red" defined somewhere in the original code? If not, this compares language to undefined.

Also, consider the lines that follow this statement:

    else if (language!='red') {
        $('.blue').hide();
        alert('The cookie is red');

You just tested that the cookie value was not red, but you alert that it is. You make a similar mistake on the first test:

    if (language!=null) {
        $('.blue').hide();
        alert('No cookie set');

language is not null, but the message you display suggests it is.

This isn't related to your question, but there are some simplifications you can make. For one, jQuery defines a trim method on String to remove leading and trailing whitespace. Make use if this in readCookie rather than the while (c.charAt(0)==' ') loop.

Another improvement is that the current technique of choosing an element to show by using a sequence of if blocks doesn't scale well. Your homework is to use string operations and whatnot to better handle an arbitrary number of elements, then add ".green", ".yellow", ".magenta" and ".cyan" elements. You might need to change the default behavior from showing all .color elements to hiding them. Using objects as associative arrays may also be helpful.

outis
Thanks. That is my problem mostly is I don't know the what is expected in most places. I'm using Notepad++ so there is colorizing but it didn't stand out because I don't know what is supposed to be there.I don't understand why it seems I am setting the cookie setCookie('language','blue', 30);with an expiration of 30 days but when I check the cookie info in Firefox there is no cookie there.
laura
How are you checking the cookie? Using JS? FF's "Cookies" dialog? An add-on? What version of FF are you using? Your code set a cookie for me when I tested it in FF 3.5.
outis
Yes, using the developer toolbar in FF 3.5.6. Cookies are enabled.Thanks so much for your help.
laura
Are you still having the problem of the cookie not setting? What happens if you step through `setCookie`, examining the cookie string (`name+"="+value+expires+"; path=/"`) and `document.cookies` before and after the last line of the function?
outis