<?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>Tue, 09 Nov 2010 11:39:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>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&#8217;s friends top menu as shown below. These 2 menu items are : &#8220;Recently Updated&#8221; &#8211; show all your friends with recent updates &#8220;Status Updates&#8221; &#8211; show the latest status updates of your friends About [...]]]></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&#8217;s friends top menu as shown below.</p>
<p>These 2 menu items are :</p>
<ul>
<li>&#8220;Recently Updated&#8221; &#8211; show all your friends with recent updates</li>
<li>&#8220;Status Updates&#8221; &#8211; 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&#8217;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&#8217;t notice any change at all&#8230; 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 &#8220;Add to Firefox&#8221; button and follow the instructions. After restarting Firefox, you will notice a monkey&#8217;s head sitting at the bottom right its status bar.<br />
If you right click on the monkey&#8217;s head and choose &#8220;Manage User Scripts&#8221; from the resulting menu, you will notice there isn&#8217;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="brush: javascript">
// ==UserScript==
// @name          My First GM script
// @namespace     http://htmlblog.net
// @description   basic Greasemonkey script
// @include       http://www.facebook.com/
// ==/UserScript==
</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&#8217;s source code, we can rapidly come to the conclusion that the parent element&#8217;s id is &#8220;fb_menu_friends_dropdown&#8221;. So the current Facebook code looks like that :</p>
<pre class="brush: xhtml">
&lt;div class=&quot;fb_menu_dropdown hidden_elem&quot; id=&quot;fb_menu_friends_dropdown&quot;&gt;
&lt;div class=&quot;fb_menu_item&quot;&gt;&lt;a href=&quot;http://www.facebook.com/friends/?added&amp;amp;ref=tn&quot;&gt;Recently Added&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;fb_menu_item&quot;&gt;&lt;a href=&quot;http://www.facebook.com/friends/?everyone&amp;amp;ref=tn&quot;&gt;All Friends&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;fb_menu_separator&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;fb_menu_item&quot;&gt;&lt;a href=&quot;http://www.facebook.com/invite.php?ref=tn&quot;&gt;Invite Friends&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;fb_menu_item&quot;&gt;&lt;a href=&quot;http://www.facebook.com/find-friends/?ref=friends&quot;&gt;Find Friends&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
</pre>
<p>and our goal is to insert the two menu items before the &#8220;Recently Added&#8221; 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="brush: javascript">
// ==UserScript==
// @name          My First GM script
// @namespace     http://htmlblog.net
// @description   basic &lt;a href=&quot;https://addons.mozilla.org/firefox/addon/748&quot; target=&quot;_blank&quot;&gt;Greasemonkey&lt;/a&gt; script
// @include       http://www.facebook.com/
// ==/UserScript==
DOM = function () {

    function get(id) {
        if (id &amp;&amp; typeof id === &#039;string&#039;) {
            id = document.getElementById(id);
        }
        return id || null;
    }

    function walk(element, tag, walk, start, all) {
        var el = get(element)[start || walk], elements = all ? [] : null;
        while (el) {
            if (el.nodeType === 1 &amp;&amp; (!tag || el.tagName.toLowerCase() === tag)) {
                if (!all) {
                    return el;
                }
                elements.push(el);
            }
            el = el[walk];
        }
        return elements;
    }

    return {

        // Get the element by its id
        get: get,

        walk: walk,

        // Returns the previousSibling of the Element (excluding text nodes).
        getPrevious: function (el, tag) {
            return walk(el, tag, &#039;previousSibling&#039;);
        },

        // Like getPrevious, but returns a collection of all the matched previousSiblings.
        getAllPrevious: function (el, tag) {
            return walk(el, tag, &#039;previousSibling&#039;, null, true);
        },

        // As getPrevious, but tries to find the nextSibling (excluding text nodes).
        getNext: function (el, tag) {
            return walk(el, tag, &#039;nextSibling&#039;);
        },

        // Like getNext, but returns a collection of all the matched nextSiblings.
        getAllNext: function (el, tag) {
            return walk(el, tag, &#039;nextSibling&#039;, null, true);
        },

        // Works as getPrevious, but tries to find the firstChild (excluding text nodes).
        getFirst: function (el, tag) {
            return walk(el, tag, &#039;nextSibling&#039;, &#039;firstChild&#039;);
        },

        // Works as getPrevious, but tries to find the lastChild.
        getLast: function (el, tag) {
            return walk(el, tag, &#039;previousSibling&#039;, &#039;lastChild&#039;);
        },

        // Works as getPrevious, but tries to find the parentNode.
        getParent: function (el, tag) {
            return walk(el, tag, &#039;parentNode&#039;);
        },

        // Like getParent, but returns a collection of all the matched parentNodes up the tree.
        getParents: function (el, tag) {
            return walk(el, tag, &#039;parentNode&#039;, null, true);
        },

        // Returns all the Element&#039;s children (excluding text nodes).
        getChildren: function (el, tag) {
            return walk(el, tag, &#039;nextSibling&#039;, &#039;firstChild&#039;, true);
        },

        // Removes the Element from the DOM.
        dispose: function (el) {
            el = get(el);
            return (el.parentNode) ? el.parentNode.removeChild(el) : el;
        }

    };
}();
</pre>
<p>To get the parent element and its first child, we then append the following code :</p>
<pre class="brush: javascript">
// get drop down menu
var parentNode = DOM.get(&#039;fb_menu_friends_dropdown&#039;);
// get its first child
var firstNode = DOM.getFirst(&#039;fb_menu_friends_dropdown&#039;);
</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="brush: javascript">
/** For &quot;Recently Updated&quot; */
// create our div with class fb_menu_item
var recentDiv = document.createElement(&#039;div&#039;);
recentDiv.setAttribute(&#039;class&#039;, &#039;fb_menu_item&#039;);

// create our link
var recentLink = document.createElement(&#039;a&#039;);
recentLink.href = &#039;http://www.facebook.com/friends/?recent&amp;ref=tn&#039;;

// add text to our link
var recentDivContent = document.createTextNode(&#039;Recently Updated&#039;);
recentLink.appendChild(recentDivContent);

// add link to our div
recentDiv.appendChild(recentLink);

/** For &quot;Status Updates&quot; */
// create our div with class fb_menu_item
var statusDiv = document.createElement(&#039;div&#039;);
statusDiv.setAttribute(&#039;class&#039;, &#039;fb_menu_item&#039;);

// create our link
var statusLink = document.createElement(&#039;a&#039;);
statusLink.href = &#039;http://www.facebook.com/friends/?status&amp;ref=tn&#039;;

// add text to our link
var statusDivContent = document.createTextNode(&#039;Status Updates&#039;);
statusLink.appendChild(statusDivContent);

// add link to our div
statusDiv.appendChild(statusLink);
</pre>
<p>We can also add a separator, for having a cleaner look :</p>
<pre class="brush: javascript">
/** Creates a separator, just to look good */
var separatorDiv = document.createElement(&#039;div&#039;);
separatorDiv.setAttribute(&#039;class&#039;, &#039;fb_menu_separator&#039;);
</pre>
<p>Finally we insert these 3 nodes before the first child element we got earlier on :</p>
<pre class="brush: javascript">
// add all divs before first child of menu
parentNode.insertBefore(statusDiv, firstNode);
parentNode.insertBefore(recentDiv, firstNode);
parentNode.insertBefore(separatorDiv, firstNode);
</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="brush: javascript">
// ==UserScript==
// @name          My First GM script
// @namespace     http://htmlblog.net
// @description   basic &lt;a href=&quot;https://addons.mozilla.org/firefox/addon/748&quot; target=&quot;_blank&quot;&gt;Greasemonkey&lt;/a&gt; script
// @include       http://www.facebook.com/
// ==/UserScript==

DOM = function () {

    function get(id) {
        if (id &amp;&amp; typeof id === &#039;string&#039;) {
            id = document.getElementById(id);
        }
        return id || null;
    }

    function walk(element, tag, walk, start, all) {
        var el = get(element)[start || walk], elements = all ? [] : null;
        while (el) {
            if (el.nodeType === 1 &amp;&amp; (!tag || el.tagName.toLowerCase() === tag)) {
                if (!all) {
                    return el;
                }
                elements.push(el);
            }
            el = el[walk];
        }
        return elements;
    }

    return {

        // Get the element by its id
        get: get,

        walk: walk,

        // Returns the previousSibling of the Element (excluding text nodes).
        getPrevious: function (el, tag) {
            return walk(el, tag, &#039;previousSibling&#039;);
        },

        // Like getPrevious, but returns a collection of all the matched previousSiblings.
        getAllPrevious: function (el, tag) {
            return walk(el, tag, &#039;previousSibling&#039;, null, true);
        },

        // As getPrevious, but tries to find the nextSibling (excluding text nodes).
        getNext: function (el, tag) {
            return walk(el, tag, &#039;nextSibling&#039;);
        },

        // Like getNext, but returns a collection of all the matched nextSiblings.
        getAllNext: function (el, tag) {
            return walk(el, tag, &#039;nextSibling&#039;, null, true);
        },

        // Works as getPrevious, but tries to find the firstChild (excluding text nodes).
        getFirst: function (el, tag) {
            return walk(el, tag, &#039;nextSibling&#039;, &#039;firstChild&#039;);
        },

        // Works as getPrevious, but tries to find the lastChild.
        getLast: function (el, tag) {
            return walk(el, tag, &#039;previousSibling&#039;, &#039;lastChild&#039;);
        },

        // Works as getPrevious, but tries to find the parentNode.
        getParent: function (el, tag) {
            return walk(el, tag, &#039;parentNode&#039;);
        },

        // Like getParent, but returns a collection of all the matched parentNodes up the tree.
        getParents: function (el, tag) {
            return walk(el, tag, &#039;parentNode&#039;, null, true);
        },

        // Returns all the Element&#039;s children (excluding text nodes).
        getChildren: function (el, tag) {
            return walk(el, tag, &#039;nextSibling&#039;, &#039;firstChild&#039;, true);
        },

        // Removes the Element from the DOM.
        dispose: function (el) {
            el = get(el);
            return (el.parentNode) ? el.parentNode.removeChild(el) : el;
        }

    };
}();

try{
	// get drop down menu
	var parentNode = DOM.get(&#039;fb_menu_friends_dropdown&#039;);
	// get its first child
	var firstNode = DOM.getFirst(&#039;fb_menu_friends_dropdown&#039;);

	/** For &quot;Recently Updated&quot; */
	// create our div with class fb_menu_item
	var recentDiv = document.createElement(&#039;div&#039;);
	recentDiv.setAttribute(&#039;class&#039;, &#039;fb_menu_item&#039;);

	// create our link
	var recentLink = document.createElement(&#039;a&#039;);
	recentLink.href = &#039;http://www.facebook.com/friends/?recent&amp;ref=tn&#039;;

	// add text to our link
	var recentDivContent = document.createTextNode(&#039;Recently Updated&#039;);
	recentLink.appendChild(recentDivContent);

	// add link to our div
	recentDiv.appendChild(recentLink);

	/** For &quot;Status Updates&quot; */
	// create our div with class fb_menu_item
	var statusDiv = document.createElement(&#039;div&#039;);
	statusDiv.setAttribute(&#039;class&#039;, &#039;fb_menu_item&#039;);

	// create our link
	var statusLink = document.createElement(&#039;a&#039;);
	statusLink.href = &#039;http://www.facebook.com/friends/?status&amp;ref=tn&#039;;

	// add text to our link
	var statusDivContent = document.createTextNode(&#039;Status Updates&#039;);
	statusLink.appendChild(statusDivContent);

	// add link to our div
	statusDiv.appendChild(statusLink);

	/** Creates a separator, just to look good */
	var separatorDiv = document.createElement(&#039;div&#039;);
	separatorDiv.setAttribute(&#039;class&#039;, &#039;fb_menu_separator&#039;);

	// add both divs before first child of menu
	parentNode.insertBefore(statusDiv, firstNode);
	parentNode.insertBefore(recentDiv, firstNode);
	parentNode.insertBefore(separatorDiv, firstNode);
}
catch(e){};
</pre>
<h3 class="post-title">Installing and testing our script</h3>
<p>Fire up Firefox and select <strong>&#8220;File&#8221; -> &#8220;Open File&#8230;&#8221;</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 &#8220;Friends&#8221; menu, you will see your newly created menus.</p>
<p><a href="http://htmlblog.net/demo/greasemonkey/fb_recently_updated_link.user.zip"><strong>Download script</strong></a><br />
<a href="http://htmlblog.net/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>9</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&#8217;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. The HTML markup for our menu is : &#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; We use 2 div tags, [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I&#8217;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><span id="more-22"></span><br />
The HTML markup for our menu is :</p>
<pre class="brush: xhtml">
&lt;div id=&quot;menu&quot;&gt;
			&lt;h1 id=&quot;menu-title&quot;&gt;Menu&lt;/h1&gt;
			&lt;div id=&quot;menu-links&quot;&gt;
				&lt;ul&gt;
					&lt;li&gt;Home&lt;/li&gt;
					&lt;li&gt;About&lt;/li&gt;
					&lt;li&gt;CSS&lt;/li&gt;
				&lt;/ul&gt;
			&lt;/div&gt;
		&lt;/div&gt;
</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="brush: css">
/** width of the menu */
#menu{
				width: 180px;
			}

/** the main title */
			#menu h1{
				line-height: 35px;
				color: #fff;
				/** simulate link */
				cursor: pointer;
				font-weight: bold;
				/** background image will change when menu expands/collapses */
				background: #666 url(images/block-arrow-expanded.gif) no-repeat scroll 30px 13px;
			}

/** the links */
			#menu-links li{
				line-height: 35px;
				border-bottom:1px solid #ddd;
				background-color: #eee;
			}
</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&#8217;ll be using the block-arrow-collapsed.gif image and when the menu expands we&#8217;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>To do the collapsing/expanding effects, we need to include the following javascript files beforehand :</p>
<pre class="brush: xhtml">
&lt;!-- js --&gt;
&lt;script type=&quot;text/javascript&quot;  src=&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;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;js/tools-min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;js/effects-min.js&quot;&gt;&lt;/script&gt;
</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="brush: javascript">
&lt;script type=&quot;text/javascript&quot;&gt;
			YAHOO.util.Event.addListener(&#039;menu-title&#039;, &#039;click&#039;, function(){
				if(YAHOO.util.Dom.getStyle(&#039;menu-links&#039;, &#039;display&#039;) == &#039;block&#039;){
					new YAHOO.widget.Effects.BlindUp(&#039;menu-links&#039;, {seconds: 0.2});
					YAHOO.util.Dom.setStyle(&#039;menu-title&#039;, &#039;background-image&#039;, &#039;url(images/block-arrow-collapsed.gif)&#039;);
				}
				else{
					new YAHOO.widget.Effects.BlindDown(&#039;menu-links&#039;, {seconds: 0.2});
					YAHOO.util.Dom.setStyle(&#039;menu-title&#039;, &#039;background-image&#039;, &#039;url(images/block-arrow-expanded.gif)&#039;);
				}
			});
		&lt;/script&gt;
</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&#8217;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="brush: javascript">
new YAHOO.widget.Effects.BlindDown(&#039;menu-links&#039;, {seconds: 0.2});
</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="brush: javascript">

YAHOO.util.Dom.setStyle(&#039;menu-title&#039;, &#039;background-image&#039;, &#039;url(images/block-arrow-expanded.gif)&#039;);
</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>
]]></content:encoded>
			<wfw:commentRss>http://htmlblog.net/expanding-collapsing-javascript-menu/feed/</wfw:commentRss>
		<slash:comments>34</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 : &#60;!-- CSS --&#62; &#60;link rel=&#34;stylesheet&#34; type=&#34;text/css&#34; href=&#34;stepper/stepper.css&#34; /&#62; &#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; &#60;!-- Stepper source file --&#62; &#60;script [...]]]></description>
			<content:encoded><![CDATA[<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="brush: xhtml">
&lt;!-- CSS --&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;stepper/stepper.css&quot; /&gt;

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

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

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

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

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

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

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

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

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

