Reply
how to add cookie to text enlarger script
Old 12-18-2007, 03:00 PM how to add cookie to text enlarger script
dansgalaxy's Avatar
Eat, Sleep, Code

Latest Blog Post:
New College Enrollment.
Posts: 5,805
Name: Dan
Location: Swindon
hey all,

can someone help me add cookie support to this text enlarger script i have please

Code:
var min=8;
var max=24;
function increaseFontSize() {
   var p = document.getElementsByTagName('body');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 14;
      }
      if(s!=max) {
         s += 1;
      }
      p[i].style.fontSize = s+"px"
   }
}
function decreaseFontSize() {
   var p = document.getElementsByTagName('body');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 14;
      }
      if(s!=min) {
         s -= 1;
      }
      p[i].style.fontSize = s+"px"
   }   
}
can someone tell me how and where to add whatever to save it in a cookie so it works from page to page and dont have to resize text on page refresh or change.

also how could i add a reset button which just sets to default?

Thanks,
TP for working/helpful answers
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
When You Register, These Ads Go Away!
     
Old 12-23-2007, 01:44 PM Re: how to add cookie to text enlarger script
Junior Talker

Posts: 4
Name: Jesse Skinner
Quote:
Originally Posted by dansgalaxy View Post
can someone help me add cookie support to this text enlarger script i have please
Basically you just need to use a couple of functions which let you set and retrieve cookies. When you change the font size, record the change. When the page loads, see if a cookie is set and call the appropriate function.

Code:
/* cookie functions from QuirksMode */
function setCookie (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 = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie (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 increaseFontSize() {
   .. same as before ..

   // save the font size in a cookie for 25 years
   setCookie('fontsize', s, 365 * 25);
}

function decreaseFontSize() {
   .. same as before ..
    
   // save the font size in a cookie for 25 years
   setCookie('fontsize', s, 365 * 25);
}

window.onload = function() {
    // if the cookie exists, restore the variable
    var fontsize = getCookie('fontsize');
    if (fontsize) {
        document.getElementsByTagName('body')[0].style.fontSize = fontsize + 'px'
    }
}
You'll probably want to use something like addDOMLoadEvent so that the font size kicks in instantly instead of waiting for images to load. Or you could try using document.write to output a <style> element.
__________________
The Future of the Web - JavaScript, Ajax, CSS, HTML and anything else of interest to standards-loving web designers and developers.
jesse is offline
Reply With Quote
View Public Profile
 
Old 12-23-2007, 02:26 PM Re: how to add cookie to text enlarger script
dansgalaxy's Avatar
Eat, Sleep, Code

Latest Blog Post:
New College Enrollment.
Posts: 5,805
Name: Dan
Location: Swindon
hey, sorry i probably being thick but its not working?

i really am crap at javascript so am a bit clueless is this what you ment?

Code:
/* cookie functions from QuirksMode */
function setCookie (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 = "";
    document.cookie = name+"="+value+expires+"; path=/";
}
function getCookie (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;
}
var min=8;
var max=24;
function increaseFontSize() {
   var p = document.getElementsByTagName('body');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 14;
      }
      if(s!=max) {
         s += 1;
      }
      p[i].style.fontSize = s+"px"
   }
      // save the font size in a cookie for 25 years
   setCookie('fontsize', s, 365 * 25);
}
function decreaseFontSize() {
   var p = document.getElementsByTagName('body');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 14;
      }
      if(s!=min) {
         s -= 1;
      }
      p[i].style.fontSize = s+"px"
   }  
      // save the font size in a cookie for 25 years
   setCookie('fontsize', s, 365 * 25);
}
Thanks,
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 12-25-2007, 03:56 PM Re: how to add cookie to text enlarger script
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,025
Name: Forrest Croce
Location: Seattle, WA
What happens? Are you getting a js error? It's just not doing what you're expecting?

The code looks pretty similar to what I'm doing for my navigation.
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 12-25-2007, 04:49 PM Re: how to add cookie to text enlarger script
dansgalaxy's Avatar
Eat, Sleep, Code

Latest Blog Post:
New College Enrollment.
Posts: 5,805
Name: Dan
Location: Swindon
Well it works but it isnt saving to so it carries on to the next pages etc so if i was impared or whatever i would have to be increases the text every page...

i think it might need to have a if cookie set use var else use default but im clueles on how to implement help will be reward with all be it hardly any tp but what i can give!
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 12-27-2007, 09:31 AM Re: how to add cookie to text enlarger script
Junior Talker

Posts: 4
Name: Jesse Skinner
Are you missing the window.onload function?
__________________
The Future of the Web - JavaScript, Ajax, CSS, HTML and anything else of interest to standards-loving web designers and developers.
jesse is offline
Reply With Quote
View Public Profile
 
Old 12-27-2007, 10:58 AM Re: how to add cookie to text enlarger script
dansgalaxy's Avatar
Eat, Sleep, Code

Latest Blog Post:
New College Enrollment.
Posts: 5,805
Name: Dan
Location: Swindon
No clue, sorry can you guide me through it?

Do you have MSN or a IM would be easier than this

Thanks, and btw happy to give reconition on the site for it!

and TP!
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 12-28-2007, 05:33 PM Re: how to add cookie to text enlarger script
dansgalaxy's Avatar
Eat, Sleep, Code

Latest Blog Post:
New College Enrollment.
Posts: 5,805
Name: Dan
Location: Swindon
why dont this work?
i tried this to get the cookie and do it but im just crapy at javascript

Code:
<style type="text/css">
<script type="text/javascript">
var s;
if(getCookie(name) === TRUE)
{
document.write('
body{ font-size: +s+; }
'); 
}
</script>
</style>
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Reply     « Reply to how to add cookie to text enlarger script
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.15265 seconds with 13 queries