Connecting Tech Pros Worldwide Forums | Help | Site Map

Cookie Troubles

Familiar Sight
 
Join Date: Sep 2008
Posts: 255
#1: Jul 21 '09
I am creating a cookie when a user clicks on a link and saving a value based on the link clicked, everytime the user goes to a different page i want the JS to check if the cookie already exists and use the value stored in it or if not to then create the cookie and save the selected value. The cookie can also change value based on the different links that the user clicks. Please find below what i have so far:

Expand|Select|Wrap|Line Numbers
  1. function font_size(size) 
  2. {
  3.     var checkCookie = getCookie('testcookie');
  4.     if (checkCookie == null)
  5.     {
  6.         if(size=='small') {
  7.             //document.body.style.fontSize = '10';
  8.             setCookie('10');
  9.         }
  10.         else if (size=='normal')
  11.             document.body.style.fontSize = '12';
  12.         else
  13.             document.body.style.fontSize = '14';
  14.     }
  15. }
  16. function setCookie(size)
  17. {
  18.     var theDate = new Date();
  19.     var oneYearLater = new Date( theDate.getTime() + 31536000000 );
  20.     var expiryDate = oneYearLater.toGMTString();
  21.     document.cookie = 'testcookie=test;expires='+expiryDate+';path=/';
  22. }
  23. function getCookie(cookie_name)
  24. {
  25.     var nameEQ = cookie_name + "=";
  26.     var ca = document.cookie.split(';');
  27.     for(var i=0;i < ca.length;i++) {
  28.         var c = ca[i];
  29.         while (c.charAt(0)==' ') c = c.substring(1,c.length);
  30.         if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  31.     }
  32.     return null;
  33. }

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Jul 21 '09

re: Cookie Troubles


In font_size(), you don't have a case where the cookie is set, and in setCookie, the cookie is always set to "test", not to the size parameter.
Familiar Sight
 
Join Date: Sep 2008
Posts: 255
#3: Jul 22 '09

re: Cookie Troubles


Does this make more sense? Sorry about my lack of JS understanding, never really used it until a few months ago.

Expand|Select|Wrap|Line Numbers
  1. function font_size(size) 
  2. {
  3.     var checkCookie = getCookie('testcookie');
  4.     if (checkCookie == null)
  5.     {
  6.         if(size=='small')
  7.             setCookie('10');
  8.         else if (size=='normal')
  9.             setCookie('12');
  10.         else
  11.             setCookie('14');
  12.     }
  13. }
  14. function setCookie(size)
  15. {
  16.     var theDate = new Date();
  17.     var oneYearLater = new Date( theDate.getTime() + 31536000000 );
  18.     var expiryDate = oneYearLater.toGMTString();
  19.     document.cookie = 'testcookie='+size+';expires='+expiryDate+';path=/';
  20. }
  21. function getCookie(cookie_name)
  22. {
  23.     var nameEQ = cookie_name + "=";
  24.     var ca = document.cookie.split(';');
  25.     for(var i=0;i < ca.length;i++) {
  26.         var c = ca[i];
  27.         while (c.charAt(0)==' ') c = c.substring(1,c.length);
  28.         if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  29.     }
  30.     return null;
  31.  
  32. }
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Jul 22 '09

re: Cookie Troubles


Now you're setting the cookie, but not making use of it. Set the font size to the checkCookie value if it's set, i.e. when it's not null.
Familiar Sight
 
Join Date: Sep 2008
Posts: 255
#5: Jul 22 '09

re: Cookie Troubles


This is what I'm using now as I want the value in the cookie to be updated if the link on the site is clicked again, is this the correct way of doing it?

Now the one thing I've been unable to figure out it how to get the value from the cookie if the cookie exists and apply it to the site on load?
Expand|Select|Wrap|Line Numbers
  1. function font_size(size) 
  2. {
  3.     if(size=='small')
  4.         setCookie('10');
  5.     else if (size=='normal')
  6.         setCookie('12');
  7.     else
  8.         setCookie('14');
  9. }
  10. function setCookie(size)
  11. {
  12.     var currentDate = new Date();
  13.     var expiryLength = new Date(currentDate.getTime() + 31536000000);
  14.     var expiryDate = expiryLength.toGMTString();
  15.     document.cookie = 'testcookie='+size+';expires='+expiryDate+';path=/';
  16. }
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Jul 22 '09

re: Cookie Troubles


Call a checkCookie() function onload, e.g.
Expand|Select|Wrap|Line Numbers
  1. function checkCookie() {
  2.     fontsize=getCookie('testcookie');
  3.     if (fontsize!=null) {
  4.         document.body.style.fontSize = fontsize + "px";
  5.     }
  6. }
Familiar Sight
 
Join Date: Sep 2008
Posts: 255
#7: Jul 22 '09

re: Cookie Troubles


Thanks for that, got it all working fine now, heres my code for anyone in the future.
Expand|Select|Wrap|Line Numbers
  1. function font_size(size) {
  2.     if(size=='small') {
  3.         setCookie('10');
  4.         document.body.style.fontSize = "10px";
  5.     }
  6.     else if (size=='normal') {
  7.         setCookie('12');
  8.         document.body.style.fontSize = "12px";
  9.     }
  10.     else {
  11.         setCookie('14');
  12.         document.body.style.fontSize = "14px";
  13.     }
  14. }
  15. function setCookie(size) {
  16.     var currentDate = new Date();
  17.     var expiryLength = new Date(currentDate.getTime() + 31536000000);
  18.     var expiryDate = expiryLength.toGMTString();
  19.     document.cookie = 'testcookie='+size+';expires='+expiryDate+';path=/';
  20. }
  21. function getCookie(cookie_name) {
  22.     var nameEQ = cookie_name + "=";
  23.     var ca = document.cookie.split(';');
  24.     for(var i=0;i < ca.length;i++) {
  25.         var c = ca[i];
  26.         while (c.charAt(0)==' ') c = c.substring(1,c.length);
  27.         if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  28.     }
  29.     return null;
  30.  
  31. }
  32. function checkCookie() {
  33.     fontsize = getCookie('testcookie');
  34.         if (fontsize!=null) {
  35.         document.body.style.fontSize = fontsize + "px";
  36.     }
  37. }
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#8: Jul 22 '09

re: Cookie Troubles


No problem, glad to help :)
Familiar Sight
 
Join Date: Sep 2008
Posts: 255
#9: Jul 23 '09

re: Cookie Troubles


Quick question in relation to this, instead of hardcoding the font sizes like 10,12 and 14 can you just say if size is set to normal(12px) add 2px onto all font-sizes on the webpage or is it very difficult to do as there will be many different font sizes on the webpage?
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#10: Jul 23 '09

re: Cookie Troubles


You could do that, but you'd need to get all the separate elements with the different sizes and add 2px, e.g.:
Expand|Select|Wrap|Line Numbers
  1. document.body.style.fontSize = (parseInt(document.body.style.fontSize) + 2) + "px";
Reply


Similar JavaScript / Ajax / DHTML bytes