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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Location
    London, UK
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    document.cookie not working

    I'm trying to get a session cookie to 'remember' the font size from one page to the next, but every time I open a new page, the font size reverts to the original. Here's the code:

    Code:
    function zoomLetter(mysize) {
       var p = document.getElementsByTagName('p');
       for(i=0;i<p.length;i++) {
          if(p[i].style.fontSize) {
             var s = parseInt(p[i].style.fontSize.replace("px",""));
          } else {
             var s = 16;
          }
    //increase font size if mysize==1
    	  if (mysize ==1){
          	if(s!=max) {
             s += 1;
    		 document.cookie="magnif="+s;
          }
    //decrease font size if mysize==-1
    	  } else if(mysize==-1) {
    	   if(s!=min) {
             s -= 1;
    		 document.cookie="magnif="+s;
          }
    	  }
    
    //use previous font size
    	  else if(mysize>11){
    		  s=mysize;
    		  document.cookie="magnif="+s;
    	  }
          p[i].style.fontSize = s+"px"
    	  }   
       }
    
     </script>
    <script type="text/javascript">
     //Get cookie routine by Shelley Powers 
    function get_cookie(Name) {
     var search = Name + "="
     var returnvalue = "";
     if (document.cookie.length > 0) {
     offset = document.cookie.indexOf(search)
     // if cookie exists
     if (offset != -1) { 
    offset += search.length
     // set index of beginning of value
     end = document.cookie.indexOf(";", offset);
     // set index of end of cookie value
     if (end == -1) end = document.cookie.length;
     returnvalue=unescape(document.cookie.substring(offset, end))
     }
     }
     return returnvalue;
     }
    
    if (get_cookie("magnif")!="")
    	zoomLetter(get_cookie("magnif"))
    I can increase and decrease the font size within a page, but the font size doesn't 'carry over' to a new page. Can anyone tell me what I'm doing wrong?

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    You haven't shown the code that changes the font-size; does it set the cookie at the same time?

    You aren't setting an expiry for the cookie. This means that it will expire when the browser closes. I cannot recall whether this also applies if you close the tab or move to a different page - would need to check this.

    You are using unescape() when reading the cookie but your are not storing it using escape(). But these two functions are deprecated and you should use encode.. and decodeURIComponent() instead.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    New to the CF scene
    Join Date
    Jan 2013
    Location
    London, UK
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by AndrewGSW View Post
    You haven't shown the code that changes the font-size; does it set the cookie at the same time?.
    I call the funtion zoomLetter(mysize) (at the very top of the code I posted) from buttons on the page.
    mysize is 1 for increase size and -1 for decrease size. It works fine on each page, but the size isn't 'remembered' when I go to another page.

    Quote Originally Posted by AndrewGSW View Post
    You aren't setting an expiry for the cookie.
    That's because it's a session cookie (which I stated in the original post) which expires when the browser is closed.
    I have another session cookie that's working and doesn't expire between pages. It keeps the (selectable) background colour the same between pages.
    I didn't include the code for that as I thought it would confuse matters.

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,348
    Thanks
    23
    Thanked 618 Times in 617 Posts
    Please notice that your top function is not inside javascript tags and your last function does not have a close tag.

    Your zoomLetter(mysize) function does not work even when inserted corectly. Haven't tried to trouble shoot it cause it don't make sense when you only want to store one of two values. So use this:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>New document</title>
    <script type="text/javascript">
    function zoomLetter(mysize)
    {
    	document.cookie = "magnif = "+mysize;
    }
    </script>
    </head>
    
    <body>
    <input type="button" value="increase" onclick="zoomLetter(1)">
    <input type="button" value="decrease" onclick="zoomLetter(-1)">
    </body>
    </html>

  • #5
    New to the CF scene
    Join Date
    Jan 2013
    Location
    London, UK
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sunfighter View Post
    Please notice that your top function is not inside javascript tags and your last function does not have a close tag.
    Sorry. I missed them out when C&Ping. I also appear to have missed out the following:

    <script type="text/javascript">
    var min=12;
    var max=24;
    function zoomLetter(mysize) {.....


    Your zoomLetter(mysize) function does not work
    It does for me. Calling it as zoomLetter(1) increases the size of the page font by 1px each time, and calling it as zoomLetter(-1) decreases the size of the font by 1px each time.

    Haven't tried to trouble shoot it cause it don't make sense when you only want to store one of two values.
    I want to store all the possible values between 14px and 24px. Each time the user hits one of the increase or decrease size buttons (to call zoomLetter) the font size changes between those values, 1px at a time, up or down. 14 -24 (inclusive) = 11 possible values.

    I've temporarily added another button to change the size to an arbitrary size, 22px, by calling zoomLetter(22), and that also works.

    What doesn't work is the session cookie. Each page opens in the default font size, not the one chosen by the user, even when going back to the page on which the size was originally set

    Another session cookie that keeps track of the background colour, on the same page, does work using the same get_cookie(name) function. Each page opens with the correct user chosen bgcolour.


  •  

    Posting Permissions

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