<?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; dom</title>
	<atom:link href="http://htmlblog.net/tag/dom/feed/" rel="self" type="application/rss+xml" />
	<link>http://htmlblog.net</link>
	<description>The web sandbox of Asvin Balloo</description>
	<lastBuildDate>Wed, 07 Oct 2009 17:20:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Writing your first Greasemonkey script &#8211; adding menus to Facebook&#8217;s top menu</title>
		<link>http://htmlblog.net/writing-your-first-greasemonkey-script-adding-menus-to-facebooks-top-menu/</link>
		<comments>http://htmlblog.net/writing-your-first-greasemonkey-script-adding-menus-to-facebooks-top-menu/#comments</comments>
		<pubDate>Thu, 30 Apr 2009 19:25:50 +0000</pubDate>
		<dc:creator>asvin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[greasemonkey]]></category>

		<guid isPermaLink="false">http://htmlblog.net/?p=120</guid>
		<description><![CDATA[This post will show you how to write a basic Greasemonkey script that will add two more menu items to Facebook's friends top menu as shown below.

These 2 menu items are :

"Recently Updated" - show all your friends with recent updates
"Status Updates" - show the latest status updates of your friends

About Greasemonkey
Greasemonkey is a Firefox [...]]]></description>
			<content:encoded><![CDATA[<p>This post will show you how to write a basic <a href="https://addons.mozilla.org/firefox/addon/748" target="_blank">Greasemonkey</a> script that will add two more menu items to Facebook's friends top menu as shown below.</p>
<p><img src="http://htmlblog.net/wp-content/uploads/2009/04/facebook.png" alt="facebook" title="facebook" width="357" height="135" class="aligncenter size-full wp-image-123" /></p>
<p>These 2 menu items are :</p>
<ul>
<li>"Recently Updated" - show all your friends with recent updates</li>
<li>"Status Updates" - show the latest status updates of your friends</li>
</ul>
<h3 class="post-title">About <a href="https://addons.mozilla.org/firefox/addon/748" target="_blank">Greasemonkey</a></h3>
<p><a href="https://addons.mozilla.org/firefox/addon/748" target="_blank">Greasemonkey</a> is a <a href="http://www.mozilla.com/firefox/" target="_blank">Firefox</a> extension that allows you to write scripts that alter the web pages you visit. You can</p>
<ul>
<li>use it to make a web site more readable or more usable</li>
<li>fix rendering bugs that the site owner can't be bothered to fix themselves. </li>
<li>alter pages so they work better with assistive technologies that speak a web page out loud or convert it to Braille</li>
<li>even automatically retrieve data from other sites to make two sites more interconnected.</li>
</ul>
<p><a href="https://addons.mozilla.org/firefox/addon/748" target="_blank">Greasemonkey</a> by itself does none of these things. In fact, after you install it, you won't notice any change at all... until you start installing what are called “user scripts”. A user script is just a chunk of javascript code, with some additional information that tells <a href="https://addons.mozilla.org/firefox/addon/748" target="_blank">Greasemonkey</a> where and when it should be run. Each user script can target a specific page, a specific site, or a group of sites.</p>
<h3 class="post-title">Install <a href="https://addons.mozilla.org/firefox/addon/748" target="_blank">Greasemonkey</a> addon for Firefox</h3>
<p>Go to <a href="https://addons.mozilla.org/firefox/addon/748" target="_blank">Greasemonkey</a> addon page, click on the "Add to Firefox" button and follow the instructions. After restarting Firefox, you will notice a monkey's head sitting at the bottom right its status bar.<br />
If you right click on the monkey's head and choose "Manage User Scripts" from the resulting menu, you will notice there isn't any script which is quite normal since we have not installed any ;-).</p>
<h3 class="post-title">Creating your first <a href="https://addons.mozilla.org/firefox/addon/748" target="_blank">Greasemonkey</a> script.</h3>
<p>For a <a href="https://addons.mozilla.org/firefox/addon/748" target="_blank">Greasemonkey</a> to be recognized, it must end with <strong>.user.js</strong>, so let us create an empty file and save it as <strong>myfirstscript.user.js</strong><br />
Fire up your favourite text editor and open your newly created file.</p>
<h3 class="post-title"><a href="https://addons.mozilla.org/firefox/addon/748" target="_blank">Greasemonkey</a> script metadata</h3>
<p>All user scripts has a meta data section which tells <a href="https://addons.mozilla.org/firefox/addon/748" target="_blank">Greasemonkey</a> about the script itself, where it came from, and when to run it. For example, our script :</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// ==UserScript==</span>
<span style="color: #009900; font-style: italic;">// @name          My First GM script</span>
<span style="color: #009900; font-style: italic;">// @namespace     http://htmlblog.net</span>
<span style="color: #009900; font-style: italic;">// @description   basic Greasemonkey script</span>
<span style="color: #009900; font-style: italic;">// @include       http://www.facebook.com/</span>
<span style="color: #009900; font-style: italic;">// ==/UserScript==</span>
&nbsp;</pre>
<p>You can find <a href="http://diveintogreasemonkey.org/helloworld/metadata.html" target="_blank">more information about the metadata here</a>. </p>
<p>Remember we want our script available only for <a href="http://www.facebook.com" target="_blank">Facebook</a> pages, that is why we use <strong>@include http://www.facebook.com</strong></p>
<h3 class="post-title">Show me some code.</h3>
<p>Alright, to add our own menus, we need to get the parent element holding the drop down menu. After inspecting Facebook's source code, we can rapidly come to the conclusion that the parent element's id is "fb_menu_friends_dropdown". So the current Facebook code looks like that :</p>
<pre class="html4strict">&nbsp;
<span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;fb_menu_dropdown hidden_elem&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;fb_menu_friends_dropdown&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;fb_menu_item&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><a href="http://december.com/html/4/element/a.html"><span style="color: #000000; font-weight: bold;">&lt;a</span></a> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;http://www.facebook.com/friends/?added&amp;amp;ref=tn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Recently Added<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;fb_menu_item&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><a href="http://december.com/html/4/element/a.html"><span style="color: #000000; font-weight: bold;">&lt;a</span></a> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;http://www.facebook.com/friends/?everyone&amp;amp;ref=tn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>All Friends<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;fb_menu_separator&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;fb_menu_item&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><a href="http://december.com/html/4/element/a.html"><span style="color: #000000; font-weight: bold;">&lt;a</span></a> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;http://www.facebook.com/invite.php?ref=tn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Invite Friends<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;fb_menu_item&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><a href="http://december.com/html/4/element/a.html"><span style="color: #000000; font-weight: bold;">&lt;a</span></a> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;http://www.facebook.com/find-friends/?ref=friends&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Find Friends<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>
&nbsp;</pre>
<p>and our goal is to insert the two menu items before the "Recently Added" item. </p>
<p>For us to do that, we need to get the first child of the parent element and then insert our elements before it and to help us, i.e walking through the DOM selecting parent and childs, I use a <a href="http://gist.github.com/41440" target="_blank">little piece of code found on Github</a>, which eliminates whitespace issues between elements among other features. So our myfirstscript.user.js can be updated to :</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// ==UserScript==</span>
<span style="color: #009900; font-style: italic;">// @name          My First GM script</span>
<span style="color: #009900; font-style: italic;">// @namespace     http://htmlblog.net</span>
<span style="color: #009900; font-style: italic;">// @description   basic &lt;a href=&quot;https://addons.mozilla.org/firefox/addon/748&quot; target=&quot;_blank&quot;&gt;Greasemonkey&lt;/a&gt; script</span>
<span style="color: #009900; font-style: italic;">// @include       http://www.facebook.com/</span>
<span style="color: #009900; font-style: italic;">// ==/UserScript==</span>
DOM = <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">function</span> get<span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>id &amp;&amp; <span style="color: #000066; font-weight: bold;">typeof</span> id === <span style="color: #3366CC;">'string'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            id = document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> id || <span style="color: #003366; font-weight: bold;">null</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">function</span> walk<span style="color: #66cc66;">&#40;</span>element, tag, walk, start, all<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> el = get<span style="color: #66cc66;">&#40;</span>element<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#91;</span>start || walk<span style="color: #66cc66;">&#93;</span>, elements = all ? <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> : <span style="color: #003366; font-weight: bold;">null</span>;
        <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #66cc66;">&#40;</span>el<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>el.<span style="color: #006600;">nodeType</span> === <span style="color: #CC0000;">1</span> &amp;&amp; <span style="color: #66cc66;">&#40;</span>!tag || el.<span style="color: #006600;">tagName</span>.<span style="color: #006600;">toLowerCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> === tag<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>!all<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">return</span> el;
                <span style="color: #66cc66;">&#125;</span>
                elements.<span style="color: #006600;">push</span><span style="color: #66cc66;">&#40;</span>el<span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
            el = el<span style="color: #66cc66;">&#91;</span>walk<span style="color: #66cc66;">&#93;</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> elements;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
        <span style="color: #009900; font-style: italic;">// Get the element by its id</span>
        get: get,
&nbsp;
        walk: walk,
&nbsp;
        <span style="color: #009900; font-style: italic;">// Returns the previousSibling of the Element (excluding text nodes).</span>
        getPrevious: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>el, tag<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> walk<span style="color: #66cc66;">&#40;</span>el, tag, <span style="color: #3366CC;">'previousSibling'</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>,
&nbsp;
        <span style="color: #009900; font-style: italic;">// Like getPrevious, but returns a collection of all the matched previousSiblings.</span>
        getAllPrevious: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>el, tag<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> walk<span style="color: #66cc66;">&#40;</span>el, tag, <span style="color: #3366CC;">'previousSibling'</span>, <span style="color: #003366; font-weight: bold;">null</span>, <span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>,
&nbsp;
        <span style="color: #009900; font-style: italic;">// As getPrevious, but tries to find the nextSibling (excluding text nodes).</span>
        getNext: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>el, tag<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> walk<span style="color: #66cc66;">&#40;</span>el, tag, <span style="color: #3366CC;">'nextSibling'</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>,
&nbsp;
        <span style="color: #009900; font-style: italic;">// Like getNext, but returns a collection of all the matched nextSiblings.</span>
        getAllNext: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>el, tag<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> walk<span style="color: #66cc66;">&#40;</span>el, tag, <span style="color: #3366CC;">'nextSibling'</span>, <span style="color: #003366; font-weight: bold;">null</span>, <span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>,
&nbsp;
        <span style="color: #009900; font-style: italic;">// Works as getPrevious, but tries to find the firstChild (excluding text nodes).</span>
        getFirst: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>el, tag<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> walk<span style="color: #66cc66;">&#40;</span>el, tag, <span style="color: #3366CC;">'nextSibling'</span>, <span style="color: #3366CC;">'firstChild'</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>,
&nbsp;
        <span style="color: #009900; font-style: italic;">// Works as getPrevious, but tries to find the lastChild.</span>
        getLast: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>el, tag<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> walk<span style="color: #66cc66;">&#40;</span>el, tag, <span style="color: #3366CC;">'previousSibling'</span>, <span style="color: #3366CC;">'lastChild'</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>,
&nbsp;
        <span style="color: #009900; font-style: italic;">// Works as getPrevious, but tries to find the parentNode.</span>
        getParent: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>el, tag<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> walk<span style="color: #66cc66;">&#40;</span>el, tag, <span style="color: #3366CC;">'parentNode'</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>,
&nbsp;
        <span style="color: #009900; font-style: italic;">// Like getParent, but returns a collection of all the matched parentNodes up the tree.</span>
        getParents: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>el, tag<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> walk<span style="color: #66cc66;">&#40;</span>el, tag, <span style="color: #3366CC;">'parentNode'</span>, <span style="color: #003366; font-weight: bold;">null</span>, <span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>,
&nbsp;
        <span style="color: #009900; font-style: italic;">// Returns all the Element's children (excluding text nodes).</span>
        getChildren: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>el, tag<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> walk<span style="color: #66cc66;">&#40;</span>el, tag, <span style="color: #3366CC;">'nextSibling'</span>, <span style="color: #3366CC;">'firstChild'</span>, <span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>,
&nbsp;
        <span style="color: #009900; font-style: italic;">// Removes the Element from the DOM.</span>
        dispose: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>el<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            el = get<span style="color: #66cc66;">&#40;</span>el<span style="color: #66cc66;">&#41;</span>;
            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span>el.<span style="color: #006600;">parentNode</span><span style="color: #66cc66;">&#41;</span> ? el.<span style="color: #006600;">parentNode</span>.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span>el<span style="color: #66cc66;">&#41;</span> : el;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #66cc66;">&#125;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>To get the parent element and its first child, we then append the following code :</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// get drop down menu</span>
<span style="color: #003366; font-weight: bold;">var</span> parentNode = DOM.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'fb_menu_friends_dropdown'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #009900; font-style: italic;">// get its first child</span>
<span style="color: #003366; font-weight: bold;">var</span> firstNode = DOM.<span style="color: #006600;">getFirst</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'fb_menu_friends_dropdown'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<h3 class="post-title">Creating our nodes</h3>
<p>Now we must create our two nodes which will be inserted afterward. This is done easily with the following code which is self explanatory :</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">/** For &quot;Recently Updated&quot; */</span>
<span style="color: #009900; font-style: italic;">// create our div with class fb_menu_item</span>
<span style="color: #003366; font-weight: bold;">var</span> recentDiv = document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #66cc66;">&#41;</span>;
recentDiv.<span style="color: #006600;">setAttribute</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'class'</span>, <span style="color: #3366CC;">'fb_menu_item'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #009900; font-style: italic;">// create our link</span>
<span style="color: #003366; font-weight: bold;">var</span> recentLink = document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #66cc66;">&#41;</span>;
recentLink.<span style="color: #006600;">href</span> = <span style="color: #3366CC;">'http://www.facebook.com/friends/?recent&amp;ref=tn'</span>;
&nbsp;
<span style="color: #009900; font-style: italic;">// add text to our link</span>
<span style="color: #003366; font-weight: bold;">var</span> recentDivContent = document.<span style="color: #006600;">createTextNode</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'Recently Updated'</span><span style="color: #66cc66;">&#41;</span>;
recentLink.<span style="color: #006600;">appendChild</span><span style="color: #66cc66;">&#40;</span>recentDivContent<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #009900; font-style: italic;">// add link to our div</span>
recentDiv.<span style="color: #006600;">appendChild</span><span style="color: #66cc66;">&#40;</span>recentLink<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #009900; font-style: italic;">/** For &quot;Status Updates&quot; */</span>
<span style="color: #009900; font-style: italic;">// create our div with class fb_menu_item</span>
<span style="color: #003366; font-weight: bold;">var</span> statusDiv = document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #66cc66;">&#41;</span>;
statusDiv.<span style="color: #006600;">setAttribute</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'class'</span>, <span style="color: #3366CC;">'fb_menu_item'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #009900; font-style: italic;">// create our link</span>
<span style="color: #003366; font-weight: bold;">var</span> statusLink = document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #66cc66;">&#41;</span>;
statusLink.<span style="color: #006600;">href</span> = <span style="color: #3366CC;">'http://www.facebook.com/friends/?status&amp;ref=tn'</span>;
&nbsp;
<span style="color: #009900; font-style: italic;">// add text to our link</span>
<span style="color: #003366; font-weight: bold;">var</span> statusDivContent = document.<span style="color: #006600;">createTextNode</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'Status Updates'</span><span style="color: #66cc66;">&#41;</span>;
statusLink.<span style="color: #006600;">appendChild</span><span style="color: #66cc66;">&#40;</span>statusDivContent<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #009900; font-style: italic;">// add link to our div</span>
statusDiv.<span style="color: #006600;">appendChild</span><span style="color: #66cc66;">&#40;</span>statusLink<span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>We can also add a separator, for having a cleaner look :</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">/** Creates a separator, just to look good */</span>
<span style="color: #003366; font-weight: bold;">var</span> separatorDiv = document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #66cc66;">&#41;</span>;
separatorDiv.<span style="color: #006600;">setAttribute</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'class'</span>, <span style="color: #3366CC;">'fb_menu_separator'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Finally we insert these 3 nodes before the first child element we got earlier on :</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// add all divs before first child of menu</span>
parentNode.<span style="color: #006600;">insertBefore</span><span style="color: #66cc66;">&#40;</span>statusDiv, firstNode<span style="color: #66cc66;">&#41;</span>;
parentNode.<span style="color: #006600;">insertBefore</span><span style="color: #66cc66;">&#40;</span>recentDiv, firstNode<span style="color: #66cc66;">&#41;</span>;
parentNode.<span style="color: #006600;">insertBefore</span><span style="color: #66cc66;">&#40;</span>separatorDiv, firstNode<span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Just to be on the safe side of things, we surround the whole block of code with a try and catch statement.</p>
<h3 class="post-title">Putting it all together</h3>
<p>So finally our code is like this :</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// ==UserScript==</span>
<span style="color: #009900; font-style: italic;">// @name          My First GM script</span>
<span style="color: #009900; font-style: italic;">// @namespace     http://htmlblog.net</span>
<span style="color: #009900; font-style: italic;">// @description   basic &lt;a href=&quot;https://addons.mozilla.org/firefox/addon/748&quot; target=&quot;_blank&quot;&gt;Greasemonkey&lt;/a&gt; script</span>
<span style="color: #009900; font-style: italic;">// @include       http://www.facebook.com/</span>
<span style="color: #009900; font-style: italic;">// ==/UserScript==</span>
&nbsp;
DOM = <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">function</span> get<span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>id &amp;&amp; <span style="color: #000066; font-weight: bold;">typeof</span> id === <span style="color: #3366CC;">'string'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            id = document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> id || <span style="color: #003366; font-weight: bold;">null</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">function</span> walk<span style="color: #66cc66;">&#40;</span>element, tag, walk, start, all<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> el = get<span style="color: #66cc66;">&#40;</span>element<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#91;</span>start || walk<span style="color: #66cc66;">&#93;</span>, elements = all ? <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> : <span style="color: #003366; font-weight: bold;">null</span>;
        <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #66cc66;">&#40;</span>el<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>el.<span style="color: #006600;">nodeType</span> === <span style="color: #CC0000;">1</span> &amp;&amp; <span style="color: #66cc66;">&#40;</span>!tag || el.<span style="color: #006600;">tagName</span>.<span style="color: #006600;">toLowerCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> === tag<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>!all<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">return</span> el;
                <span style="color: #66cc66;">&#125;</span>
                elements.<span style="color: #006600;">push</span><span style="color: #66cc66;">&#40;</span>el<span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
            el = el<span style="color: #66cc66;">&#91;</span>walk<span style="color: #66cc66;">&#93;</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> elements;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
        <span style="color: #009900; font-style: italic;">// Get the element by its id</span>
        get: get,
&nbsp;
        walk: walk,
&nbsp;
        <span style="color: #009900; font-style: italic;">// Returns the previousSibling of the Element (excluding text nodes).</span>
        getPrevious: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>el, tag<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> walk<span style="color: #66cc66;">&#40;</span>el, tag, <span style="color: #3366CC;">'previousSibling'</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>,
&nbsp;
        <span style="color: #009900; font-style: italic;">// Like getPrevious, but returns a collection of all the matched previousSiblings.</span>
        getAllPrevious: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>el, tag<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> walk<span style="color: #66cc66;">&#40;</span>el, tag, <span style="color: #3366CC;">'previousSibling'</span>, <span style="color: #003366; font-weight: bold;">null</span>, <span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>,
&nbsp;
        <span style="color: #009900; font-style: italic;">// As getPrevious, but tries to find the nextSibling (excluding text nodes).</span>
        getNext: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>el, tag<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> walk<span style="color: #66cc66;">&#40;</span>el, tag, <span style="color: #3366CC;">'nextSibling'</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>,
&nbsp;
        <span style="color: #009900; font-style: italic;">// Like getNext, but returns a collection of all the matched nextSiblings.</span>
        getAllNext: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>el, tag<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> walk<span style="color: #66cc66;">&#40;</span>el, tag, <span style="color: #3366CC;">'nextSibling'</span>, <span style="color: #003366; font-weight: bold;">null</span>, <span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>,
&nbsp;
        <span style="color: #009900; font-style: italic;">// Works as getPrevious, but tries to find the firstChild (excluding text nodes).</span>
        getFirst: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>el, tag<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> walk<span style="color: #66cc66;">&#40;</span>el, tag, <span style="color: #3366CC;">'nextSibling'</span>, <span style="color: #3366CC;">'firstChild'</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>,
&nbsp;
        <span style="color: #009900; font-style: italic;">// Works as getPrevious, but tries to find the lastChild.</span>
        getLast: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>el, tag<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> walk<span style="color: #66cc66;">&#40;</span>el, tag, <span style="color: #3366CC;">'previousSibling'</span>, <span style="color: #3366CC;">'lastChild'</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>,
&nbsp;
        <span style="color: #009900; font-style: italic;">// Works as getPrevious, but tries to find the parentNode.</span>
        getParent: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>el, tag<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> walk<span style="color: #66cc66;">&#40;</span>el, tag, <span style="color: #3366CC;">'parentNode'</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>,
&nbsp;
        <span style="color: #009900; font-style: italic;">// Like getParent, but returns a collection of all the matched parentNodes up the tree.</span>
        getParents: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>el, tag<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> walk<span style="color: #66cc66;">&#40;</span>el, tag, <span style="color: #3366CC;">'parentNode'</span>, <span style="color: #003366; font-weight: bold;">null</span>, <span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>,
&nbsp;
        <span style="color: #009900; font-style: italic;">// Returns all the Element's children (excluding text nodes).</span>
        getChildren: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>el, tag<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> walk<span style="color: #66cc66;">&#40;</span>el, tag, <span style="color: #3366CC;">'nextSibling'</span>, <span style="color: #3366CC;">'firstChild'</span>, <span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>,
&nbsp;
        <span style="color: #009900; font-style: italic;">// Removes the Element from the DOM.</span>
        dispose: <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>el<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            el = get<span style="color: #66cc66;">&#40;</span>el<span style="color: #66cc66;">&#41;</span>;
            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span>el.<span style="color: #006600;">parentNode</span><span style="color: #66cc66;">&#41;</span> ? el.<span style="color: #006600;">parentNode</span>.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span>el<span style="color: #66cc66;">&#41;</span> : el;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #66cc66;">&#125;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000066; font-weight: bold;">try</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #009900; font-style: italic;">// get drop down menu</span>
	<span style="color: #003366; font-weight: bold;">var</span> parentNode = DOM.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'fb_menu_friends_dropdown'</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #009900; font-style: italic;">// get its first child</span>
	<span style="color: #003366; font-weight: bold;">var</span> firstNode = DOM.<span style="color: #006600;">getFirst</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'fb_menu_friends_dropdown'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #009900; font-style: italic;">/** For &quot;Recently Updated&quot; */</span>
	<span style="color: #009900; font-style: italic;">// create our div with class fb_menu_item</span>
	<span style="color: #003366; font-weight: bold;">var</span> recentDiv = document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #66cc66;">&#41;</span>;
	recentDiv.<span style="color: #006600;">setAttribute</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'class'</span>, <span style="color: #3366CC;">'fb_menu_item'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #009900; font-style: italic;">// create our link</span>
	<span style="color: #003366; font-weight: bold;">var</span> recentLink = document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #66cc66;">&#41;</span>;
	recentLink.<span style="color: #006600;">href</span> = <span style="color: #3366CC;">'http://www.facebook.com/friends/?recent&amp;ref=tn'</span>;
&nbsp;
	<span style="color: #009900; font-style: italic;">// add text to our link</span>
	<span style="color: #003366; font-weight: bold;">var</span> recentDivContent = document.<span style="color: #006600;">createTextNode</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'Recently Updated'</span><span style="color: #66cc66;">&#41;</span>;
	recentLink.<span style="color: #006600;">appendChild</span><span style="color: #66cc66;">&#40;</span>recentDivContent<span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #009900; font-style: italic;">// add link to our div</span>
	recentDiv.<span style="color: #006600;">appendChild</span><span style="color: #66cc66;">&#40;</span>recentLink<span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #009900; font-style: italic;">/** For &quot;Status Updates&quot; */</span>
	<span style="color: #009900; font-style: italic;">// create our div with class fb_menu_item</span>
	<span style="color: #003366; font-weight: bold;">var</span> statusDiv = document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #66cc66;">&#41;</span>;
	statusDiv.<span style="color: #006600;">setAttribute</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'class'</span>, <span style="color: #3366CC;">'fb_menu_item'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #009900; font-style: italic;">// create our link</span>
	<span style="color: #003366; font-weight: bold;">var</span> statusLink = document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #66cc66;">&#41;</span>;
	statusLink.<span style="color: #006600;">href</span> = <span style="color: #3366CC;">'http://www.facebook.com/friends/?status&amp;ref=tn'</span>;
&nbsp;
	<span style="color: #009900; font-style: italic;">// add text to our link</span>
	<span style="color: #003366; font-weight: bold;">var</span> statusDivContent = document.<span style="color: #006600;">createTextNode</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'Status Updates'</span><span style="color: #66cc66;">&#41;</span>;
	statusLink.<span style="color: #006600;">appendChild</span><span style="color: #66cc66;">&#40;</span>statusDivContent<span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #009900; font-style: italic;">// add link to our div</span>
	statusDiv.<span style="color: #006600;">appendChild</span><span style="color: #66cc66;">&#40;</span>statusLink<span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #009900; font-style: italic;">/** Creates a separator, just to look good */</span>
	<span style="color: #003366; font-weight: bold;">var</span> separatorDiv = document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #66cc66;">&#41;</span>;
	separatorDiv.<span style="color: #006600;">setAttribute</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'class'</span>, <span style="color: #3366CC;">'fb_menu_separator'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #009900; font-style: italic;">// add both divs before first child of menu</span>
	parentNode.<span style="color: #006600;">insertBefore</span><span style="color: #66cc66;">&#40;</span>statusDiv, firstNode<span style="color: #66cc66;">&#41;</span>;
	parentNode.<span style="color: #006600;">insertBefore</span><span style="color: #66cc66;">&#40;</span>recentDiv, firstNode<span style="color: #66cc66;">&#41;</span>;
	parentNode.<span style="color: #006600;">insertBefore</span><span style="color: #66cc66;">&#40;</span>separatorDiv, firstNode<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #000066; font-weight: bold;">catch</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;
&nbsp;</pre>
<h3 class="post-title">Installing and testing our script</h3>
<p>Fire up Firefox and select <strong>"File" -> "Open File..."</strong> in the menu bar and then browse for your script, i.e myfirstscript.user.js in our case. You will be prompted to install the script by <a href="https://addons.mozilla.org/firefox/addon/748" target="_blank">Greasemonkey</a>. After installation is complete, head to Facebook, login and hover over the "Friends" menu, you will see your newly created menus.</p>
<p><a href="/demo/greasemonkey/fb_recently_updated_link.user.zip"><strong>Download script</strong></a><br />
<a href="/demo/greasemonkey/fb_recently_updated_link.user.js"><strong>Install script</strong></a></p>
]]></content:encoded>
			<wfw:commentRss>http://htmlblog.net/writing-your-first-greasemonkey-script-adding-menus-to-facebooks-top-menu/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Expanding/collapsing javascript menu</title>
		<link>http://htmlblog.net/expanding-collapsing-javascript-menu/</link>
		<comments>http://htmlblog.net/expanding-collapsing-javascript-menu/#comments</comments>
		<pubDate>Thu, 17 Jul 2008 18:07:13 +0000</pubDate>
		<dc:creator>asvin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[collapse]]></category>
		<category><![CDATA[dav glass]]></category>
		<category><![CDATA[dhtml]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[effects]]></category>
		<category><![CDATA[expand]]></category>
		<category><![CDATA[menu]]></category>
		<category><![CDATA[newbie]]></category>
		<category><![CDATA[slashdot]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[yahoo]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://htmlblog.net/?p=22</guid>
		<description><![CDATA[In this post I'll show you how to create an expanding/collapsing menu like Slashdot left menu used to be, using the YUI library and Dav Glass YUI effects widget.
Preview of the script we are going to build.

The HTML markup for our menu is :
&#160;&#60;div id=&#34;menu&#34;&#62;&#60;h1 id=&#34;menu-title&#34;&#62;Menu&#60;/h1&#62;&#60;div id=&#34;menu-links&#34;&#62;&#60;ul&#62;&#60;li&#62;Home&#60;/li&#62;&#60;li&#62;About&#60;/li&#62;&#60;li&#62;CSS&#60;/li&#62;&#60;/ul&#62;&#60;/div&#62;&#60;/div&#62;&#160;
We use 2 div tags, one for holding the [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I'll show you how to create an expanding/collapsing menu like <a href="http://slashdot.org">Slashdot</a> left menu used to be, using the <a href="http://developer.yahoo.com/yui">YUI library</a> and <a href="http://blog.davglass.com/">Dav Glass</a> <a href="http://blog.davglass.com/files/yui/effects/">YUI effects widget</a>.</p>
<p><strong><a href="http://htmlblog.net/demo/yui-menu-slashdot/">Preview of the script we are going to build.</a></strong><br />
<span id="more-22"></span><br />
The HTML markup for our menu is :</p>
<pre class="html4strict"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;menu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/h1.html"><span style="color: #000000; font-weight: bold;">&lt;h1</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;menu-title&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Menu<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h1&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;menu-links&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">&lt;ul&gt;</span></a></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span>Home<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span>About<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span>CSS<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>
<p>We use 2 <strong>div</strong> tags, one for holding the menu itself and the other one which contains the links in an <strong>unordered list</strong> and 1 <strong>h1</strong> tag for the title.</p>
<p>The CSS for the menu :</p>
<pre class="css"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">/** width of the menu */</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #cc00cc;">#menu</span><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #000000; font-weight: bold;">width</span>: <span style="color: #933;">180px</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">/** the main title */</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #cc00cc;">#menu</span> h1<span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #000000; font-weight: bold;">line-height</span>: <span style="color: #933;">35px</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #000000; font-weight: bold;">color</span>: <span style="color: #cc00cc;">#fff</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #808080; font-style: italic;">/** simulate link */</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #000000; font-weight: bold;">cursor</span>: <span style="color: #993333;">pointer</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #000000; font-weight: bold;">font-weight</span>: <span style="color: #993333;">bold</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #808080; font-style: italic;">/** background image will change when menu expands/collapses */</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #000000; font-weight: bold;">background</span>: <span style="color: #cc00cc;">#<span style="color: #933;">666</span></span> <span style="color: #993333;">url</span><span style="color: #66cc66;">&#40;</span><span style="color: #933;">images/block-arrow-expanded<span style="color: #6666ff;">.gif</span></span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #993333;">scroll</span> <span style="color: #933;">30px</span> <span style="color: #933;">13px</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">/** the links */</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #cc00cc;">#menu-links</span> li<span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #000000; font-weight: bold;">line-height</span>: <span style="color: #933;">35px</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				border-bottom<span style="color: #3333ff;">:<span style="color: #933;">1px</span></span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#ddd</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #000000; font-weight: bold;">background-color</span>: <span style="color: #cc00cc;">#eee</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>
<p>Please pay attention to the background for the #menu h1. It contains the image, which will change when the menu collapses or expands. When the menu collapses we'll be using the block-arrow-collapsed.gif image and when the menu expands we'll be using the block-arrow-expanded.gif image. You can find the images in the <a href="http://htmlblog.net/demo/yui-menu-slashdot/yui-menu.zip">sample demo file</a> in the images directory.</p>
<p><a href='http://htmlblog.net/demo/yui-menu-slashdot/'><img src="http://htmlblog.net/wp-content/uploads/2008/07/menu.png" alt="" title="menu" width="362" height="260" class="alignleft size-full wp-image-23" /></a></p>
<div style="clear:both"></div>
<p>To do the collapsing/expanding effects, we need to include the following javascript files beforehand :</p>
<pre class="html4strict"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- js --&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span>  <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://yui.yahooapis.com/combo?2.5.2/build/yahoo-dom-event/yahoo-dom-event.js&amp;2.5.2/build/animation/animation-min.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;js/tools-min.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;js/effects-min.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>
<p>We use Dav Glass <a href="http://blog.davglass.com/files/yui/tools/">YAHOO.Tools package</a> and his <a href="http://blog.davglass.com/files/yui/effects/">effects widget</a> along with yahoo-dom-event-animation <a href="http://yuiblog.com/blog/2008/07/16/combohandler/">aggregate file</a> which are hosted on Yahoo servers.</p>
<p>Finally the javascript to do the magical stuff goes like that :</p>
<pre class="javascript"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;script type=<span style="color: #3366CC;">&quot;text/javascript&quot;</span>&gt;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">addListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'menu-title'</span>, <span style="color: #3366CC;">'click'</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">getStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'menu-links'</span>, <span style="color: #3366CC;">'display'</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #3366CC;">'block'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">					<span style="color: #003366; font-weight: bold;">new</span> YAHOO.<span style="color: #006600;">widget</span>.<span style="color: #006600;">Effects</span>.<span style="color: #006600;">BlindUp</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'menu-links'</span>, <span style="color: #66cc66;">&#123;</span>seconds: <span style="color: #CC0000;">0.2</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">					YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'menu-title'</span>, <span style="color: #3366CC;">'background-image'</span>, <span style="color: #3366CC;">'url(images/block-arrow-collapsed.gif)'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #000066; font-weight: bold;">else</span><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">					<span style="color: #003366; font-weight: bold;">new</span> YAHOO.<span style="color: #006600;">widget</span>.<span style="color: #006600;">Effects</span>.<span style="color: #006600;">BlindDown</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'menu-links'</span>, <span style="color: #66cc66;">&#123;</span>seconds: <span style="color: #CC0000;">0.2</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">					YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'menu-title'</span>, <span style="color: #3366CC;">'background-image'</span>, <span style="color: #3366CC;">'url(images/block-arrow-expanded.gif)'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				<span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		&lt;/script&gt;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>
<p>In line 3 of the above listing, we wait when the user clicks on the menu-title container and we check the display attribute of our links. If the links are not currently displayed then we display them by using Dav's <a href="http://blog.davglass.com/files/yui/docs/?type=effects">BlindDown effect</a> with a duration of 0.2 seconds ;-).</p>
<pre class="javascript"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">new</span> YAHOO.<span style="color: #006600;">widget</span>.<span style="color: #006600;">Effects</span>.<span style="color: #006600;">BlindDown</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'menu-links'</span>, <span style="color: #66cc66;">&#123;</span>seconds: <span style="color: #CC0000;">0.2</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>
<p>We also change the background image for the menu title by using the <a href="http://developer.yahoo.com/yui/dom">YAHOO.util.Dom.setStyle</a> method:</p>
<pre class="javascript"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'menu-title'</span>, <span style="color: #3366CC;">'background-image'</span>, <span style="color: #3366CC;">'url(images/block-arrow-expanded.gif)'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>
<p>If the links are visible then we do the contrary, i.e we use the BlindUp effect and use the collapsed gif as background image.</p>
<p><strong><a href="http://htmlblog.net/demo/yui-menu-slashdot/yui-menu.zip">Download sample demo</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://htmlblog.net/expanding-collapsing-javascript-menu/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>YUI-based numeric stepper widget</title>
		<link>http://htmlblog.net/yui-based-numeric-stepper-widget/</link>
		<comments>http://htmlblog.net/yui-based-numeric-stepper-widget/#comments</comments>
		<pubDate>Mon, 14 Jul 2008 16:08:20 +0000</pubDate>
		<dc:creator>asvin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[controls]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[html xhtml]]></category>
		<category><![CDATA[numeric]]></category>
		<category><![CDATA[stepper]]></category>
		<category><![CDATA[widget]]></category>
		<category><![CDATA[yahoo]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://htmlblog.net/?p=20</guid>
		<description><![CDATA[

Since I had some problems with YUI slider, I decided to write a YUI-based numeric stepper that allows users to select values within a predefined range.

To instantiate the widget include the following files :
&#160;
&#60;!-- CSS --&#62;
&#60;link rel=&#34;stylesheet&#34; type=&#34;text/css&#34; href=&#34;stepper/stepper.css&#34; /&#62;
&#160;
&#60;!-- Dependencies --&#62;
&#60;script type=&#34;text/javascript&#34; src=&#34;http://yui.yahooapis.com/2.5.2/build/yahoo-dom-event/yahoo-dom-event.js&#34;&#62;&#60;/script&#62;
&#160;
&#60;!-- Stepper source file --&#62;
&#60;script type=&#34;text/javascript&#34; src=&#34;stepper/stepper-min.js&#34;&#62;&#60;/script&#62;
&#160;
Then place your stepper in your [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://htmlblog.net/demo/stepper/"><img src="http://htmlblog.net/wp-content/uploads/2008/07/stepper.png" alt="" title="stepper" width="444" height="126" class="alignleft size-full wp-image-21" /></a></p>
<div style="clear:both;"></div>
<p>Since I had some problems with <a href="http://developer.yahoo.com/yui/slider/">YUI slider</a>, I decided to write a <a href="http://developer.yahoo.com/yui/">YUI-based</a> numeric stepper that allows users to select values within a predefined range.<br />
<span id="more-20"></span><br />
To instantiate the widget include the following files :</p>
<pre class="html4strict">&nbsp;
<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- CSS --&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/link.html"><span style="color: #000000; font-weight: bold;">&lt;link</span></a> <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;stepper/stepper.css&quot;</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Dependencies --&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://yui.yahooapis.com/2.5.2/build/yahoo-dom-event/yahoo-dom-event.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Stepper source file --&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;stepper/stepper-min.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span>
&nbsp;</pre>
<p>Then place your stepper in your body :</p>
<pre class="html4strict">&nbsp;
<span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;stepperValue&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>10 px<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;stepperControls&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;stepperUp&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><a href="http://december.com/html/4/element/img.html"><span style="color: #000000; font-weight: bold;">&lt;img</span></a> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;stepper/add.gif&quot;</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;stepperDown&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><a href="http://december.com/html/4/element/img.html"><span style="color: #000000; font-weight: bold;">&lt;img</span></a> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;stepper/sub.gif&quot;</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;clear:both&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>
&nbsp;</pre>
<p>The <strong>stepperValue</strong> id will display the current value of the stepper.<br />
The <strong>stepperControls</strong> will contain the different controls for our stepper, and you can place it anywhere you want and use whatever images you want.</p>
<p>Finally when the <a href="http://developer.yahoo.com/yui/event/#ondomready">DOM is ready</a>, instantiate the widget :</p>
<pre class="javascript">&nbsp;
&lt;script type=<span style="color: #3366CC;">&quot;text/javascript&quot;</span>&gt;			
	YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">onDOMReady</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> mystepper = <span style="color: #003366; font-weight: bold;">new</span> YAHOO.<span style="color: #006600;">widget</span>.<span style="color: #006600;">Stepper</span><span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">10</span>, <span style="color: #3366CC;">'stepperUp'</span>, <span style="color: #3366CC;">'stepperDown'</span>, <span style="color: #CC0000;">0</span>, <span style="color: #CC0000;">200</span>, <span style="color: #CC0000;">10</span><span style="color: #66cc66;">&#41;</span>;
		mystepper.<span style="color: #006600;">onChange</span>.<span style="color: #006600;">subscribe</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'stepperValue'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">innerHTML</span> = mystepper.<span style="color: #006600;">getValue</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #3366CC;">' px'</span>;
		<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&lt;/script&gt;
&nbsp;</pre>
<p>The stepper takes 6 parameters, all mandatory (check source code) :</p>
<pre class="javascript">&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> mystepper = <span style="color: #003366; font-weight: bold;">new</span> YAHOO.<span style="color: #006600;">widget</span>.<span style="color: #006600;">Stepper</span><span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">10</span>, <span style="color: #3366CC;">'stepperUp'</span>, <span style="color: #3366CC;">'stepperDown'</span>, <span style="color: #CC0000;">0</span>, <span style="color: #CC0000;">200</span>, <span style="color: #CC0000;">10</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<ul>
<li>1. the initial value</li>
<li>2. the ID of the element for the up control button</li>
<li>3. the ID of the element for the down control button</li>
<li>4. the minimum value the stepper can decreased to</li>
<li>5. the maximum value the stepper can increased to</li>
<li>6. by how much the stepper is incremented/decremented after each click</li>
</ul>
<p>We listen for any change to the stepper and in this case, display the updated value in the container we defined earlier on ( stepperValue) using <a href="http://developer.mozilla.org/en/docs/DOM:element.innerHTML">innerHTML</a>.</p>
<pre class="javascript">&nbsp;
mystepper.<span style="color: #006600;">onChange</span>.<span style="color: #006600;">subscribe</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'stepperValue'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">innerHTML</span> = mystepper.<span style="color: #006600;">getValue</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #3366CC;">' px'</span>;
		<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>You can get the current value of the stepper by calling :</p>
<pre class="javascript">&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> currentValue = mystepper.<span style="color: #006600;">getValue</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p><a href="http://htmlblog.net/demo/stepper/"><strong>Live demo</strong></a><br />
<a href="http://htmlblog.net/demo/stepper/stepper.zip"><strong>Download source code (including demo)</strong></a></p>
]]></content:encoded>
			<wfw:commentRss>http://htmlblog.net/yui-based-numeric-stepper-widget/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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's the Cookie utility and at this time of posting, it's still in beta version.
I'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 size for [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://farm1.static.flickr.com/23/26073454_6be105c0cf_m.jpg" alt="Cookie" class="image" /></p>
<p>Since <a href="http://developer.yahoo.com/yui/">YUI 2.5.0</a> there's the <a href="http://developer.yahoo.com/yui/cookie/">Cookie utility</a> and at this time of posting, it's still in beta version.<br />
I'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'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>
<p><strong>Getting started.</strong></p>
<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'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="html4strict"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #00bbdd;">&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;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">&lt;html</span></a> xmlns=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> xml:<span style="color: #000066;">lang</span>=<span style="color: #ff0000;">&quot;en&quot;</span> <span style="color: #000066;">lang</span>=<span style="color: #ff0000;">&quot;en&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900;"><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">&lt;head&gt;</span></a></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900;"><a href="http://december.com/html/4/element/title.html"><span style="color: #000000; font-weight: bold;">&lt;title&gt;</span></a></span>yui cookie<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900;"><a href="http://december.com/html/4/element/meta.html"><span style="color: #000000; font-weight: bold;">&lt;meta</span></a> <span style="color: #000066;">http-equiv</span>=<span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;text/html; charset=utf-8&quot;</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900;"><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">&lt;body&gt;</span></a></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div&gt;</span></a></span><span style="color: #009900;"><a href="http://december.com/html/4/element/img.html"><span style="color: #000000; font-weight: bold;">&lt;img</span></a> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;A+.gif&quot;</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;increase font size&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;increaseFont&quot;</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div&gt;</span></a></span><span style="color: #009900;"><a href="http://december.com/html/4/element/img.html"><span style="color: #000000; font-weight: bold;">&lt;img</span></a> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;A-.gif&quot;</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;decrease font size&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;decreaseFont&quot;</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;news-main&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sit amet metus.</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Nunc quam elit, posuere nec, auctor in, rhoncus quis, dui. Aliquam erat volutpat. Ut dignissim, massa 	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>
<p><a href='http://htmlblog.net/wp-content/uploads/2008/06/cookie.png'><img src="http://htmlblog.net/wp-content/uploads/2008/06/cookie.png" alt="" title="cookie" class="alignleft size-full wp-image-9" /></a></p>
<div style="clear: both;"></div>
<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="html4strict"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #00bbdd;">&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;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">&lt;html</span></a> xmlns=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> xml:<span style="color: #000066;">lang</span>=<span style="color: #ff0000;">&quot;en&quot;</span> <span style="color: #000066;">lang</span>=<span style="color: #ff0000;">&quot;en&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900;"><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">&lt;head&gt;</span></a></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900;"><a href="http://december.com/html/4/element/title.html"><span style="color: #000000; font-weight: bold;">&lt;title&gt;</span></a></span>yui cookie<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900;"><a href="http://december.com/html/4/element/meta.html"><span style="color: #000000; font-weight: bold;">&lt;meta</span></a> <span style="color: #000066;">http-equiv</span>=<span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;text/html; charset=utf-8&quot;</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900;"><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">&lt;body&gt;</span></a></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div&gt;</span></a></span><span style="color: #009900;"><a href="http://december.com/html/4/element/img.html"><span style="color: #000000; font-weight: bold;">&lt;img</span></a> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;A+.gif&quot;</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;increase font size&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;increaseFont&quot;</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div&gt;</span></a></span><span style="color: #009900;"><a href="http://december.com/html/4/element/img.html"><span style="color: #000000; font-weight: bold;">&lt;img</span></a> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;A-.gif&quot;</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;decrease font size&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;decreaseFont&quot;</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;news-main&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sit amet metus.</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Nunc quam elit, posuere nec, auctor in, rhoncus quis, dui. Aliquam erat volutpat. Ut dignissim, massa</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- js --&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://yui.yahooapis.com/2.5.2/build/yahoo-dom-event/yahoo-dom-event.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://yui.yahooapis.com/2.5.2/build/cookie/cookie-beta-min.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- /js --&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></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="javascript"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> updateFontSize = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>o, params<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">try</span><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">type</span> == <span style="color: #3366CC;">'increase'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			newSize = parseInt<span style="color: #66cc66;">&#40;</span>YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">getStyle</span><span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">id</span>, <span style="color: #3366CC;">'font-size'</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #CC0000;">10</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #CC0000;">1</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #000066; font-weight: bold;">else</span><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			newSize = parseInt<span style="color: #66cc66;">&#40;</span>YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">getStyle</span><span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">id</span>, <span style="color: #3366CC;">'font-size'</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #CC0000;">10</span><span style="color: #66cc66;">&#41;</span> - <span style="color: #CC0000;">1</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">id</span>, <span style="color: #3366CC;">'font-size'</span>, newSize + <span style="color: #3366CC;">'px'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Cookie</span>.<span style="color: #006600;">set</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;news-size&quot;</span>, newSize, <span style="color: #66cc66;">&#123;</span> path: <span style="color: #3366CC;">&quot;/&quot;</span>, expires: <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;December 22, 2010&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">catch</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">addListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'increaseFont'</span>, <span style="color: #3366CC;">'click'</span>, updateFontSize, <span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">&quot;id&quot;</span> : <span style="color: #3366CC;">'news-main'</span>, <span style="color: #3366CC;">&quot;type&quot;</span> : <span style="color: #3366CC;">'increase'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">addListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'decreaseFont'</span>, <span style="color: #3366CC;">'click'</span>, updateFontSize, <span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">&quot;id&quot;</span> : <span style="color: #3366CC;">'news-main'</span>, <span style="color: #3366CC;">&quot;type&quot;</span> : <span style="color: #3366CC;">'decrease'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">addListener</span><span style="color: #66cc66;">&#40;</span>window, <span style="color: #3366CC;">'load'</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">try</span><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #003366; font-weight: bold;">var</span> newsSize = YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Cookie</span>.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;news-size&quot;</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	        	YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'news-main'</span>, <span style="color: #3366CC;">'font-size'</span>, value + <span style="color: #3366CC;">'px'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">catch</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>
<p><strong>Now we let us have an extensive look at the code above.</strong></p>
<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="javascript">&nbsp;
YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">id</span>, <span style="color: #3366CC;">'font-size'</span>, newSize + <span style="color: #3366CC;">'px'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</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>
<p><strong>Listeners</strong><br />
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="javascript">&nbsp;
YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">addListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'increaseFont'</span>, <span style="color: #3366CC;">'click'</span>, updateFontSize, <span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">&quot;id&quot;</span> : <span style="color: #3366CC;">'news-main'</span>, <span style="color: #3366CC;">&quot;type&quot;</span> : <span style="color: #3366CC;">'increase'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">addListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'decreaseFont'</span>, <span style="color: #3366CC;">'click'</span>, updateFontSize, <span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">&quot;id&quot;</span> : <span style="color: #3366CC;">'news-main'</span>, <span style="color: #3366CC;">&quot;type&quot;</span> : <span style="color: #3366CC;">'decrease'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</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="javascript">&nbsp;
YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">addListener</span><span style="color: #66cc66;">&#40;</span>window, <span style="color: #3366CC;">'load'</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">try</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> newsSize = YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Cookie</span>.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;news-size&quot;</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
&nbsp;</pre>
<p>If the latter doesn'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="javascript">&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> newsSize = YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Cookie</span>.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;news-size&quot;</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'news-main'</span>, <span style="color: #3366CC;">'font-size'</span>, value + <span style="color: #3366CC;">'px'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>As always we surround the block with try catch statements.</p>
<p><strong>Our final code looks that that :</strong></p>
<pre class="html4strict"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #00bbdd;">&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;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">&lt;html</span></a> xmlns=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> xml:<span style="color: #000066;">lang</span>=<span style="color: #ff0000;">&quot;en&quot;</span> <span style="color: #000066;">lang</span>=<span style="color: #ff0000;">&quot;en&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900;"><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">&lt;head&gt;</span></a></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900;"><a href="http://december.com/html/4/element/title.html"><span style="color: #000000; font-weight: bold;">&lt;title&gt;</span></a></span>yui cookie<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900;"><a href="http://december.com/html/4/element/meta.html"><span style="color: #000000; font-weight: bold;">&lt;meta</span></a> <span style="color: #000066;">http-equiv</span>=<span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;text/html; charset=utf-8&quot;</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900;"><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">&lt;body&gt;</span></a></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div&gt;</span></a></span><span style="color: #009900;"><a href="http://december.com/html/4/element/img.html"><span style="color: #000000; font-weight: bold;">&lt;img</span></a> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;A+.gif&quot;</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;increase font size&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;increaseFont&quot;</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div&gt;</span></a></span><span style="color: #009900;"><a href="http://december.com/html/4/element/img.html"><span style="color: #000000; font-weight: bold;">&lt;img</span></a> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;A-.gif&quot;</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;decrease font size&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;decreaseFont&quot;</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;news-main&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sit amet metus.</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Nunc quam elit, posuere nec, auctor in, rhoncus quis, dui. Aliquam erat volutpat. Ut dignissim, massa</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- js --&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://yui.yahooapis.com/2.5.2/build/yahoo-dom-event/yahoo-dom-event.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://yui.yahooapis.com/2.5.2/build/cookie/cookie-beta-min.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- /js --&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		var updateFontSize = function(o, params){</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			try{</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				if(params.type == 'increase'){</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">					newSize = parseInt(YAHOO.util.Dom.getStyle(params.id, 'font-size'), 10) + 1;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				}</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				else{</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">					newSize = parseInt(YAHOO.util.Dom.getStyle(params.id, 'font-size'), 10) - 1;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				}</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				YAHOO.util.Dom.setStyle(params.id, 'font-size', newSize + 'px');</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				YAHOO.util.Cookie.set(&quot;news-size&quot;, newSize, { path: &quot;/&quot;, expires: new Date(&quot;December 22, 2010&quot;) });</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			}</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			catch(e){}</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		};</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		YAHOO.util.Event.addListener('increaseFont', 'click', updateFontSize, {&quot;id&quot; : 'news-main', &quot;type&quot; : 'increase'});</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		YAHOO.util.Event.addListener('decreaseFont', 'click', updateFontSize, {&quot;id&quot; : 'news-main', &quot;type&quot; : 'decrease'});</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		YAHOO.util.Event.addListener(window, 'load', function(){</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			try{</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				var newsSize = YAHOO.util.Cookie.get(&quot;news-size&quot;, function(value){</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	        			YAHOO.util.Dom.setStyle('news-main', 'font-size', value + 'px');</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				});</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			}</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			catch(e){}</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		});</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html&gt;</span></span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>
<p><strong><a href='http://htmlblog.net/wp-content/uploads/2008/06/Cookie.tar.gz'>Download sample demo.</a></strong></p>
<p>Of course all this can be done with using the YUI library, but it'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>
