Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2004
    Location
    mile high city
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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')>

  • #3
    Regular Coder
    Join Date
    Jul 2004
    Location
    mile high city
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #4
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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....

  • #5
    Regular Coder
    Join Date
    Jul 2004
    Location
    mile high city
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks again. Much appreciated.

  • #6
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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. "


  •  

    Posting Permissions

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