Solved

getcookie: how to retrieve value of a cookie

Posted on 2004-10-25
884 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
Question by:geertbro
    2 Comments
     
    LVL 33

    Accepted Solution

    by:
    //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
    Some extra point for your EXCELLENT solution !
    Many many thanx!!!!!
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Shellfire Box VPN + Lifetime Subscription

    The Shellfire Box easily connects all of your devices, even those that don't offer the possibility to establish a safe vpn connection. Access blocked content and surf safely, no matter where in the world you are located.

    Suggested Solutions

    Both jQuery and Mootools offer some nice, easy to use scripts. Mootools is best known for their interactive slideshows and jQuery for their menus, but both have many other uses as well.   jQuery is basically a toolkit with many DOM (http://en.wik…
    This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
    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…

    934 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now