<?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; Javascript</title>
	<atom:link href="http://htmlblog.net/tag/javascript/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>WP To Top &#8211; a Wordpress plugin that takes you to the top</title>
		<link>http://htmlblog.net/wp-to-top/</link>
		<comments>http://htmlblog.net/wp-to-top/#comments</comments>
		<pubDate>Thu, 05 Mar 2009 17:23:10 +0000</pubDate>
		<dc:creator>asvin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[scroll]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wp to top]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://htmlblog.net/?p=110</guid>
		<description><![CDATA[About WP To Top
WP To Top is a Wordpress plugin that adds a "Back to top" link in your blog without modifying your template files. This is useful especially if you have long posts or long pages. You will have a nice "Back to top" or whatever-text-you-want link floating at the bottom right/left of your [...]]]></description>
			<content:encoded><![CDATA[<h3 class="post-title">About WP To Top</h3>
<p><a href="http://wordpress.org/extend/plugins/wp-to-top/" target="_blank">WP To Top</a> is a <a href="http://www.wordpress.org" target="_blank">Wordpress</a> plugin that adds a "Back to top" link in your blog without modifying your template files. This is useful especially if you have long posts or long pages. You will have a nice "Back to top" or whatever-text-you-want link floating at the bottom right/left of your page.</p>
<h3 class="post-title">Features</h3>
<ul>
<li>Smooth scrolling animation and fade in, fade out effect, powered by the YUI library</li>
<li>Customizable options via the admin panel, from the text to the position of the link</li>
<li>Works on almost all browsers including IE6 (yes!)</li>
</ul>
<h3 class="post-title">Download</h3>
<p><a href="http://wordpress.org/extend/plugins/wp-to-top/" target="_blank">You can get the plugin here</a></p>
<h3 class="post-title">Demo</h3>
<p>You have a demo on this page. Just scroll a little bit and you will see a nice "Take me up" link appearing at the bottom of your page.</p>
<h3 class="post-title">Installation</h3>
<ul>
<li>Extract <a href="http://wordpress.org/extend/plugins/wp-to-top/" target="_blank">wptotop.zip</a> in the "/wp-content/plugins/" directory</li>
<li>Activate the plugin through the "Plugins" menu in <a href="http://www.wordpress.org" target="_blank">WordPress</a></li>
<li>Go to "Settings" and then "WP To Top" to configure the plugin. The options are self explanatory and easy to understand.</li>
</ul>
<h3 class="post-title">Thanks</h3>
<p>I got the inspiration to write this plugin after having a look at <a href="http://davidwalsh.name/jquery-top-link" target="_blank">David Walsh's jQuery topLink Plugin</a>. Thanks to the <a href="http://developer.yahoo.com/yui" target="_blank">YUI team</a> for the great YUI library and <a href="http://semihhazar.com/blog/animated-page-scroll-with-yui/" target="_blank">Semih's Animated Page Scroll with YUI</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://htmlblog.net/wp-to-top/feed/</wfw:commentRss>
		<slash:comments>49</slash:comments>
		</item>
		<item>
		<title>twitree.com &#8211; see on which leaves the birds are&#8230;</title>
		<link>http://htmlblog.net/twitreecom-see-on-which-leaves-the-birds-are/</link>
		<comments>http://htmlblog.net/twitreecom-see-on-which-leaves-the-birds-are/#comments</comments>
		<pubDate>Mon, 29 Dec 2008 12:01:02 +0000</pubDate>
		<dc:creator>asvin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[twitree]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[visualization]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://htmlblog.net/?p=81</guid>
		<description><![CDATA[I am happy to announce the launch of twitree.com. It's a twitter application which allows a twitterer to view his followers in a tree-like navigation, which can be expanded.

twitree makes extensive use of the YUI library. The following components were used :

grids css - for the layout
yahoo dom event - for DOM and event handling
connection [...]]]></description>
			<content:encoded><![CDATA[<p>I am happy to announce the launch of <a href="http://twitree.com/">twitree.com</a>. It's a <a href="http://twitter.com">twitter</a> application which allows a twitterer to view his followers in a tree-like navigation, which can be expanded.</p>
<p><a href="http://twitree.com/"><img src="http://htmlblog.net/wp-content/uploads/2008/12/twitree-300x116.jpg" alt="twitree" title="twitree" width="300" height="116" class="aligncenter size-medium wp-image-82" /></a></p>
<p><a href="http://twitree.com/">twitree</a> makes extensive use of the <a href="http://developer.yahoo.com/yui">YUI library</a>. The following components were used :</p>
<ul>
<li><a href="http://developer.yahoo.com/yui/grids">grids css</a> - for the layout</li>
<li><a href="http://developer.yahoo.com/yui/yahoo/">yahoo</a> <a href="http://developer.yahoo.com/yui/dom/">dom</a> <a href="http://developer.yahoo.com/yui/event/">event</a> - for DOM and event handling</li>
<li><a href="http://developer.yahoo.com/yui/connection/">connection manager</a> - for AJAX requests like getting the list of followers for a user</li>
<li><a href="http://developer.yahoo.com/yui/container/">container family</a> - to display modal dialogs like prompting for a twitter username, displaying the loading panel</li>
<li><a href="http://developer.yahoo.com/yui/json/">JSON utility</a> - to parse the JSON data twitree receives from the twitter servers</li>
<li><a href="http://developer.yahoo.com/yui/cookie/">cookie utility</a> - to fetch username information from a cookie so that the user doesn't have to input his username every time</li>
<li>and finally the <a href="http://developer.yahoo.com/yui/treeview/">treeview control</a> which provides a nice tree and dynamically loads the data upon clicking on a node</li>
</ul>
<p>The application is very much in an initial phase and there are some limitations, like only 100 followers are being returned, <del datetime="2009-01-07T06:10:37+00:00">rate limiting</del>, etc...</p>
<p>Stay tuned for further information</p>
<p><strong>Latest update :</strong></p>
<p>twitterers can now </p>
<ul>
<li>update their status via twitree</li>
<li>follow a user by right clicking on the user and choose "Follow" from the contextual menu</li>
<li>send a direct message to a fellow twitterer by right clicking on the user and choose "Send message" from the contextual menu</li>
</ul>
<p>FAQ has also been added</p>
]]></content:encoded>
			<wfw:commentRss>http://htmlblog.net/twitreecom-see-on-which-leaves-the-birds-are/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Alternate colors to table rows with javascript (YUI)</title>
		<link>http://htmlblog.net/alternate-colors-to-table-rows-with-javascript-yui/</link>
		<comments>http://htmlblog.net/alternate-colors-to-table-rows-with-javascript-yui/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 16:49:36 +0000</pubDate>
		<dc:creator>asvin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[alternate rows]]></category>
		<category><![CDATA[table]]></category>
		<category><![CDATA[yahoo]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://htmlblog.net/?p=71</guid>
		<description><![CDATA[
This article will show you how to add alternate colors to table rows without any server side development or hard coding your scripts, thus providing good readability for your users and great flexibility for you. We will use the YUI library for that.

Demo
Download the demo
The HTML

We need to create our basic HTML file containing a [...]]]></description>
			<content:encoded><![CDATA[<p><a href="/demo/alternate/" target="_blank"><img src="http://htmlblog.net/wp-content/uploads/2008/12/colors.png" alt="" title="colors" width="411" height="323" class="aligncenter size-full wp-image-72" /></a></p>
<p>This article will show you how to add alternate colors to table rows without any server side development or hard coding your scripts, thus providing good readability for your users and great flexibility for you. We will use the <a href="http://developer.yahoo.com/yui" target="_blank">YUI library</a> for that.<br />
<span id="more-71"></span></p>
<p><a href="/demo/alternate/" target="_blank">Demo</a><br />
<a href="/demo/alternate/alternate.zip" target="_blank">Download the demo</a></p>
<p style="font-size:20px;font-weight:bold;">The HTML
<p>
We need to create our basic HTML file containing a table and adding a class name to it.</p>
<pre class="html4strict">&nbsp;
<span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;</span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">&lt;html&gt;</span></a></span>
	<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>
		<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>
		<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>Alternate colors<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head&gt;</span></span>
	<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>
<span style="color: #009900;"><a href="http://december.com/html/4/element/table.html"><span style="color: #000000; font-weight: bold;">&lt;table</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;alternateRows&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/tr.html"><span style="color: #000000; font-weight: bold;">&lt;tr&gt;</span></a></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000; font-weight: bold;">&lt;td&gt;</span></a></span>First Row<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/tr.html"><span style="color: #000000; font-weight: bold;">&lt;tr&gt;</span></a></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000; font-weight: bold;">&lt;td&gt;</span></a></span>Second Row<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/tr.html"><span style="color: #000000; font-weight: bold;">&lt;tr&gt;</span></a></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000; font-weight: bold;">&lt;td&gt;</span></a></span>Third Row<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/tr.html"><span style="color: #000000; font-weight: bold;">&lt;tr&gt;</span></a></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000; font-weight: bold;">&lt;td&gt;</span></a></span>Fourth Row<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/tr.html"><span style="color: #000000; font-weight: bold;">&lt;tr&gt;</span></a></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000; font-weight: bold;">&lt;td&gt;</span></a></span>Fifth Row<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/table&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html&gt;</span></span>
&nbsp;</pre>
<p style="font-size:20px;font-weight:bold;">The Javascript</p>
<p>Next we need to include the <a href="http://developer.yahoo.com/yui" target="_blank">YUI libraries</a>, served from their <a href="http://developer.yahoo.com/yui/articles/hosting/" target="_blank">CDN</a>. We need the</p>
<ul>
<li><a href="http://developer.yahoo.com/yui/yahoo/" target="_blank">YAHOO global object</a> which contains a number of methods that are used throughout the library</li>
<li><a href="http://developer.yahoo.com/yui/dom/" target="_blank">DOM collection</a> which provides methods that simplify common DOM-scripting tasks, including element positioning and CSS style management</li>
<li><a href="http://developer.yahoo.com/yui/event/" target="_blank">Event utility</a> for various event handling</li>
</ul>
<pre class="html4strict">&nbsp;
<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Combo-handled YUI JS files: --&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/combo?2.6.0/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;</pre>
<p>And also our javascript file, <a href="/demo/alternate/alternate.zip" target="_blank">alternate.js</a></p>
<pre class="html4strict">&nbsp;
<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;alternate.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>So our HTML page looks like this:</p>
<pre class="html4strict">&nbsp;
<span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;</span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">&lt;html&gt;</span></a></span>
	<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>
		<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>
		<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Combo-handled YUI JS files: --&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/combo?2.6.0/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>
		<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;alternate.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>
		<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>Alternate colors<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head&gt;</span></span>
	<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>
<span style="color: #009900;"><a href="http://december.com/html/4/element/table.html"><span style="color: #000000; font-weight: bold;">&lt;table</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;alternateRows&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/tr.html"><span style="color: #000000; font-weight: bold;">&lt;tr&gt;</span></a></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000; font-weight: bold;">&lt;td&gt;</span></a></span>First Row<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/tr.html"><span style="color: #000000; font-weight: bold;">&lt;tr&gt;</span></a></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000; font-weight: bold;">&lt;td&gt;</span></a></span>Second Row<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/tr.html"><span style="color: #000000; font-weight: bold;">&lt;tr&gt;</span></a></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000; font-weight: bold;">&lt;td&gt;</span></a></span>Third Row<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/tr.html"><span style="color: #000000; font-weight: bold;">&lt;tr&gt;</span></a></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000; font-weight: bold;">&lt;td&gt;</span></a></span>Fourth Row<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/tr.html"><span style="color: #000000; font-weight: bold;">&lt;tr&gt;</span></a></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000; font-weight: bold;">&lt;td&gt;</span></a></span>Fifth Row<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/table&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html&gt;</span></span>
&nbsp;</pre>
<p style="font-size:20px;font-weight:bold;">Invoking the javascript</p>
<p>To add the alternate colors to your table rows, you just have to put the following javascript code in your HTML page.</p>
<pre class="javascript">&nbsp;
	YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">on</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>
		YAHOO.<span style="color: #006600;">htmlblog</span>.<span style="color: #006600;">alternateRows</span>.<span style="color: #006600;">init</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'alternateRows'</span>, <span style="color: #3366CC;">'#fff'</span>, <span style="color: #3366CC;">'#ccc'</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>That means that after the page has been loaded, we call the alternateRows.init function, with 3 parameters:</p>
<ul>
<li>the table class name, in our case it's "alternateRows"</li>
<li>the first color in hex, e.g #fff</li>
<li>the second color in hex, e.g #ccc</li>
</ul>
<p><br/><br />
<br/></p>
<p style="font-size:20px;font-weight:bold;">Our complete HTML page</p>
<pre class="html4strict">&nbsp;
<span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;</span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">&lt;html&gt;</span></a></span>
	<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>
		<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>
		<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Combo-handled YUI JS files: --&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/combo?2.6.0/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>
		<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;alternate.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>
		<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>
			YAHOO.util.Event.on(window, 'load', function(){
				YAHOO.htmlblog.alternateRows.init('alternateRows', '#fff', '#ccc');
			});
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span>
		<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>Alternate colors<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head&gt;</span></span>
	<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>
<span style="color: #009900;"><a href="http://december.com/html/4/element/table.html"><span style="color: #000000; font-weight: bold;">&lt;table</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;alternateRows&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/tr.html"><span style="color: #000000; font-weight: bold;">&lt;tr&gt;</span></a></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000; font-weight: bold;">&lt;td&gt;</span></a></span>First Row<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/tr.html"><span style="color: #000000; font-weight: bold;">&lt;tr&gt;</span></a></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000; font-weight: bold;">&lt;td&gt;</span></a></span>Second Row<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/tr.html"><span style="color: #000000; font-weight: bold;">&lt;tr&gt;</span></a></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000; font-weight: bold;">&lt;td&gt;</span></a></span>Third Row<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/tr.html"><span style="color: #000000; font-weight: bold;">&lt;tr&gt;</span></a></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000; font-weight: bold;">&lt;td&gt;</span></a></span>Fourth Row<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/tr.html"><span style="color: #000000; font-weight: bold;">&lt;tr&gt;</span></a></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/td.html"><span style="color: #000000; font-weight: bold;">&lt;td&gt;</span></a></span>Fifth Row<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/table&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html&gt;</span></span>
&nbsp;</pre>
<p><a href="/demo/alternate/" target="_blank">Demo</a><br />
<a href="/demo/alternate/alternate.zip" target="_blank">Download the demo</a></p>
]]></content:encoded>
			<wfw:commentRss>http://htmlblog.net/alternate-colors-to-table-rows-with-javascript-yui/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Bubble menu javascript or playing with YUI&#8217;s event delegation</title>
		<link>http://htmlblog.net/bubble-menu-javascript-or-playing-with-yuis-event-delegation/</link>
		<comments>http://htmlblog.net/bubble-menu-javascript-or-playing-with-yuis-event-delegation/#comments</comments>
		<pubDate>Mon, 24 Nov 2008 15:10:41 +0000</pubDate>
		<dc:creator>asvin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[event delegation]]></category>
		<category><![CDATA[menu]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://htmlblog.net/?p=63</guid>
		<description><![CDATA[
Image courtesy of jobee59's photostream
Event delegation refers to the use of a single event listener on a parent object to listen for events happening on its children (or deeper descendants). Event delegation allows developers to be sparse in their application of event listeners while still reacting to events as they happen on highly specific targets. [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://farm1.static.flickr.com/199/469216645_020fc74cce.jpg?v=0" alt="bubble" /><br />
Image courtesy of <a href="http://flickr.com/photos/jobee59/469216645/" target="_blank">jobee59's photostream</a></p>
<p>Event delegation refers to the use of a single event listener on a parent object to listen for events happening on its children (or deeper descendants). Event delegation allows developers to be sparse in their application of event listeners while still reacting to events as they happen on highly specific targets. This proves to be a key strategy for maintaining high performance in event-rich web projects, where the creation of hundreds of event listeners can quickly degrade performance. <a href="http://developer.yahoo.com/yui/examples/event/event-delegation.html" target="_blank">More on event delegation including examples</a>.</p>
<p>This post illustrates the use of event delegation to create a "bubble" menu, inspired by <a href="http://nettuts.com/html-css-techniques/how-to-create-a-mootools-homepage-inspired-navigation-effect-using-jquery/" target="_blank">Bedrich Rios' post on Nettuts</a>.<br />
<span id="more-63"></span><br />
<a href="/demo/bubble/" target="_blank">Demo</a><br />
<a href="/demo/bubble/bubble.zip">Download sample file</a><br />
<br/><br />
<br/></p>
<p class="headline"><strong>The HTML</strong></p>
<p>We'll need the following components from the <a href="http://developer.yahoo.com/yui" target="_blank">YUI library</a>:</p>
<ul>
<li><a href="http://developer.yahoo.com/yui/dom/" target="_blank">yahoo-dom-event</a> - for DOM/Event handling</li>
<li><a href="http://developer.yahoo.com/yui/animation/" target="_blank">animation</a> - for sliding effects </li>
</ul>
<p>So our basic HTML page is like this:</p>
<pre class="html4strict">&nbsp;
<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>
<span style="color: #009900;"><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">&lt;html&gt;</span></a></span>
	<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>
		<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>
		<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Combo-handled YUI JS files: --&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/combo?2.6.0/build/yahoo-dom-event/yahoo-dom-event.js&amp;2.6.0/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>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head&gt;</span></span>
	<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>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html&gt;</span></span>
&nbsp;</pre>
<p>Next we'll add some structural markup, a simple list, with "menu" as class name and with 5 items.</p>
<pre class="html4strict">&nbsp;
<span style="color: #009900;"><a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">&lt;ul</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;menu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<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>Menu item 1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<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>Menu item 2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<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>Menu item 3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<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>Menu item 4<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<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>Menu item 5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul&gt;</span></span>
&nbsp;</pre>
<p>Updated HTML page:</p>
<pre class="html4strict">&nbsp;
<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>
<span style="color: #009900;"><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">&lt;html&gt;</span></a></span>
	<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>
		<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>
		<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Combo-handled YUI JS files: --&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/combo?2.6.0/build/yahoo-dom-event/yahoo-dom-event.js&amp;2.6.0/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>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head&gt;</span></span>
	<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>
<span style="color: #009900;"><a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">&lt;ul</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;menu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<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>Menu item 1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<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>Menu item 2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<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>Menu item 3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<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>Menu item 4<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<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>Menu item 5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html&gt;</span></span>
&nbsp;</pre>
<p class="headline"><strong>The Javascript</strong></p>
<p>Then we just have to add the <a href="/demo/bubble/bubble.zip" target="_blank">bubble.js</a> javascript file to our page and it's done, we'll have a nice "bubble" menu.</p>
<pre class="html4strict">&nbsp;
<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>
<span style="color: #009900;"><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">&lt;html&gt;</span></a></span>
	<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>
		<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>
		<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Combo-handled YUI JS files: --&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/combo?2.6.0/build/yahoo-dom-event/yahoo-dom-event.js&amp;2.6.0/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>
		<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- our bubble 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;bubble.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>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head&gt;</span></span>
	<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>
<span style="color: #009900;"><a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">&lt;ul</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;menu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<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>Menu item 1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<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>Menu item 2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<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>Menu item 3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<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>Menu item 4<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<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>Menu item 5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html&gt;</span></span>
&nbsp;</pre>
<p class="headline"><strong>bubble.js</strong></p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">/**
*	Bubble Menu
*	@author Asvin Balloo (http://htmlblog.net)
*/</span>
bubbleMenu = <span style="color: #66cc66;">&#123;</span>
	<span style="color: #009900; font-style: italic;">// private variables</span>
	<span style="color: #009900; font-style: italic;">// class name of our menu</span>
	menuClassName: <span style="color: #003366; font-weight: bold;">null</span>,
	<span style="color: #009900; font-style: italic;">// by how much we are sliding</span>
	moveToLength : <span style="color: #CC0000;">10</span>,
&nbsp;
	<span style="color: #009900; font-style: italic;">/**
	*	Init the whole process here
	*	@method init
	*	@params menuClassName {string} class name of our menus
	*/</span>
	init: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>menuClassName<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #009900; font-style: italic;">// set our class name to be used later one</span>
		bubbleMenu.<span style="color: #006600;">menuClassName</span> = menuClassName;
&nbsp;
		<span style="color: #009900; font-style: italic;">// get all the lists with the class name</span>
		<span style="color: #003366; font-weight: bold;">var</span> list = YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">getElementsByClassName</span><span style="color: #66cc66;">&#40;</span>menuClassName<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #009900; font-style: italic;">// loop through them</span>
		<span style="color: #000066; font-weight: bold;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i=<span style="color: #CC0000;">0</span>; i
&lt;list.<span style="color: #006600;">length</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #009900; font-style: italic;">// add the onmouseover event to it, call the mouseOverHandler function</span>
			YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">on</span><span style="color: #66cc66;">&#40;</span>list<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>, <span style="color: #3366CC;">&quot;mouseover&quot;</span>, bubbleMenu.<span style="color: #006600;">mouseOverHandler</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #009900; font-style: italic;">// get the first child and its padding left for future reference. Useful when using onmouseout to </span>
&nbsp;
restore the <span style="color: #000066; font-weight: bold;">item</span> to original state
			<span style="color: #003366; font-weight: bold;">var</span> listItems = list<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">getElementsByTagName</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #003366; font-weight: bold;">var</span> startingPadding = 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>listItems<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #3366CC;">'paddingLeft'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #009900; font-style: italic;">// add the onmouseout event, call the mouseOutHandler function</span>
			YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">on</span><span style="color: #66cc66;">&#40;</span>list<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>, <span style="color: #3366CC;">&quot;mouseout&quot;</span>, bubbleMenu.<span style="color: #006600;">mouseOutHandler</span>, <span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">'list'</span>:list<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>, <span style="color: #3366CC;">&quot;padding&quot;</span> : 
&nbsp;
startingPadding<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>,
&nbsp;
	<span style="color: #009900; font-style: italic;">/**
	*	Mouse over handler
	*	@params e {Event} the event
	*/</span>
	mouseOverHandler: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #009900; font-style: italic;">// get the target</span>
		<span style="color: #003366; font-weight: bold;">var</span> elTarget = YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">getTarget</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #009900; font-style: italic;">// calculate the current padding left</span>
		<span style="color: #003366; font-weight: bold;">var</span> paddingLeft = 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>elTarget, <span style="color: #3366CC;">'paddingLeft'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #009900; font-style: italic;">// call the delegate method, with the target and the padding left value</span>
		bubbleMenu.<span style="color: #006600;">delegate</span><span style="color: #66cc66;">&#40;</span>elTarget, paddingLeft<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>,
&nbsp;
	<span style="color: #009900; font-style: italic;">/**
	*	Mouse out handler
	*	@params e {Event} the event
	*	@params o {JSON} JSON data containing the list + padding value, eg. {'list': mylist, 'padding': 10}
	*/</span>
	mouseOutHandler: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>e, o<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #009900; font-style: italic;">// get the target</span>
		<span style="color: #003366; font-weight: bold;">var</span> elTarget = YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">getTarget</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #009900; font-style: italic;">// calculate the left padding, the original padding value - by how much we have padded</span>
		<span style="color: #003366; font-weight: bold;">var</span> paddingLeft = o.<span style="color: #006600;">padding</span> - bubbleMenu.<span style="color: #006600;">moveToLength</span>;
&nbsp;
		<span style="color: #009900; font-style: italic;">// call the delegate method with the target and padding value</span>
		bubbleMenu.<span style="color: #006600;">delegate</span><span style="color: #66cc66;">&#40;</span>elTarget, paddingLeft<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>,
&nbsp;
	<span style="color: #009900; font-style: italic;">/**
	*	Cool things go here, like animation
	*	@params elTarget {Target} our target
	*	@params padding {Int} by how much we are going to padd
	*/</span>
	delegate: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>elTarget, padding<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #009900; font-style: italic;">//walk up the DOM tree looking for an &lt;li&gt; in the target's ancestry; desist when you reach the container with our class name</span>
		<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #66cc66;">&#40;</span>elTarget.<span style="color: #006600;">className</span> != bubbleMenu.<span style="color: #006600;">menuClassName</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #009900; font-style: italic;">// are you an LI?</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>elTarget.<span style="color: #006600;">nodeName</span>.<span style="color: #006600;">toUpperCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #3366CC;">&quot;LI&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				<span style="color: #009900; font-style: italic;">// if so then animate, set the attributes, in our case the left padding</span>
				<span style="color: #003366; font-weight: bold;">var</span> attributes = <span style="color: #66cc66;">&#123;</span> paddingLeft: <span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">&quot;to&quot;</span>: <span style="color: #66cc66;">&#91;</span>padding + bubbleMenu.<span style="color: #006600;">moveToLength</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#125;</span>;
&nbsp;
				<span style="color: #009900; font-style: italic;">// create our animation</span>
				<span style="color: #003366; font-weight: bold;">var</span> anim = <span style="color: #003366; font-weight: bold;">new</span> YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Motion</span><span style="color: #66cc66;">&#40;</span>elTarget, attributes, <span style="color: #CC0000;">0.6</span>, YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Easing</span>.<span style="color: #006600;">bounceOut</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
				<span style="color: #009900; font-style: italic;">// then animate</span>
				anim.<span style="color: #006600;">animate</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #000066; font-weight: bold;">break</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #66cc66;">&#123;</span>
				<span style="color: #009900; font-style: italic;">// it's not an li, so we keep looking and looking</span>
	            elTarget = elTarget.<span style="color: #006600;">parentNode</span>;
	        <span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>;
&nbsp;
<span style="color: #009900; font-style: italic;">// init the whole thing here</span>
YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">on</span><span style="color: #66cc66;">&#40;</span>window, <span style="color: #3366CC;">&quot;load&quot;</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>
	bubbleMenu.<span style="color: #006600;">init</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;menu&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>You can have unlimited (don't abuse) number of bubble menus by just adding the "menu" class to each one of them and including the bubble.js file.<br />
If you're not happy with the menu class or you found another cool class name, then you'll have to update the bubble.js file towards the end:</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// init the whole thing here</span>
YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">on</span><span style="color: #66cc66;">&#40;</span>window, <span style="color: #3366CC;">&quot;load&quot;</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>
	bubbleMenu.<span style="color: #006600;">init</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;mycoolclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Also you can modify by how much an item slides by modifying the moveToLength variable (default 10):</p>
<pre class="javascript">&nbsp;
&nbsp;
<span style="color: #009900; font-style: italic;">// by how much we are sliding</span>
moveToLength : <span style="color: #CC0000;">30</span>,
&nbsp;</pre>
<p>Note that the demo is using reset-fonts-grids CSS and the page has been styled with the following:</p>
<pre class="css">&nbsp;
body <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">margin</span>: <span style="color: #933;">0</span>;
	<span style="color: #000000; font-weight: bold;">padding</span>: <span style="color: #933;">0</span>;
	<span style="color: #000000; font-weight: bold;">background</span>: <span style="color: #cc00cc;">#1d1d1d</span>;
	<span style="color: #000000; font-weight: bold;">font-family</span>: <span style="color: #ff0000;">&quot;Lucida Grande&quot;</span>, Verdana, <span style="color: #993333;">sans-serif</span>;
	<span style="color: #000000; font-weight: bold;">font-size</span>: <span style="color: #933;"><span style="color: #933;">100</span>%</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.menu</span> li<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">padding-left</span>: <span style="color: #933;">20px</span>;
	<span style="color: #000000; font-weight: bold;">line-height</span>: <span style="color: #933;">2em</span>;
	<span style="color: #000000; font-weight: bold;">width</span>: <span style="color: #933;">150px</span>;
	<span style="color: #000000; font-weight: bold;">color</span>: <span style="color: #cc00cc;">#<span style="color: #933;">999</span></span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">background</span>: <span style="color: #cc00cc;">#<span style="color: #933;">222</span></span>;
	<span style="color: #000000; font-weight: bold;">border</span>: <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#1a1a1a</span>;
	-moz-border-radius: <span style="color: #933;">10px</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
]]></content:encoded>
			<wfw:commentRss>http://htmlblog.net/bubble-menu-javascript-or-playing-with-yuis-event-delegation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>8 practical tips to make your web pages faster</title>
		<link>http://htmlblog.net/8-practical-tips-to-make-your-web-pages-faster/</link>
		<comments>http://htmlblog.net/8-practical-tips-to-make-your-web-pages-faster/#comments</comments>
		<pubDate>Thu, 06 Nov 2008 16:36:23 +0000</pubDate>
		<dc:creator>asvin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[fast]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[mod_deflate]]></category>
		<category><![CDATA[mod_expires]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[speed up]]></category>
		<category><![CDATA[yahoo]]></category>

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

Yahoo's Exceptional Performance team has compiled a list of 34 best practices to have faster web pages. In this post I'll show you 8 tips which helped me to get a B grade in YSlow! for web pages that I develop. Currently YSlow's web page analysis is based on 13 identified basic rules that affect [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://farm4.static.flickr.com/3062/2520059957_ae331ae75a.jpg?v=0" alt="" /></p>
<div style="clear:both"></div>
<p><a href="http://developer.yahoo.com/performance/rules.html" target="_blank">Yahoo's Exceptional Performance team</a> has compiled a list of <a href="http://developer.yahoo.com/performance/rules.html" target="_blank">34 best practices</a> to have faster web pages. In this post I'll show you 8 tips which helped me to get a B grade in <a href="http://developer.yahoo.com/yslow/" target="_blank">YSlow!</a> for web pages that I develop. Currently YSlow's web page analysis is based on 13 identified basic rules that affect web page performance. In decreasing order of priority they are:</p>
<ol>
<li><a href="http://developer.yahoo.com/performance/rules.html#num_http" target="_blank">Make Fewer HTTP Requests</a></li>
<li><a href="http://developer.yahoo.com/performance/rules.html#cdn" target="_blank">Use a Content Delivery Network</a></li>
<li><a href="http://developer.yahoo.com/performance/rules.html#expires" target="_blank">Add an Expires Header</a></li>
<li><a href="http://developer.yahoo.com/performance/rules.html#gzip" target="_blank">Gzip Components</a></li>
<li><a href="http://developer.yahoo.com/performance/rules.html#css_top" target="_blank">Put CSS at the Top</a></li>
<li><a href="http://developer.yahoo.com/performance/rules.html#js_bottom" target="_blank">Move Scripts to the Bottom</a></li>
<li><a href="http://developer.yahoo.com/performance/rules.html#css_expressions" target="_blank">Avoid CSS Expressions</a></li>
<li><a href="http://developer.yahoo.com/performance/rules.html#external" target="_blank">Make JavaScript and CSS External</a></li>
<li><a href="http://developer.yahoo.com/performance/rules.html#dns_lookups" target="_blank">Reduce DNS Lookups</a></li>
<li><a href="http://developer.yahoo.com/performance/rules.html#minify" target="_blank">Minify JavaScript</a></li>
<li><a href="http://developer.yahoo.com/performance/rules.html#redirects" target="_blank">Avoid Redirects</a></li>
<li><a href="http://developer.yahoo.com/performance/rules.html#js_dupes" target="_blank">Remove Duplicate Scripts</a></li>
<li><a href="http://developer.yahoo.com/performance/rules.html#etags" target="_blank">Configure ETags</a></li>
</ol>
<p>You can get more info by clicking on each item. Of course using a <a href="http://en.wikipedia.org/wiki/Content_Delivery_Network" target="_blank">CDN</a> is not affordable by everybody, including me. This didn't prevent me to have a B grade though. I will guide you through some of the items which worked for me.<br />
<span id="more-40"></span></p>
<hr/>
<strong>Make Fewer HTTP Requests</strong><br />
If you're using the <a href="http://developer.yahoo.com" target="_blank">YUI library</a>, the good news is that recently the team graciously offered a <a href="http://developer.yahoo.com/yui/articles/hosting/" target="_blank">Combo Handler Service</a>, served from their CDN. This helps us to eliminate HTTP requests by requesting a single file. For example, to use the <a href="http://developer.yahoo.com/yui/editor/" target="_blank">Rich Text editor</a>, it needed 6 separate HTTP requests:</p>
<pre class="html4strict">&nbsp;
<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>
<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/container/container_core-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>
<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/menu/menu-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>
<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/element/element-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>
<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/button/button-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>
<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/editor/editor-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>
&nbsp;</pre>
<p>Now with the Combo Handler service, it has been stripped to only 1 file:</p>
<pre class="html4strict">&nbsp;
<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/container/container_core-min.js&amp;2.5.2/build/menu/menu-min.js&amp;2.5.2/build/element/element-beta-min.js&amp;2.5.2/build/button/button-min.js&amp;2.5.2/build/editor/editor-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>
&nbsp;</pre>
<p>If you're not using the YUI library or not interested in using the Combo Handler service, you can try <a href="http://code.google.com/p/minify/" target="_blank">Minify!</a><br />
<a href="http://code.google.com/p/minify/" target="_blank">Minify</a> is a <a href="http://php.net" target="_blank">PHP5</a> app that can combine multiple CSS or Javascript files, compress their contents (i.e. removal of unnecessary whitespace/comments), and serve the results with HTTP encoding (gzip/deflate) and headers that allow optimal client-side caching.</p>
<p>Another tool which can be helpful is <a href="http://shrinksafe.dojotoolkit.org/" target="_blank">Dojo Shrinksafe</a> which not only minifies your javascript code but also allows you to upload several javascript files and have them compressed in 1 single file.</p>
<p>Another way to make fewer HTTP requests is by using CSS sprites which are big images containing lots of smaller ones. An online tool which can make CSS sprites out of your smaller ones is the <a href="http://spritegen.website-performance.org/" target="_blank">CSS Sprite Generator</a>. You just have to upload your pics and it will generate your sprite together with the CSS code. More info about CSS sprites can be found on <a href="http://css-tricks.com/css-sprites-what-they-are-why-theyre-cool-and-how-to-use-them/" target="_blank">CSS Tricks</a>.</p>
<hr/>
<strong>Add an Expires Header</strong><br />
For this to work on Apache, you'll need to have <a href="http://httpd.apache.org/docs/2.0/mod/mod_expires.html" target="_blank">mod_expires</a> enabled. Then create an .htaccess file or edit your httpd.conf with the following content:</p>
<pre class="apache">&nbsp;
<span style="color: #00007f;">ExpiresActive</span> <span style="color: #0000ff;">On</span>
<span style="color: #00007f;">ExpiresByType</span> image/jpg <span style="color: #7f007f;">&quot;access plus 2 years&quot;</span>
<span style="color: #00007f;">ExpiresByType</span> image/jpeg <span style="color: #7f007f;">&quot;access plus 2 years&quot;</span>
<span style="color: #00007f;">ExpiresByType</span> image/gif <span style="color: #7f007f;">&quot;access plus 2 years&quot;</span>
<span style="color: #00007f;">ExpiresByType</span> application/javascript <span style="color: #7f007f;">&quot;access plus 2 years&quot;</span>
<span style="color: #00007f;">ExpiresByTYpe</span> text/css <span style="color: #7f007f;">&quot;access plus 2 years&quot;</span>
&nbsp;</pre>
<p>In the above listing, all images(jpg/jpeg/gif) will have an expires header 2 years in the future.</p>
<hr/>
<strong>Gzip Components</strong><br />
You must have <a href="http://httpd.apache.org/docs/2.0/mod/mod_deflate.html" target="_blank">mod_deflate</a> enabled and drop these lines in your httpd.conf file</p>
<pre class="apache">&nbsp;
SetOutputFilter DEFLATE
<span style="color: #00007f;">SetEnvIfNoCase</span> Request_URI \.<span style="color: #66cc66;">&#40;</span>?:gif|jpe?g|png<span style="color: #66cc66;">&#41;</span>$ no-gzip dont-vary
<span style="color: #00007f;">SetEnvIfNoCase</span> Request_URI \.<span style="color: #66cc66;">&#40;</span>?:exe|t?gz|zip|bz2|sit|rar<span style="color: #66cc66;">&#41;</span>$ no-gzip dont-vary
<span style="color: #00007f;">SetEnvIfNoCase</span> Request_URI \.pdf$ no-gzip dont-vary
&nbsp;</pre>
<hr/>
<strong>Put CSS at the Top</strong><br />
All CSS must be between your head tag, like that :</p>
<pre class="html4strict">&nbsp;
	<span style="color: #009900;"><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">&lt;html&gt;</span></a></span>
		<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>
<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;">href</span>=<span style="color: #ff0000;">&quot;style.css&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">media</span>=<span style="color: #ff0000;">&quot;screen&quot;</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head&gt;</span></span>
		<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>
&nbsp;
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html&gt;</span></span>
&nbsp;</pre>
<hr/>
<strong>Move Scripts to the Bottom</strong><br />
While the CSS are sitting at the top of your page, javascripts must be moved towards the lower part of your page, typically in the body tag. I usually put them before the closing body tag.</p>
<pre class="html4strict">&nbsp;
	<span style="color: #009900;"><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">&lt;html&gt;</span></a></span>
		<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>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head&gt;</span></span>
		<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>
		...
		...
		<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;myjs.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>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html&gt;</span></span>
&nbsp;</pre>
<hr/>
<strong>Reduce DNS Lookups</strong><br />
I don't usually have more than 2 DNS lookups in my scripts, else YSlow will be removing some points.</p>
<hr/>
<strong>Minify JavaScript</strong><br />
As I mentioned earlier on, <a href="http://shrinksafe.dojotoolkit.org/" target="_blank">Dojo Shrinksafe</a> is an online tool which minifies your code.</p>
<p>Another great command line tool is <a href="http://www.julienlecomte.net/blog/2007/08/11/" target="_blank">Julien Lecomte YUI Compressor</a>. </p>
<p>In order to use it you must have <a href="http://java.sun.com" target="_blank">Java</a> installed (>= 1.4).</p>
<p><a href="http://www.crockford.com/" target="_blank">Douglas Crockford</a> also has something similar in <a href="http://www.crockford.com/javascript/jsmin.html" target="_blank">JSMin</a></p>
<hr/>
<strong>Configure ETags</strong><br />
Just drop this line in your httpd.conf:</p>
<pre class="apache">&nbsp;
FileETag <span style="color: #0000ff;">None</span>
&nbsp;</pre>
<p>;-)</p>
]]></content:encoded>
			<wfw:commentRss>http://htmlblog.net/8-practical-tips-to-make-your-web-pages-faster/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>AJAX image cropper with YUI and PHP</title>
		<link>http://htmlblog.net/ajax-image-cropper-with-yui-and-php/</link>
		<comments>http://htmlblog.net/ajax-image-cropper-with-yui-and-php/#comments</comments>
		<pubDate>Wed, 08 Oct 2008 11:01:55 +0000</pubDate>
		<dc:creator>asvin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[crop]]></category>
		<category><![CDATA[gd]]></category>
		<category><![CDATA[image cropper]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[yui]]></category>

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

