PDA

View Full Version : Trying to save cookie for checkbox



Annorax
07-20-2007, 11:56 PM
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):


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:


// 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. :)

jscheuer1
07-21-2007, 08:30 AM
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:


<!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>

Annorax
07-21-2007, 02:02 PM
Thanks for the help. That works wonderfully. :)

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


// 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)