473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Cookie Troubles

384 256MB
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. }
Jul 21 '09 #1
9 1907
acoder
16,027 Expert Mod 8TB
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.
Jul 21 '09 #2
ziycon
384 256MB
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. }
Jul 22 '09 #3
acoder
16,027 Expert Mod 8TB
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.
Jul 22 '09 #4
ziycon
384 256MB
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. }
Jul 22 '09 #5
acoder
16,027 Expert Mod 8TB
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. }
Jul 22 '09 #6
ziycon
384 256MB
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. }
Jul 22 '09 #7
acoder
16,027 Expert Mod 8TB
No problem, glad to help :)
Jul 22 '09 #8
ziycon
384 256MB
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?
Jul 23 '09 #9
acoder
16,027 Expert Mod 8TB
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";
Jul 23 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ben Hearsum | last post by:
I'm confused about the way Cookie.load outputs cookies. Is there a way I can tell it to give me the value of a key? I set my cookie like this: mycookie = Cookie.SimpleCookie() mycookie =...
5
by: brettr | last post by:
When I reference document.cookie, there is a long string of key=value; pairs listed. I may have 100 hundred cookies on my hard drive. However, most only have one key=value pair. Does the...
4
by: socialism001 | last post by:
I'm trying to store a value in a cookie but its not working. Can anyone see what I might be doing wrong. Thanks, Chris ~~~~~~~~~~~~~~~~~~ <script language="javascript">...
9
by: Marco Krechting | last post by:
Hi All, I have a page with a list of hyperlinks. I want to save information in a cookie about the fact that I entered an hyperlink or not. When I click one of the hyperlinks I want this stored...
7
by: ehendrikd | last post by:
hi all i need some clarification on how the php session work in relation to cookies. we have a web site where users need to log in. a few of our users were having troubles with their browser...
1
by: Owen | last post by:
I have a web app that is a mixture of ASP and ASP.NET pages. Largely the only data passed between them is via the querystring, or by reading from a database. However there is a requirement for...
0
by: csgraham74 | last post by:
Hi, Im trying to make changes to an appliction that i inherited. i have set several cookies with different small values from XML. i get to a piece of code where i response.redirect to another...
2
by: RootShell | last post by:
I have been experiencing a lot of troubles in reading a PHP COOKIE from javascript. if i write the cookie and read it in javascript in the same PHP file it works well, but if i write the cookie...
4
by: gcervantes | last post by:
Hello, I am making a screen scraper, in other programs I have been able to log into a site, get the cookie, submit a form and read the results. However, this time I am in troubles, the site I need...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.