This post will show you how to build an AJAX crop image tool using the image cropper control from YUI library and PHP.
Demo
Download sample file

The ImageCropper Control from the YUI library gives you an interactive interface for getting the dimensions to crop an image and using these dimensions in PHP, we can do some cropping.
The [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://htmlblog.net/wp-content/uploads/2008/10/cropper.png" alt="" title="cropper" width="500" height="270" class="alignleft size-full wp-image-36" /></p>
<div style="clear:both"></div>
<p>This post will show you how to build an AJAX crop image tool using the image cropper control from YUI library and PHP.</p>
<p><strong><a href="http://htmlblog.net/demo/cropper/" target="_blank">Demo</a></strong><br />
<strong><a href="http://htmlblog.net/demo/cropper/cropper.zip">Download sample file</a></strong><br />
<span id="more-35"></span><br />
The <a href="http://developer.yahoo.com/yui/imagecropper/">ImageCropper Control</a> from the <a href="http://developer.yahoo.com/yui/">YUI library</a> gives you an interactive interface for getting the dimensions to crop an image and using these dimensions in <a href="http://php.net">PHP</a>, we can do some cropping.</p>
<p>The script we are going to build will </p>
<ul>
<li>allow users to upload an image via AJAX</li>
<li>then allow them to select an area for cropping</li>
<li>lastly, provide a download link for the cropped image.</li>
</ul>
<p>There are 3 files we are going to use </p>
<ul>
<li>index.php - will contain the form for image upload as well as the cropping interface</li>
<li>upload.php - provides uploading functionality</li>
<li>crop.php - provides cropping functionality</li>
</ul>
<p>From a technical point of view, the flow will be like this :</p>
<ol>
<li>user uploads jpg image (index.php)</li>
<li>index.php then posts the image asynchronously to upload.php which will do the upload on the server, returning JSON data containing the image file name, its width and its height.</li>
<li>with the JSON data and innerHTML we put the image in our page</li>
<li>initialize the javascript cropping tool</li>
<li>generate a download link (crop.php)</li>
</ol>
<p><strong><br />
Let's have a look at index.php</strong><br />
The index.php is our main file where users will be able upload images and then download the cropped ones. </p>
<p>We'll need the following components from the YUI library :</p>
<ul>
<li>yahoo-dom-event.js - for DOM manipulation and Event handling</li>
<li>dragdrop - dependency for the image cropper control</li>
<li>element - dependency for the image cropper control</li>
<li>resize - dependency for the image cropper control</li>
<li>connection - for AJAX requests, in our case for image uploads via AJAX</li>
<li>json - for parsing JSON data</li>
<li>imagecropper - our most important control</li>
</ul>
<p>Of course we'll use <a href="http://developer.yahoo.com/yui/articles/hosting/">Yahoo combo handling</a> and add the js to our page along with the CSS needed for the above controls :</p>
<pre class="html4strict">&nbsp;
<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;http://yui.yahooapis.com/2.5.2/build/resize/assets/skins/sam/resize.css&quot;</span> /<span style="color: #000000; font-weight: bold;">&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;http://yui.yahooapis.com/2.5.2/build/imagecropper/assets/skins/sam/imagecropper.css&quot;</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- js --&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/combo?2.5.2/build/yahoo-dom-event/yahoo-dom-event.js&amp;2.5.2/build/dragdrop/dragdrop-min.js&amp;2.5.2/build/element/element-beta-min.js&amp;2.5.2/build/resize/resize-beta-min.js&amp;2.5.2/build/imagecropper/imagecropper-beta-min.js&amp;2.
5.2/build/connection/connection-min.js&amp;2.5.2/build/json/json-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>Next users must be able to upload images via AJAX, so we add a form to our page:</p>
<pre class="html4strict">&nbsp;
<span style="color: #009900;"><a href="http://december.com/html/4/element/form.html"><span style="color: #000000; font-weight: bold;">&lt;form</span></a> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;upload.php&quot;</span> <span style="color: #000066;">enctype</span>=<span style="color: #ff0000;">&quot;multipart/form-data&quot;</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;uploadForm&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;uploadForm&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	Image :
<span style="color: #009900;"><a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">&lt;input</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;uploadImage&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;uploadImage&quot;</span> /<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">&lt;input</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;button&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;uploadButton&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Upload&quot;</span>/<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form&gt;</span></span>
&nbsp;</pre>
<p>We have an onclick event to the upload button to fire the uploading process.</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// add listeners</span>
YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">on</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'uploadButton'</span>, <span style="color: #3366CC;">'click'</span>, uploader.<span style="color: #006600;">carry</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>We'll also need 2 containers :</p>
<ul>
<li>imageContainer - will contain our uploaded image</li>
<li>downloadLink - will contain the download link</li>
</ul>
<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;imageContainer&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;downloadLink&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>Both containers will be updated via innerHTML afterwards.</p>
<p>AJAX upload<br />
For the AJAX upload, <a href="http://thecodecentral.com/2007/09/04/asynchronous-file-upload-yuis-approach">Code Central</a> </p>
<p>has an excellent tutorial which I highly recommend. I took the code sample and modified it a bit to fit my needs. Finally I came up with a nice <a href="http://json.org/">JSON object</a> called uploader which has one method, carry. The latter just posts form data to a specified URL.</p>
<pre class="javascript">&nbsp;
uploader = <span style="color: #66cc66;">&#123;</span>
	carry: <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: #009900; font-style: italic;">// set form</span>
		YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Connect</span>.<span style="color: #006600;">setForm</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'uploadForm'</span>, <span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #009900; font-style: italic;">// upload image</span>
		YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Connect</span>.<span style="color: #006600;">asyncRequest</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'POST'</span>, <span style="color: #3366CC;">'upload.php'</span>, <span style="color: #66cc66;">&#123;</span>
			upload: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>o<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #009900; font-style: italic;">// parse our json data</span>
				<span style="color: #003366; font-weight: bold;">var</span> jsonData = YAHOO.<span style="color: #006600;">lang</span>.<span style="color: #006600;">JSON</span>.<span style="color: #006600;">parse</span><span style="color: #66cc66;">&#40;</span>o.<span style="color: #006600;">responseText</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
				<span style="color: #009900; font-style: italic;">// put image in our image container</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;">'imageContainer'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">innerHTML</span> = <span style="color: #3366CC;">'&lt;img id=&quot;yuiImg&quot; src=&quot;'</span> + jsonData.<span style="color: #006600;">image</span> + <span style="color: #3366CC;">'&quot; width=&quot;'</span> + jsonData.<span style="color: #006600;">width</span> + <span style="color: #3366CC;">'&quot; height=&quot;'</span> + jsonData.<span style="color: #006600;">height</span> + <span style="color: #3366CC;">'&quot; alt=&quot;&quot; /&gt;'</span>;
&nbsp;
				<span style="color: #009900; font-style: italic;">// init our photoshop</span>
				photoshop.<span style="color: #006600;">init</span><span style="color: #66cc66;">&#40;</span>jsonData.<span style="color: #006600;">image</span><span style="color: #66cc66;">&#41;</span>; 
&nbsp;
				<span style="color: #009900; font-style: italic;">// get first cropped image</span>
				photoshop.<span style="color: #006600;">getCroppedImage</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>;
&nbsp;</pre>
<p>When upload is complete, we get the JSON data we mentioned earlier on. For e.g :</p>
<pre class="javascript">&nbsp;
<span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">&quot;image&quot;</span> : <span style="color: #3366CC;">&quot;images/myimage.jpg&quot;</span>, <span style="color: #3366CC;">&quot;width&quot;</span> : <span style="color: #3366CC;">&quot;500&quot;</span>, <span style="color: #3366CC;">&quot;height&quot;</span> : <span style="color: #CC0000;">400</span><span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>With this data and using innerHTML we update our imageContainer div to put our image which will have yuiImg as id :</p>
<pre class="javascript">&nbsp;
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;">'imageContainer'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">innerHTML</span> = <span style="color: #3366CC;">'&lt;img id=&quot;yuiImg&quot; src=&quot;'</span> + jsonData.<span style="color: #006600;">image</span> + <span style="color: #3366CC;">'&quot; width=&quot;'</span> + jsonData.<span style="color: #006600;">width</span> + <span style="color: #3366CC;">'&quot; height=&quot;'</span> + jsonData.<span style="color: #006600;">height</span> + <span style="color: #3366CC;">'&quot; alt=&quot;&quot; /&gt;'</span>;
&nbsp;</pre>
<p>It's very important to specify the image width and height else the image cropper won't work.<br />
Next we initialize another JSON object, photoshop which we'll have a look now.</p>
<p>Our photoshop object</p>
<pre class="javascript">&nbsp;
photoshop = <span style="color: #66cc66;">&#123;</span>
	image: <span style="color: #003366; font-weight: bold;">null</span>,
	crop: <span style="color: #003366; font-weight: bold;">null</span>,
&nbsp;
	init: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>image<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #009900; font-style: italic;">// set our image</span>
		photoshop.<span style="color: #006600;">image</span> = image;
&nbsp;
		<span style="color: #009900; font-style: italic;">// our image cropper from the uploaded image</span>
		photoshop.<span style="color: #006600;">crop</span> = <span style="color: #003366; font-weight: bold;">new</span> YAHOO.<span style="color: #006600;">widget</span>.<span style="color: #006600;">ImageCropper</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'yuiImg'</span><span style="color: #66cc66;">&#41;</span>;
		photoshop.<span style="color: #006600;">crop</span>.<span style="color: #006600;">on</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'moveEvent'</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: #009900; font-style: italic;">// get updated coordinates</span>
			photoshop.<span style="color: #006600;">getCroppedImage</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>,
&nbsp;
	getCroppedImage: <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> coordinates = photoshop.<span style="color: #006600;">getCoordinates</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #003366; font-weight: bold;">var</span> url = <span style="color: #3366CC;">'crop.php?image='</span> + photoshop.<span style="color: #006600;">image</span> + <span style="color: #3366CC;">'&amp;cropStartX='</span> + coordinates.<span style="color: #006600;">left</span> +<span style="color: #3366CC;">'&amp;cropStartY='</span> + coordinates.<span style="color: #006600;">top</span> +<span style="color: #3366CC;">'&amp;cropWidth='</span> + coordinates.<span style="color: #006600;">width</span> +<span style="color: #3366CC;">'&amp;cropHeight='</span> + coordinates.<span style="color: #006600;">height</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;">'downloadLink'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">innerHTML</span> = <span style="color: #3366CC;">'&lt;a href=&quot;'</span> + url + <span style="color: #3366CC;">'&quot;&gt;download cropped image&lt;/a&gt;'</span>;		
&nbsp;
	<span style="color: #66cc66;">&#125;</span>,
&nbsp;
	getCoordinates: <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;">return</span> photoshop.<span style="color: #006600;">crop</span>.<span style="color: #006600;">getCropCoords</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>;
&nbsp;</pre>
<p>The init function iniatializes the YUI image cropper from the uploaded image which has yuiImg as id.</p>
<pre class="javascript">&nbsp;
photoshop.<span style="color: #006600;">crop</span> = <span style="color: #003366; font-weight: bold;">new</span> YAHOO.<span style="color: #006600;">widget</span>.<span style="color: #006600;">ImageCropper</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'yuiImg'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>We also subscribe to the moveEvent for the cropper since we'll need to update the download link for the cropped image. So whenever the image cropper is moved/resized, we call the getCroppedImage function.</p>
<pre class="javascript">&nbsp;
photoshop.<span style="color: #006600;">crop</span>.<span style="color: #006600;">on</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'moveEvent'</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: #009900; font-style: italic;">// get updated coordinates</span>
	photoshop.<span style="color: #006600;">getCroppedImage</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>The getCroppedImage function will generate the download link for the cropped image. To do image cropping in <a href="http://php.net">PHP</a> we'll need</p>
<ul>
<li>the image we want to crop</li>
<li>the X,Y coordinates</li>
<li>height and width of the to be cropped area</li>
</ul>
<p>Fortunately the YUI cropper utility has a function which will give us what we want, it's the getCropCoords() method. So, whenever the getCroppedImage function is called, we get the coordinates of the cropped area, build a URL and finally put the download link in our downloadLink container.</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// get coordinates</span>
<span style="color: #003366; font-weight: bold;">var</span> coordinates = photoshop.<span style="color: #006600;">getCoordinates</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #009900; font-style: italic;">// build our url</span>
<span style="color: #003366; font-weight: bold;">var</span> url = <span style="color: #3366CC;">'crop.php?image='</span> + photoshop.<span style="color: #006600;">image</span> + <span style="color: #3366CC;">'&amp;cropStartX='</span> + coordinates.<span style="color: #006600;">left</span> +<span style="color: #3366CC;">'&amp;cropStartY='</span> + coordinates.<span style="color: #006600;">top</span> +<span style="color: #3366CC;">'&amp;cropWidth='</span> + coordinates.<span style="color: #006600;">width</span> +<span style="color: #3366CC;">'&amp;cropHeight='</span> + coordinates.<span style="color: #006600;">height</span>;
&nbsp;
<span style="color: #009900; font-style: italic;">// put download link in our page</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;">'downloadLink'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">innerHTML</span> = <span style="color: #3366CC;">'&lt;a href=&quot;'</span> + url + <span style="color: #3366CC;">'&quot;&gt;download cropped image&lt;/a&gt;'</span>;
&nbsp;</pre>
<p>This is all we need for the index page.</p>
<p><strong>upload.php</strong></p>
<pre class="php">&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>!<a href="http://www.php.net/empty"><span style="color: #000066;">empty</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$_FILES</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;uploadImage&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  	<span style="color: #808080; font-style: italic;">// get file name</span>
	<span style="color: #0000ff;">$filename</span> = <a href="http://www.php.net/basename"><span style="color: #000066;">basename</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$_FILES</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'uploadImage'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">// get extension</span>
  	<span style="color: #0000ff;">$ext</span> = <a href="http://www.php.net/substr"><span style="color: #000066;">substr</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$filename</span>, <a href="http://www.php.net/strrpos"><span style="color: #000066;">strrpos</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$filename</span>, <span style="color: #ff0000;">'.'</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
  	<span style="color: #808080; font-style: italic;">// check for jpg only</span>
  	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ext</span> == <span style="color: #ff0000;">&quot;jpg&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      		<span style="color: #808080; font-style: italic;">// generate unique file name</span>
  		<span style="color: #0000ff;">$newName</span> = <span style="color: #ff0000;">'images/'</span>.<a href="http://www.php.net/time"><span style="color: #000066;">time</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #ff0000;">'.'</span>.<span style="color: #0000ff;">$ext</span>;
&nbsp;
  		<span style="color: #808080; font-style: italic;">// upload files</span>
        	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/move_uploaded_file"><span style="color: #000066;">move_uploaded_file</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$_FILES</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'uploadImage'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'tmp_name'</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #0000ff;">$newName</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
        		<span style="color: #808080; font-style: italic;">// get height and width for image uploaded</span>
        		<a href="http://www.php.net/list"><span style="color: #000066;">list</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$width</span>, <span style="color: #0000ff;">$height</span><span style="color: #66cc66;">&#41;</span> = <a href="http://www.php.net/getimagesize"><span style="color: #000066;">getimagesize</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$newName</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        		<span style="color: #808080; font-style: italic;">// return json data</span>
           		<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'{&quot;image&quot; : &quot;'</span>.<span style="color: #0000ff;">$newName</span>.<span style="color: #ff0000;">'&quot;, &quot;height&quot; : &quot;'</span>.<span style="color: #0000ff;">$height</span>.<span style="color: #ff0000;">'&quot;, &quot;width&quot; : &quot;'</span>.<span style="color: #0000ff;">$width</span>.<span style="color: #ff0000;">'&quot; }'</span>;
        	<span style="color: #66cc66;">&#125;</span>
        	<span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
           		<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'{&quot;error&quot; : &quot;An error occurred while moving the files&quot;}'</span>;
        	<span style="color: #66cc66;">&#125;</span>
  	<span style="color: #66cc66;">&#125;</span>
  	<span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
     		<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'{&quot;error&quot; : &quot;Invalid image format&quot;}'</span>;
  	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>The upload.php file too is self explanatory, we check for a jpg image only, then generate an unique filename, put it in the images folder and finally build json data which we'll use for DOM manipulation. Of course the images folder must be writable by the web server.</p>
<p><strong>crop.php</strong></p>
<pre class="php">&nbsp;
<span style="color: #808080; font-style: italic;">// get variables</span>
<span style="color: #0000ff;">$imgfile</span> = <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'image'</span><span style="color: #66cc66;">&#93;</span>;
<span style="color: #0000ff;">$cropStartX</span> = <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'cropStartX'</span><span style="color: #66cc66;">&#93;</span>;
<span style="color: #0000ff;">$cropStartY</span> = <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'cropStartY'</span><span style="color: #66cc66;">&#93;</span>;
<span style="color: #0000ff;">$cropW</span> = <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'cropWidth'</span><span style="color: #66cc66;">&#93;</span>;
<span style="color: #0000ff;">$cropH</span> = <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'cropHeight'</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Create two images</span>
<span style="color: #0000ff;">$origimg</span> = imagecreatefromjpeg<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$imgfile</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$cropimg</span> = imagecreatetruecolor<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$cropW</span>,<span style="color: #0000ff;">$cropH</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Get the original size</span>
<a href="http://www.php.net/list"><span style="color: #000066;">list</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$width</span>, <span style="color: #0000ff;">$height</span><span style="color: #66cc66;">&#41;</span> = <a href="http://www.php.net/getimagesize"><span style="color: #000066;">getimagesize</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$imgfile</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Crop</span>
imagecopyresized<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$cropimg</span>, <span style="color: #0000ff;">$origimg</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #0000ff;">$cropStartX</span>, <span style="color: #0000ff;">$cropStartY</span>, <span style="color: #0000ff;">$width</span>, <span style="color: #0000ff;">$height</span>, <span style="color: #0000ff;">$width</span>, <span style="color: #0000ff;">$height</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// force download nes image</span>
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Content-type: image/jpeg&quot;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.php.net/header"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Content-Disposition: attachment; filename=&quot;'</span>.<span style="color: #0000ff;">$imgfile</span>.<span style="color: #ff0000;">'&quot;'</span><span style="color: #66cc66;">&#41;</span>;
imagejpeg<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$cropimg</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// destroy the images</span>
imagedestroy<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$cropimg</span><span style="color: #66cc66;">&#41;</span>;
imagedestroy<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$origimg</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Crop.php allows us to crop our uploaded image. First we get all the variables passed to us via the AJAX request,</p>
<pre class="php">&nbsp;
<span style="color: #808080; font-style: italic;">// get variables</span>
<span style="color: #0000ff;">$imgfile</span> = <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'image'</span><span style="color: #66cc66;">&#93;</span>;
<span style="color: #0000ff;">$cropStartX</span> = <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'cropStartX'</span><span style="color: #66cc66;">&#93;</span>;
<span style="color: #0000ff;">$cropStartY</span> = <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'cropStartY'</span><span style="color: #66cc66;">&#93;</span>;
<span style="color: #0000ff;">$cropW</span> = <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'cropWidth'</span><span style="color: #66cc66;">&#93;</span>;
<span style="color: #0000ff;">$cropH</span> = <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'cropHeight'</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;</pre>
<p>Then we create 2 images, the original one and the cropped one, and use the imagecopyresized function to generate the cropped image. We add some header information to tell the world it's an image and prompt for a save dialog.</p>
<p><strong><a href="http://htmlblog.net/demo/cropper/" target="_blank">Demo</a></strong><br />
<strong><a href="http://htmlblog.net/demo/cropper/cropper.zip">Download sample file</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://htmlblog.net/ajax-image-cropper-with-yui-and-php/feed/</wfw:commentRss>
		<slash:comments>39</slash:comments>
		</item>
		<item>
		<title>Show/hide containers by only adding CSS classes without writing javascript code (YUI-based)</title>
		<link>http://htmlblog.net/showhide-containers-by-only-adding-css-classes-without-writing-javascript-code-yui-based/</link>
		<comments>http://htmlblog.net/showhide-containers-by-only-adding-css-classes-without-writing-javascript-code-yui-based/#comments</comments>
		<pubDate>Sat, 20 Sep 2008 07:37:49 +0000</pubDate>
		<dc:creator>asvin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[collapse]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[div]]></category>
		<category><![CDATA[expand]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[selector]]></category>
		<category><![CDATA[yahoo]]></category>
		<category><![CDATA[yui]]></category>

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

