...

View Full Version : set a checkbox to checked if cookie is detected



mcdougals4all
07-07-2004, 09:33 PM
I'm trying to customize the following script to remember a user's preference with a persistent cookie. The script lets the visitor choose whether certain links open in a new window. Since the doc type is Strict XHTML, the script sets the anchor's target to blank based on it's rel attribute. However, new windows are disabled when the user has checked the checkbox. When checking the checkbox the script also creates a cookie named 'newWindow' with a value of 'disabled'.

I'm able to read the cookie and alert that it's been set, but I've been unable to replace the alert with the necessary code to set the checkbox to checked when reopening the page.

I've tried the following snippet unsuccessfully.


function readIt(name)
{
if (readCookie(name) == 'disabled')
{
document.getElementById("targetform").targetnew.checked = true;
}

Following is the full code:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>
<title></title>


<script type="text/javascript">
//<![CDATA[
function createCookie(name,value,days)
{
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
var ck = name+"="+value+expires+"; path=/";
if (days != -1);
document.cookie = ck;
}

function readCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i<ca.length;i++)
{
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

function eraseCookie(name)
{
createCookie(name,"",-1);
}

function externalLinks()
{
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++)
{
var anchor = anchors[i];
if (anchor.getAttribute("href") &&
anchor.getAttribute("rel") == "external")
anchor.target = "_blank";
var isChecked=document.getElementById("targetform").targetnew.checked;
if(isChecked==true)
{
anchor.target = "";
createCookie('newWindow','disabled',365);
}
else if(isChecked==false)
{
createCookie('newWindow','enabled',365);
}
}
}

function readIt(name)
{
if (readCookie(name) == 'disabled')
{
alert('The DISABLED cookie has been detected');
}
else if (readCookie(name) == 'enabled')
{
alert('The ENABLED cookie has been detected');
}
else
{
alert('No cookie has been detected');
}
}

window.onload = externalLinks;readIt('newWindow');

//]]>
</script>

</head>

<body>

<div>

PDF files open in a new window.

<form id="targetform" action="">
<div>
<input type="checkbox" id="targetnew" onclick="externalLinks(this.checked);" /><label for="targetnew">Disable new windows.</label>
</div>
</form>

<a href="some_file.pdf" rel="external">some PDF file</a>


</div>

</body>
</html>

I'd appreciate any suggestions for refining this javascript, but mainly need help setting the checkbox to checked if the 'disabled' cookie is present. Any help is much appreciated. Thanks!

Willy Duitt
07-07-2004, 11:18 PM
Replace this line: alert('The DISABLED cookie has been detected');

With this line:
document.getElementById("targetform").targetnew.checked = true;

And move your readIt function call to the body onload....
<body onload = readIt('newWindow')>

mcdougals4all
07-07-2004, 11:41 PM
Thank you so much. That's perfect. I never would have thought to try calling the readIt function from the body onload.

If you'd be so kind... why does the externalLinks function need to be called from 'window.onload' versus the readIt function called from 'body onload'? Aren't they being executed when the page loads in both cases? What exactly is happening here?

In any case thanks again for sharing your knowledge.

Willy Duitt
07-07-2004, 11:53 PM
They both can be called from the body but the externalLinks can be called from the window.onload because it is traversing the DOM and therefore each tag is evaluated as the page is loaded.

The readIt function does not use the DOM and therefore you can not access the checkbox element until it is present on the page....

mcdougals4all
07-07-2004, 11:57 PM
Thanks again. Much appreciated.

jamescover
07-08-2004, 02:51 AM
Just for reference, you can also use the click() method:

document.getElementById("targetform").targetnew.click();

-james



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum