Quote:
Originally Posted by dansgalaxy
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.
|