This post will show you how to add the collapse/expand functionality in your HTML pages, perfect for FAQs by only adding two class names to your existing page and including the scripts below. Of course it's all YUI-based and also a demo of the fantastic YUI selector utility.
Demo of the toggler
Download the javascript file

What does [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://farm1.static.flickr.com/50/156713983_c264885895.jpg?v=0" alt="html code" /></p>
<div style="clear:both"></div>
<p>This post will show you how to add the collapse/expand functionality in your HTML pages, perfect for FAQs by only adding two class names to your existing page and including the scripts below. Of course it's all YUI-based and also a demo of the fantastic <a href="http://developer.yahoo.com/yui/selector/">YUI selector utility</a>.</p>
<p><a href="http://htmlblog.net/demo/toggler/" target="_blank"><strong>Demo of the toggler</strong></a><br />
<a href="http://htmlblog.net/demo/toggler/toggler.zip"><strong>Download the javascript file</strong></a><br />
<span id="more-34"></span><br />
<strong>What does the script do ?</strong><br />
OK, suppose you have a FAQ page where you have traditional anchor methods, i.e you click on the question and then you go to the bottom of the page to get the answer or you get redirected to another page. But now your users are complaining your site's not trendy enough and want to have the answer displayed immediately after clicking the question.</p>
<p><strong>The solution.</strong><br />
After downloading the javascript file, include it at the bottom of your page with the following HTML code:</p>
<pre class="html4strict">&nbsp;
<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/selector/selector-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>
<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/toggler.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>We need to include the <a href="http://developer.yahoo.com/yui/selector/">selector utility</a> which will be used to retrieve a collection of elements that match a given CSS selector. For e.g if we want to retrieve all div nodes in HTML page having the class "foo", we can write :</p>
<pre class="javascript">&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> myNodes = YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Selector</span>.<span style="color: #006600;">query</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div.foo'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>The selector is used in the toggler.js file.</p>
<p>After adding the scripts, now you have to add CSS classes to your containers. For example for the FAQ, for questions (or main, call it whatever you want), you add the class "htmlblog-main" and for the answers (or sub), add the "htmlblog-sub" class.</p>
<p>Before :</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&gt;</span></a></span>1. Question<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&gt;</span></a></span>Answer1<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&gt;</span></a></span>2. Question 2<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&gt;</span></a></span>Answer 2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>
&nbsp;</pre>
<p>After :</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;htmlblog-main&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>1. Question<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;htmlblog-sub&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Answer1<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;htmlblog-main&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>2. Question 2<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;htmlblog-sub&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Answer 2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>
&nbsp;</pre>
<p>If you already had a class for that container, no problem you can add the htmlblog-main or htmlblog-sub class :</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;myclass htmlblog-main&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>1. Question<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>
&nbsp;</pre>
<p><strong>What next ? </strong><br />
That's all ! All your answer containers will be hidden and questions will have a [+] besides them. Pretty easy</p>
<p><strong>Am I obliged to use the htmlblog-main and html-sub classes ? And I don't want to use div tags, I want li tags.</strong><br />
Don't worry, if you don't like the class names you can put whatever you want but you'll have to edit the toggler.js file at the following lines :</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>
	YAHOO.<span style="color: #006600;">htmlblog</span>.<span style="color: #006600;">init</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div'</span>, <span style="color: #3366CC;">'htmlblog-main'</span>, <span style="color: #3366CC;">'htmlblog-sub'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>For e.g, if you're using li tags instead of divs with the "myMainClass" as main and "mySubClass" as sub, the code goes :</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>
	YAHOO.<span style="color: #006600;">htmlblog</span>.<span style="color: #006600;">init</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'li'</span>, <span style="color: #3366CC;">'myMainClass'</span>, <span style="color: #3366CC;">'mySubClass'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p><strong>I'am getting an alert saying "Number of main and subs incorrect".</strong><br />
That means the number of main/subs are not the same. You need to have the number of the main class equals the number of the sub class.</p>
<p>I've commented the toggler.js file and you can easily change it to fit your needs.</p>
<p><a href="http://htmlblog.net/demo/toggler/" target="_blank"><strong>Demo of the toggler</strong></a><br />
<a href="http://htmlblog.net/demo/toggler/toggler.zip"><strong>Download the javascript file</strong></a></p>
]]></content:encoded>
			<wfw:commentRss>http://htmlblog.net/showhide-containers-by-only-adding-css-classes-without-writing-javascript-code-yui-based/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>nouzil.com &#8211; a new Mauritian portal on the web (YUI-based)</title>
		<link>http://htmlblog.net/nouzilcom-a-new-mauritian-portal-on-the-web-yui-based/</link>
		<comments>http://htmlblog.net/nouzilcom-a-new-mauritian-portal-on-the-web-yui-based/#comments</comments>
		<pubDate>Wed, 10 Sep 2008 15:25:47 +0000</pubDate>
		<dc:creator>asvin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[mauritius portal]]></category>
		<category><![CDATA[nouzil]]></category>
		<category><![CDATA[nouzil.com]]></category>
		<category><![CDATA[portal]]></category>
		<category><![CDATA[servihoo]]></category>
		<category><![CDATA[servihoo.com]]></category>
		<category><![CDATA[yahoo]]></category>
		<category><![CDATA[yui]]></category>

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

