Results 1 to 3 of 3

Thread: Trying to save cookie for checkbox

  1. #1
    Join Date
    Jul 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Trying to save cookie for checkbox

    Hi everyone,

    I have a page in which I have a check box and some links. The check box reads "Open in New Window" and, when checked, will open the links in a new window. I want to save this setting so the user can set it once and not have to keep setting it each time they visit.

    I have following JSP functions to save cookies (which I've tested and do work):

    Code:
      function setCookie(c_name,value,expiredays) {
            var exdate=new Date()
            exdate.setDate(exdate.getDate()+expiredays)
            document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate)
        }
    
        function getCookie(c_name) {
            if (document.cookie.length>0) {
                c_start=document.cookie.indexOf(c_name + "=")
                if (c_start!=-1) { 
                    c_start=c_start + c_name.length+1 
                    c_end=document.cookie.indexOf(";",c_start)
                    if (c_end==-1) c_end=document.cookie.length
                        return unescape(document.cookie.substring(c_start,c_end))
                } 
        	}
            return null
        }
    I have the following lines of code to save and load the cookies for the check boxes:

    Code:
    // Load cookie
    document.getElementById("linksNewWindow").setAttribute('checked', getCookie("linksNewWindow"))	
    
    // Save cookie
    setCookie("linksNewWindow", document.getElementById("linksNewWindow").checked, 100);
    However, when I reload the page, the check box always appears as checked, even if I leave the page and save the cookie. Is there something simple that I am missing? My cookies for text boxes work but not for check boxes.

    Thanks for any help.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The value of a cookie is set as a string (it can be a number, but it will then be treated as a number that is a string), but you were setting it as a boolean (true or false). Also, in many browsers, using setAttribute() to set the value of a boolean tag attribute results in its being seen as true, no matter what the passed value is.

    This worked out fairly well:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
      function setCookie(c_name,value,expiredays) {
            var exdate=new Date()
            exdate.setDate(exdate.getDate()+expiredays)
            document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate)
        }
    
        function getCookie(c_name) {
            if (document.cookie.length>0) {
                c_start=document.cookie.indexOf(c_name + "=")
                if (c_start!=-1) { 
                    c_start=c_start + c_name.length+1 
                    c_end=document.cookie.indexOf(";",c_start)
                    if (c_end==-1) c_end=document.cookie.length
                        return unescape(document.cookie.substring(c_start,c_end))
                } 
        	}
            return null
        }
    onload=function(){
    document.getElementById('linksNewWindow').checked = getCookie('linksNewWindow')==1? true : false;
    }
    function set_check(){
    setCookie('linksNewWindow', document.getElementById('linksNewWindow').checked? 1 : 0, 100);
    }
    </script>
    </head>
    <body>
    <div>Hi</div>
    <input type="checkbox" id="linksNewWindow" onchange="set_check();">
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jul 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the help. That works wonderfully.

    What I was doing was something very similar, just not as neatly coded as your solution:

    Code:
    // Load cookie
    if (getCookie("linksNewWindow") == "1")
    	linksNewWindow.checked = true;
    else
    	linksNewWindow.checked = false;	
    
    // Save cookie
    if (linksNewWindow.checked)
           	setCookie("linksNewWindow", "1", 100)
    else
          	setCookie("linksNewWindow", "0", 100)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •