<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The html blog &#187; css font dom</title>
	<atom:link href="http://htmlblog.net/tag/css-font-dom/feed/" rel="self" type="application/rss+xml" />
	<link>http://htmlblog.net</link>
	<description>The web sandbox of Asvin Balloo</description>
	<lastBuildDate>Tue, 09 Nov 2010 11:39:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Simple YUI Cookie example &#8211; javascript text magnifier for your page</title>
		<link>http://htmlblog.net/simple-yui-cookie-example/</link>
		<comments>http://htmlblog.net/simple-yui-cookie-example/#comments</comments>
		<pubDate>Mon, 30 Jun 2008 06:11:16 +0000</pubDate>
		<dc:creator>asvin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[cookie]]></category>
		<category><![CDATA[css font dom]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[magnifier]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[xhtml]]></category>
		<category><![CDATA[yahoo]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://htmlblog.net/?p=6</guid>
		<description><![CDATA[Since YUI 2.5.0 there&#8217;s the Cookie utility and at this time of posting, it&#8217;s still in beta version. I&#8217;ll show you how we can use this simple utility to write a small javascript app. The piece of code we will be writing, using the YUI library (my favorite), will allow users to increase/decrease their font [...]]]></description>
			<content:encoded><![CDATA[<p>Since <a href="http://developer.yahoo.com/yui/">YUI 2.5.0</a> there&#8217;s the <a href="http://developer.yahoo.com/yui/cookie/">Cookie utility</a> and at this time of posting, it&#8217;s still in beta version.<br />
I&#8217;ll show you how we can use this simple utility to write a small javascript app.</p>
<p>The piece of code we will be writing, using the YUI library (my favorite), will allow users to increase/decrease their font size for a block of text, storing the font size in a cookie<br />
so that when they return to the page they don&#8217;t have to modify the font size again.<br />
<span id="more-6"></span><br />
<strong><a href="http://orange.mu/kinews/afp/football/204126/spain-put-their-quot-black-legend-quot-to-bed-with-euro-win.html">You can view a live demo here.</a></strong></p>
<h3>Getting started.</h3>
<p>Our page will consist of a block of text, along with 2 images. One to increase the font size and the other one to decrease it.<br />
The images we&#8217;ll be using :</p>
<p><a href='http://htmlblog.net/wp-content/uploads/2008/06/a.gif'><img src="http://htmlblog.net/wp-content/uploads/2008/06/a.gif" alt="" title="a" width="20" height="16" class="alignnone size-full wp-image-7" /></a></p>
<p><a href='http://htmlblog.net/wp-content/uploads/2008/06/a1.gif'><img src="http://htmlblog.net/wp-content/uploads/2008/06/a1.gif" alt="" title="a1" width="20" height="16" class="alignnone size-full wp-image-8" /></a></p>
<div style="clear: both;"></div>
<p>So the HTML goes like that :</p>
<pre class="brush: xhtml">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;en&quot; lang=&quot;en&quot;&gt;
	&lt;head&gt;
		&lt;title&gt;yui cookie&lt;/title&gt;
		&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;div&gt;&lt;img src=&quot;A+.gif&quot; alt=&quot;increase font size&quot; id=&quot;increaseFont&quot; /&gt;&lt;/div&gt;
		&lt;div&gt;&lt;img src=&quot;A-.gif&quot; alt=&quot;decrease font size&quot; id=&quot;decreaseFont&quot; /&gt;&lt;/div&gt;
		&lt;div id=&quot;news-main&quot;&gt;
			Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sit amet metus.
Nunc quam elit, posuere nec, auctor in, rhoncus quis, dui. Aliquam erat volutpat. Ut dignissim, massa 	&lt;/div&gt;
	&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>We define unique ids for DOM manipulation afterwards.</p>
<p>Next we add the necessary YUI libraries. We will be using</p>
<ul>
<li><a href="http://developer.yahoo.com/yui/articles/hosting/#configure">yahoo-dom-event.js (aggregated yahoo, dom, event js)</a></li>
<li><a href="http://developer.yahoo.com/yui/articles/hosting/#configure">cookie-beta.js</a></li>
</ul>
<p>hosted at the geolocated Yahoo servers .</p>
<p>We add these two lines of code at the end of the file, before the closing body tag, <a href="http://developer.yahoo.com/performance/rules.html">to maximise performance</a> (more on that in a later post).</p>
<pre class="brush: xhtml">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;en&quot; lang=&quot;en&quot;&gt;
	&lt;head&gt;
		&lt;title&gt;yui cookie&lt;/title&gt;
		&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;div&gt;&lt;img src=&quot;A+.gif&quot; alt=&quot;increase font size&quot; id=&quot;increaseFont&quot; /&gt;&lt;/div&gt;
		&lt;div&gt;&lt;img src=&quot;A-.gif&quot; alt=&quot;decrease font size&quot; id=&quot;decreaseFont&quot; /&gt;&lt;/div&gt;
		&lt;div id=&quot;news-main&quot;&gt;
			Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sit amet metus.
Nunc quam elit, posuere nec, auctor in, rhoncus quis, dui. Aliquam erat volutpat. Ut dignissim, massa
		&lt;/div&gt;

		&lt;!-- js --&gt;
		&lt;script type=&quot;text/javascript&quot; src=&quot;http://yui.yahooapis.com/2.5.2/build/yahoo-dom-event/yahoo-dom-event.js&quot;&gt;&lt;/script&gt;
		&lt;script type=&quot;text/javascript&quot; src=&quot;http://yui.yahooapis.com/2.5.2/build/cookie/cookie-beta-min.js&quot;&gt;&lt;/script&gt;
		&lt;!-- /js --&gt;
	&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Our page is almost ready we just have to write the code to update the font size. The javascript code goes like this,</p>
<pre class="brush: javascript">
var updateFontSize = function(o, params){
	try{
		if(params.type == &#039;increase&#039;){
			newSize = parseInt(YAHOO.util.Dom.getStyle(params.id, &#039;font-size&#039;), 10) + 1;
		}
		else{
			newSize = parseInt(YAHOO.util.Dom.getStyle(params.id, &#039;font-size&#039;), 10) - 1;
		}

		YAHOO.util.Dom.setStyle(params.id, &#039;font-size&#039;, newSize + &#039;px&#039;);
		YAHOO.util.Cookie.set(&quot;news-size&quot;, newSize, { path: &quot;/&quot;, expires: new Date(&quot;December 22, 2010&quot;) });
	}
	catch(e){}
};

YAHOO.util.Event.addListener(&#039;increaseFont&#039;, &#039;click&#039;, updateFontSize, {&quot;id&quot; : &#039;news-main&#039;, &quot;type&quot; : &#039;increase&#039;});
YAHOO.util.Event.addListener(&#039;decreaseFont&#039;, &#039;click&#039;, updateFontSize, {&quot;id&quot; : &#039;news-main&#039;, &quot;type&quot; : &#039;decrease&#039;});
YAHOO.util.Event.addListener(window, &#039;load&#039;, function(){
	try{
		var newsSize = YAHOO.util.Cookie.get(&quot;news-size&quot;, function(value){
	        	YAHOO.util.Dom.setStyle(&#039;news-main&#039;, &#039;font-size&#039;, value + &#039;px&#039;);
		});
	}
	catch(e){}
});
</pre>
<h3>Now we let us have an extensive look at the code above.</h3>
<p>We define a function updateFontSize which takes 2 parameters, the second one being json data we will be passing using YUI <a href="http://developer.yahoo.com/yui/event/">addListener </a>method.<br />
Line 3 checks whether we want to increase or decrease the font size. In any case we get the current font size of the text using<br />
<a href="http://developer.yahoo.com/yui/dom/">YAHOO.util.Dom.getStyle </a>method which will return a value of 11px for example. We then use the <a href="http://www.w3schools.com/jsref/jsref_parseInt.asp">parseInt</a> function to return only the integer so that<br />
we can increase/decrease it by 1;</p>
<p>After getting the new size in the newSize variable, we just apply it to the block of text using the YAHOO.util.Dom.setStyle method :</p>
<pre class="brush: javascript">
YAHOO.util.Dom.setStyle(params.id, &#039;font-size&#039;, newSize + &#039;px&#039;);
</pre>
<p>the params.id being the id on which we want the style to be applied on and passed through at line 16/17 above.</p>
<p>Line 11 just sets the cookie for future visits, giving it a name and an expiration date.</p>
<p>The whole block is surrounded by <a href="http://www.w3schools.com/JS/js_try_catch.asp">try catch statement</a>.</p>
<h3>Listeners</h3>
<p>In line 16 and 17 we add event listeners to the respective images, one to increase the font size, the other one to decrease it</p>
<pre class="brush: javascript">
YAHOO.util.Event.addListener(&#039;increaseFont&#039;, &#039;click&#039;, updateFontSize, {&quot;id&quot; : &#039;news-main&#039;, &quot;type&quot; : &#039;increase&#039;});
YAHOO.util.Event.addListener(&#039;decreaseFont&#039;, &#039;click&#039;, updateFontSize, {&quot;id&quot; : &#039;news-main&#039;, &quot;type&quot; : &#039;decrease&#039;});
</pre>
<p>For simple understanding the above code just says when someone click on the increaseFont id, call the updateFontSize function,<br />
with the id and the type as parameters. The parameter is the <a href="http://www.json.org/js.html">JSON</a> data I was talking earlier.</p>
<p>Line 18 says when the after the window has loaded completely, we try to get the news-size cookie. </p>
<pre class="brush: javascript">
YAHOO.util.Event.addListener(window, &#039;load&#039;, function(){
	try{
		var newsSize = YAHOO.util.Cookie.get(&quot;news-size&quot;, function(value){
</pre>
<p>If the latter doesn&#8217;t exist, it<br />
will return false, else it returns the value in it, and we just take the value and apply it to the block of text via the YAHOO.util.Dom.setStyle method.</p>
<pre class="brush: javascript">
var newsSize = YAHOO.util.Cookie.get(&quot;news-size&quot;, function(value){
	YAHOO.util.Dom.setStyle(&#039;news-main&#039;, &#039;font-size&#039;, value + &#039;px&#039;);
}
</pre>
<p>As always we surround the block with try catch statements.</p>
<h3>Our final code looks that that :</h3>
<pre class="brush: xhtml">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;en&quot; lang=&quot;en&quot;&gt;
	&lt;head&gt;
		&lt;title&gt;yui cookie&lt;/title&gt;
		&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;div&gt;&lt;img src=&quot;A+.gif&quot; alt=&quot;increase font size&quot; id=&quot;increaseFont&quot; /&gt;&lt;/div&gt;
		&lt;div&gt;&lt;img src=&quot;A-.gif&quot; alt=&quot;decrease font size&quot; id=&quot;decreaseFont&quot; /&gt;&lt;/div&gt;
		&lt;div id=&quot;news-main&quot;&gt;
			Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sit amet metus.
Nunc quam elit, posuere nec, auctor in, rhoncus quis, dui. Aliquam erat volutpat. Ut dignissim, massa
		&lt;/div&gt;

		&lt;!-- js --&gt;
		&lt;script type=&quot;text/javascript&quot; src=&quot;http://yui.yahooapis.com/2.5.2/build/yahoo-dom-event/yahoo-dom-event.js&quot;&gt;&lt;/script&gt;
		&lt;script type=&quot;text/javascript&quot; src=&quot;http://yui.yahooapis.com/2.5.2/build/cookie/cookie-beta-min.js&quot;&gt;&lt;/script&gt;
		&lt;!-- /js --&gt;

		&lt;script type=&quot;text/javascript&quot;&gt;
		var updateFontSize = function(o, params){
			try{
				if(params.type == &#039;increase&#039;){
					newSize = parseInt(YAHOO.util.Dom.getStyle(params.id, &#039;font-size&#039;), 10) + 1;
				}
				else{
					newSize = parseInt(YAHOO.util.Dom.getStyle(params.id, &#039;font-size&#039;), 10) - 1;
				}

				YAHOO.util.Dom.setStyle(params.id, &#039;font-size&#039;, newSize + &#039;px&#039;);
				YAHOO.util.Cookie.set(&quot;news-size&quot;, newSize, { path: &quot;/&quot;, expires: new Date(&quot;December 22, 2010&quot;) });
			}
			catch(e){}
		};

		YAHOO.util.Event.addListener(&#039;increaseFont&#039;, &#039;click&#039;, updateFontSize, {&quot;id&quot; : &#039;news-main&#039;, &quot;type&quot; : &#039;increase&#039;});
		YAHOO.util.Event.addListener(&#039;decreaseFont&#039;, &#039;click&#039;, updateFontSize, {&quot;id&quot; : &#039;news-main&#039;, &quot;type&quot; : &#039;decrease&#039;});
		YAHOO.util.Event.addListener(window, &#039;load&#039;, function(){
			try{
				var newsSize = YAHOO.util.Cookie.get(&quot;news-size&quot;, function(value){
	        			YAHOO.util.Dom.setStyle(&#039;news-main&#039;, &#039;font-size&#039;, value + &#039;px&#039;);
				});
			}
			catch(e){}
		});
		&lt;/script&gt;
	&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Of course all this can be done with using the YUI library, but it&#8217;s my fav. ;-)</p>
]]></content:encoded>
			<wfw:commentRss>http://htmlblog.net/simple-yui-cookie-example/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

