[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

getcookie: how to retrieve value of a cookie

Posted on 2004-10-25
4
Medium Priority
?
888 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…

656 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