Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 07-07-2004, 10:33 PM   PM User | #1
mcdougals4all
Regular Coder

 
Join Date: Jul 2004
Location: mile high city
Posts: 482
Thanks: 0
Thanked 0 Times in 0 Posts
mcdougals4all is an unknown quantity at this point
set a checkbox to checked if cookie is detected

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.

Code:
function readIt(name)
          {
          if (readCookie(name) == 'disabled')
            {
            document.getElementById("targetform").targetnew.checked = true;
            }
Following is the full code:

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!
mcdougals4all is offline   Reply With Quote
Old 07-08-2004, 12:18 AM   PM User | #2
Willy Duitt
Banned

 
Join Date: Sep 2003
Posts: 3,620
Thanks: 0
Thanked 0 Times in 0 Posts
Willy Duitt is an unknown quantity at this point
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')>
Willy Duitt is offline   Reply With Quote
Old 07-08-2004, 12:41 AM   PM User | #3
mcdougals4all
Regular Coder

 
Join Date: Jul 2004
Location: mile high city
Posts: 482
Thanks: 0
Thanked 0 Times in 0 Posts
mcdougals4all is an unknown quantity at this point
Thank you!

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.
mcdougals4all is offline   Reply With Quote
Old 07-08-2004, 12:53 AM   PM User | #4
Willy Duitt
Banned

 
Join Date: Sep 2003
Posts: 3,620
Thanks: 0
Thanked 0 Times in 0 Posts
Willy Duitt is an unknown quantity at this point
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....
Willy Duitt is offline   Reply With Quote
Old 07-08-2004, 12:57 AM   PM User | #5
mcdougals4all
Regular Coder

 
Join Date: Jul 2004
Location: mile high city
Posts: 482
Thanks: 0
Thanked 0 Times in 0 Posts
mcdougals4all is an unknown quantity at this point
Thanks again. Much appreciated.
mcdougals4all is offline   Reply With Quote
Old 07-08-2004, 03:51 AM   PM User | #6
jamescover
Regular Coder

 
Join Date: Aug 2002
Location: USA
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
jamescover is an unknown quantity at this point
Just for reference, you can also use the click() method:

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

-james
__________________
"God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "
jamescover is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:15 PM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.