www.webdeveloper.com
Results 1 to 9 of 9

Thread: Cookies issue

  1. #1
    Join Date
    Jan 2011
    Posts
    16

    Unhappy Cookies issue

    What is wrong with this code so that it is not getting any cookies.
    Code:
    var nameEQ = donatehide + "=";                             //I'm going to search for the name of the cookie, followed by an =. So create this new string and put it in nameEQ
    var ca = document.cookie.split(';');                       //Then split document.cookie on semicolons. ca becomes an array containing all cookies that are set for this domain and path.
    for(var i=0;i < ca.length;i++) {                           //Then go through the array (so through all cookies)
    	var c = ca[i];                                          //Set c to the cookie to be checked.
    	while (c.charAt(0)==' ') c = c.substring(1,c.length);   //If the first character is a space, remove it by using the substring() method. Continue doing this until the first character is not a space.
    	if (c.indexOf(nameEQ) == 0)                             //Now string c begins with the name of the current cookie. If this is the name of the desired cookie...
    	{ 
    	var donatehide = c.substring(nameEQ.length,c.length);   //...we've found what we were looking for. We now only need to return the value of the cookie, which is the part of c that comes after nameEQ. By returning this value we also end the function: mission accomplished.
    	}
    	}
    if (!donatehide)
    {
    var donatehide = 0
    }
    
    
    
    
    
    
    function hidedonate()
    {
    if ($("#panel").hasClass("donatepanel"))
    {
    	$('#panel').animate({right: "-145"}, 500)
    	$("#panel").removeClass("donatepanel").addClass("donatepanelhide")
    	document.cookie = "donatehide=0; expires=30; path=/"
    }
    else
    {
    	$('#panel').animate({right: "-1"}, 500)
    	$("#panel").removeClass("donatepanelhide").addClass("donatepanel")
    	document.cookie = "donatehide=0; expires=30; path=/"
    }
    }

  2. #2
    Join Date
    Jan 2011
    Posts
    16
    hidedonate() is run whenever I click a button to hide a 'donate' box. I can see that it is running because the box moves. I am also using jquery (hence the 'animate'). When donatehide = 1 the box will have a class change so that it is immediately hidden and I want to make these settings stay via cookies

  3. #3
    Join Date
    Jan 2011
    Posts
    16
    I am currently using the script further down, beneath the donate box
    Code:
    <script type="text/javascript" >
    if (donatehide != 0)
    {
    	$("#panel").removeClass("donatepanel").addClass("donatepanelhide")
    }
    </script>
    Which hides it the box. I am using if (donatehide != 0) instead of if (donatehide = 1) because I tried that and it redefined donatehide to 1 every time

  4. #4
    Join Date
    Dec 2010
    Posts
    207
    If you split document.cookie, you should split on "; " rather than ";".

    The expires parameter has to be in a particular format, generated by the date.toUTCString method. Your value is probably setting a zero-lifetime cookie.

    G.I.Y.F.


    Q: I found this code...
    A: Then find its author.

  5. #5
    Join Date
    Jan 2011
    Posts
    16
    Quote Originally Posted by clueful View Post
    If you split document.cookie, you should split on "; " rather than ";".

    The expires parameter has to be in a particular format, generated by the date.toUTCString method. Your value is probably setting a zero-lifetime cookie.

    G.I.Y.F.
    I don't think I did. The script here is copied and pasted directly from my script and it doesn't have that mistake in it.

    Edit: ignore this comment I read your message wrong
    Last edited by sAuhsoj; 04-02-2011 at 09:51 AM.

  6. #6
    Join Date
    Jan 2011
    Posts
    16
    thanks, but that still doesn't work though.

  7. #7
    Join Date
    Dec 2010
    Posts
    207
    Quote Originally Posted by sAuhsoj View Post
    thanks, but that still doesn't work though.
    So what do you have now?


    Q: I found this code...
    A: Then find its author.

  8. #8
    Join Date
    Jan 2011
    Posts
    16
    the exact same thing but the "; " instead of ";"
    every time I load the page donatehide = 0

  9. #9
    Join Date
    Dec 2010
    Posts
    207
    Quote Originally Posted by sAuhsoj View Post
    the exact same thing but the "; " instead of ";"
    every time I load the page donatehide = 0
    You still have to fix the setting of the cookie.
    Code:
    var dt = new Date();
    
    dt.setDate( dt.getDate() + 30 );
    
    document.cookie = "donatehide=0; expires=" + dt.toUTCString() + "; path=/";


    Q: I found this code...
    A: Then find its author.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles