Nomadic Functions
Where selfishness is good™
blog
code
code
/
dynamic fonts
Once the font size is selected, a cookie is set. The next time the user comes to the page the font will be the same size they left it.
Example:
larger
smaller
JAVASCRIPT
function $(v) { return(document.getElementById(v)); } function cookieStab(f,v) { document.cookie=f+'='+v+'; path=/'; } function cookieGrab(f) { var c=document.cookie.split(';'); f=f+"="; for(var i in c) { var v=trim(c[i]); if(v.indexOf(f)==0) { return(v.substr(f.length,v.length)); } } } function mkStyle(v,s) { v=zero(v); if(v) { var b=!s?(zero($('test').style.fontSize)+v):v; $('test').style.fontSize=b+'px'; cookieStab('fontSize',b); } } function trim(v) { return(v.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"")); } function zero(v) { v=parseInt(v); return(!isNaN(v)?v:0); } window.onload=function() { mkStyle(cookieGrab('fontSize'),1); };
HTML
<div id="test"> <a href="#" onclick="mkStyle(1); return false;" title="increase font size">larger</a> <a href="#" onclick="mkStyle(-1); return false;" title="decrease font size">smaller</a> </div>
Have ideas for code changes?
CC
·
GPL
·
NoFunc.com