I came to know the existing of a new Mauritian portal via Avinash called nouzil (which means our island in creole) and I must say at first look it reminds me of Netvibes a lot. I think the nouzil.com team picked up some bits and pieces from Netvibes.com.
Anyway, looking behind the scenes I notice that [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://nouzil.com"><img src="http://htmlblog.net/wp-content/uploads/2008/09/nouzil-shot.png" alt="" title="nouzil-shot" width="500" height="271" class="aligncenter size-full wp-image-33" /></a></p>
<div style="clear:both"></div>
<p>I came to know the existing of a <a href="http://nouzil.com">new Mauritian portal</a> via <a href="http://www.noulakaz.net/weblog/2008/09/07/nouzil-a-new-mashup-for-mauritius/">Avinash</a> called <a href="http://nouzil.com">nouzil</a> (which means our island in creole) and I must say at first look it reminds me of <a href="http://www.netvibes.com">Netvibes</a> a lot. I think the <a href="http://nouzil.com">nouzil.com</a> team picked up some bits and pieces from <a href="http://www.netvibes.com">Netvibes.com</a>.<span id="more-32"></span><br />
Anyway, looking behind the scenes I notice that the new portal uses <a href="http://developer.yahoo.com/yui/">YUI </a>a lot, in fact it's all YUI-based. Had a look at the source and they are using the following <a href="http://developer.yahoo.com/yui/">YUI components</a> (with my comments) :</p>
<ul>
<li><a href="http://developer.yahoo.com/yui/">utilities</a> - for XHR requests, dragging of widgets, various DOM manipulations</li>
<li><a href="http://developer.yahoo.com/yui/cookie/">cookie</a> - to save the state of the widgets and order I presume</li>
<li><a href="http://developer.yahoo.com/yui/container/">container</a> - for various settings panels and the loading panel at the start</li>
<li><a href="http://developer.yahoo.com/yui/json/">json</a> - json data (hadn't seen any in my Firebug panel)</li>
<li><a href="http://developer.yahoo.com/yui/element/">element</a> - ?</li>
<li><a href="http://developer.yahoo.com/yui/calendar/">calendar</a> - calendar widget at the bottom</li>
<li><a href="http://developer.yahoo.com/yui/tabview/">tabview</a> - when doing a Google search the results are displayed in a new tab</li>
<li><a href="http://developer.yahoo.com/yui/grids/">reset-fonts-grids</a> - for layout, they are using the 974px width template</li>
</ul>
<p>Unfortunately the <a href="http://nouzil.com/js/nouzil-min.js">nouzil.js</a> was minified thus unreadable and couldn't get very far in my analysis ;-)</p>
<p>Overall I find the portal quite nice and find the CanalSat widget very useful since the <a href="http://www.canalsatellite-reunion.com/">CanalSat Reunion</a> website is very slow and the widget shows the current programs on air. It would be interesting if the user could choose the time frame he wishes to view the programme. The <a href="http://flickr.com">Flickr</a> widget too is cool, showing the latest photos from Mauritius and I think it's an exact replica from that of <a href="http://www.netvibes.com">Netvibes</a>, so is the <a href="http://delicious.com">delicious</a> one.</p>
<p>Another thing I noticed is that the team did not follow <a href="http://developer.yahoo.com/performance/rules.html">Yahoo's performance rules</a> for website, no caching, no gzipping and I have to download the same images again and again. I think they can improve it and take it very far, why not compete with the big boys.</p>
<p>Good luck to the <a href="http://nouzil.com">nouzil.com</a> team and congrats for that all <a href="http://developer.yahoo.com/yui/">YUI-based</a> portal.</p>
]]></content:encoded>
			<wfw:commentRss>http://htmlblog.net/nouzilcom-a-new-mauritian-portal-on-the-web-yui-based/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>YUI-based alert box &#8211; replace your ugly javascript alert box</title>
		<link>http://htmlblog.net/yui-based-alert-box-replace-your-ugly-javascript-alert-box/</link>
		<comments>http://htmlblog.net/yui-based-alert-box-replace-your-ugly-javascript-alert-box/#comments</comments>
		<pubDate>Sat, 30 Aug 2008 10:21:30 +0000</pubDate>
		<dc:creator>asvin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[alert]]></category>
		<category><![CDATA[message box]]></category>
		<category><![CDATA[replacement]]></category>
		<category><![CDATA[ugly]]></category>
		<category><![CDATA[yahoo]]></category>
		<category><![CDATA[yui]]></category>

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

