Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

getcookie: how to retrieve value of a cookie

Posted on 2004-10-25
4
Medium Priority
?
891 Views
Last Modified: 2006-11-17
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
Comment
Question by:geertbro
2 Comments
 
LVL 33

Accepted Solution

by:
sajuks earned 600 total points
ID: 12398728
//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
 
LVL 1

Author Comment

by:geertbro
ID: 12398740
Some extra point for your EXCELLENT solution !
Many many thanx!!!!!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question