Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 526469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:46:49+00:00 2026-05-13T08:46:49+00:00

I am learning, please be kind if it is an obvious mistake. /*set cookie*/

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:46:49+00:00Added an answer on May 13, 2026 at 8:46 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.