This tutorial will explain how you can override the default alert box of your browser, without modifying your existing code and by adding 2 lines of javascript code.
Demo of the alert box
Download javascript file

Those 2 famous lines
After downloading the package and extracting it to your folder, add the following lines at the end of your [...]]]></description>
			<content:encoded><![CDATA[<p><a href='http://htmlblog.net/wp-content/uploads/2008/08/alert.png'><img src="http://htmlblog.net/wp-content/uploads/2008/08/alert.png" alt="" title="alert" width="318" height="122" class="alignleft size-full wp-image-31" /></a></p>
<div style="clear:both;"></div>
<p>This tutorial will explain how you can override the default alert box of your browser, without modifying your existing code and by adding 2 lines of javascript code.</p>
<p><strong><a href="http://htmlblog.net/demo/alert/" target="_blank">Demo of the alert box</a><br />
<a href="http://htmlblog.net/demo/alert/alert.zip">Download javascript file</a></strong><br />
<span id="more-30"></span><br />
<strong>Those 2 famous lines</strong><br />
After downloading the package and extracting it to your folder, add the following lines at the end of your page.</p>
<pre class="html4strict">&nbsp;
<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/yuiloader/yuiloader-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>
<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/alert.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>We make use of <a href="http://developer.yahoo.com/yui/">YUI's</a> fantastic<a href="http://developer.yahoo.com/yui/yuiloader/"> loader utility</a> which will allows us to load specific YUI components and their dependencies into our page via script from their servers. The second file actually contains all the code which will replace the default alert box by a YUI-based and <a href="http://developer.yahoo.com/yui/articles/skinning/">YUI-skinned one</a>.<br />
Note that it's better to include the files at the bottom of your page as told earlier as recommended by <a href="http://developer.yahoo.com/performance/rules.html">Yahoo's Exceptional Performance team for speeding your page</a>.</p>
<p><strong>The HTML code</strong></p>
<pre class="html4strict">&nbsp;
<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;#&quot;</span> <span style="color: #000066;">onclick</span>=<span style="color: #ff0000;">&quot;alert('Hello World !');&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Click to say hi<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span>
&nbsp;</pre>
<p>That's all, you don't need to include any CSS or other javascript files. They are all taken from Yahoo servers and you get a nice alert message. If for one reason or another you're fed up with this alert box, you just remove the included javascript files and it will fall back to the default alert box.</p>
<p><strong>Behind the scenes.</strong><br />
You can open the alert.js file which is well commented to have a look at what's going behind the scenes. Dav Glass <a href="http://blog.davglass.com/files/yui/widget_alert/">wrote something</a> about this replacement for alert, it's almost the same thing except all is done through the <a href="http://developer.yahoo.com/yui/yuiloader/">loader utility</a>. ;-)</p>
<p>The <a href="http://htmlblog.net/demo/alert/js/alert.js" target="_blank">alert.js</a> file contains 2 JSON objects namely :</p>
<ul>
<li>bootstrap</li>
<li>ui</li>
</ul>
<p><strong>bootstrap</strong><br />
bootstrap is responsible for fetching all the components from the Yahoo servers namely :</p>
<ul>
<li>container - since we are using the SimpleDialog component to render the alert box</li>
<li>button - to have the nice button in the box</li>
<li>fonts - which offers cross-browser typographical normalization and control</li>
<li>selector - to perform some lookups to add the yui-skin-sam class to the body so as we can use <a href="http://developer.yahoo.com/yui/articles/skinning/">YUI's skinning system</a>. </li>
</ul>
<p><strong>ui</strong><br />
The ui in fact initializes our SimpleDialog and then render it</p>
<pre class="javascript">&nbsp;
<span style="color: #009900; font-style: italic;">// our dialog for info, to show messages to the users</span>
ui.<span style="color: #006600;">dialogInfo</span> = <span style="color: #003366; font-weight: bold;">new</span> YAHOO.<span style="color: #006600;">widget</span>.<span style="color: #006600;">SimpleDialog</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;simpledialog1&quot;</span>,
<span style="color: #66cc66;">&#123;</span>
width: <span style="color: #3366CC;">&quot;300px&quot;</span>,
fixedcenter: <span style="color: #003366; font-weight: bold;">true</span>,
visible: <span style="color: #003366; font-weight: bold;">false</span>,
draggable: <span style="color: #003366; font-weight: bold;">false</span>,
zIndex: <span style="color: #CC0000;">9999</span>,
<span style="color: #000066;">close</span>: <span style="color: #003366; font-weight: bold;">true</span>,
modal: <span style="color: #003366; font-weight: bold;">true</span>,
effect:<span style="color: #66cc66;">&#123;</span>effect:YAHOO.<span style="color: #006600;">widget</span>.<span style="color: #006600;">ContainerEffect</span>.<span style="color: #006600;">FADE</span>,duration:<span style="color: #CC0000;">0.25</span><span style="color: #66cc66;">&#125;</span>,
constraintoviewport: <span style="color: #003366; font-weight: bold;">true</span>,
buttons: <span style="color: #66cc66;">&#91;</span> <span style="color: #66cc66;">&#123;</span> text:<span style="color: #3366CC;">&quot;close&quot;</span>, handler: <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;">this</span>.<span style="color: #006600;">hide</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #66cc66;">&#125;</span>, isDefault:<span style="color: #003366; font-weight: bold;">true</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
ui.<span style="color: #006600;">dialogInfo</span>.<span style="color: #006600;">setHeader</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;information&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #009900; font-style: italic;">// Render the Dialog</span>
ui.<span style="color: #006600;">dialogInfo</span>.<span style="color: #006600;">render</span><span style="color: #66cc66;">&#40;</span>document.<span style="color: #006600;">body</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>It also contains the showDialogInfo method which is called when alert function is called.</p>
<pre class="javascript">&nbsp;
window.<span style="color: #000066;">alert</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>text<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	ui.<span style="color: #006600;">dialogInfo</span>.<span style="color: #006600;">setBody</span><span style="color: #66cc66;">&#40;</span>text<span style="color: #66cc66;">&#41;</span>;
	ui.<span style="color: #006600;">dialogInfo</span>.<span style="color: #006600;">show</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>;
&nbsp;</pre>
<p><strong><a href="http://htmlblog.net/demo/alert/" target="_blank">Demo of the alert box</a><br />
<a href="http://htmlblog.net/demo/alert/alert.zip">Download javascript file</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://htmlblog.net/yui-based-alert-box-replace-your-ugly-javascript-alert-box/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
	</channel>
</rss>
