Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 893
  • Last Modified:

getcookie: how to retrieve value of a cookie

Hi,
trouble in the last line in my code:
document.getElementById('Checkbox1').checked = getCookie('Checkbox1');

I am always getting 'true' for the value of my cookie,
so I guess it is just checking if the cookie exists or not.
I actually want the values TRUE or FALSE
I have a checkbox on my form, when I tick it the value is TRUE for getcookie('Checkbox1'))
when I leave it blank, the value is FALSE for getcookie('Checkbox1')

This is my code:
<html>
<head>
<title>TITLE</title>
</head>
<body onload="getCookie('Checkbox1');">                                    
      <Form name="form1">
      Keep opening automatically:
      <input type="checkbox" name="Checkbox1" value="" ONCLICK="setCookie(this.checked);">  
      <a href="#" onclick="alert(getCookie('Checkbox1'))">Get Cookie!</a>
      </Form>
</body>
</html>
<script>
function setCookie(value)
{
    document.cookie= 'Checkbox1=' + value;
}

function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}
alert(getCookie('Checkbox1'));
document.getElementById('Checkbox1').checked = getCookie('Checkbox1');
</script>
0
geertbro
Asked:
geertbro
1 Solution
 
sajuksCommented:
//try this.The value of true and false is a boolean value and not a string value.
<html>
<head>
<title>TITLE</title>
</head>
<body onload="getCookie('Checkbox1');">                              
     <Form name="form1">
     Keep opening automatically:
     <input type="checkbox" name="Checkbox1" value="" ONCLICK="setCookie(this.checked);">  
     <a href="#" onclick="alert(getCookie('Checkbox1'))">Get Cookie!</a>
     </Form>
</body>
</html>
<script>
function setCookie(value)
{
alert( value)
    document.cookie= 'Checkbox1=' + value;
}

function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

ls_string = getCookie('Checkbox1')
if ( ( ls_string == "false")||( ls_string == null))
{

document.getElementById('Checkbox1').checked = false;
}
else
{

document.getElementById('Checkbox1').checked = true;
}
</script>
0
 
geertbroAuthor Commented:
Some extra point for your EXCELLENT solution !
Many many thanx!!!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now