<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>chad's blog</title>
  <link rel="alternate" type="text/html" href="http://2tbsp.com/blogs/chad"/>
  <link rel="self" type="application/atom+xml" href="http://2tbsp.com/blog/1/atom/feed"/>
  <id>http://2tbsp.com/blog/1/atom/feed</id>
  <updated>2007-09-30T00:23:19-07:00</updated>
  <entry>
    <title>How to Remove a Workspace in Eclipse under OS X</title>
    <link rel="alternate" type="text/html" href="http://2tbsp.com/node/93" />
    <id>http://2tbsp.com/node/93</id>
    <published>2008-05-02T13:50:33-07:00</published>
    <updated>2008-05-03T10:28:03-07:00</updated>
    <author>
      <name>chad</name>
    </author>
    <category term="Eclipse" />
    <category term="OS X" />
    <summary type="html"><![CDATA[<p>The other morning, before I had my morning coffee, I fired up Eclipse and when prompted to select my Workspace, I entered my password and hit return. Still in a haze, I wondered why I was looking at the default Workspace. After restarting and seeing my password in the selected Workspace I finally woke up. D'oh!</p>
    ]]></summary>
    <content type="html"><![CDATA[<p>The other morning, before I had my morning coffee, I fired up Eclipse and when prompted to select my Workspace, I entered my password and hit return. Still in a haze, I wondered why I was looking at the default Workspace. After restarting and seeing my password in the selected Workspace I finally woke up. D'oh!</p>
<p>So where the heck is this Workspace list stored? I couldn't find the list under preferences. There wasn't a hidden settings file in my home directory. I then started poking around in /Applications/eclipse. Ah ha! Here's the setting file that contains recent workspaces.</p>
<p><div class="geshifilter"><pre class="geshifilter-html4strict"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">/Applications/eclipse/configuration/.settings/org.eclipse.ui.ide.prefs</div></li></ol></pre></div></p>
<p>Opened this file up and deleted the workspace from the RECENT_WORKSPACES. I had to make sure that I didn't remove to many line feeds (\n) which separate each workspace. I'm not sure why eclipse stores these preferences in the Applications folder. Why isn't this information stored in my home directory? </p>
<p>Oh well. I may never make this mistake again, but you never know. Hope this helps someone else out there.</p>
    ]]></content>
  </entry>
  <entry>
    <title>YUI Rollover Part 2: Improving JavaScript Performance and Scalability</title>
    <link rel="alternate" type="text/html" href="http://2tbsp.com/node/92" />
    <id>http://2tbsp.com/node/92</id>
    <published>2008-05-01T16:14:33-07:00</published>
    <updated>2008-05-01T16:27:40-07:00</updated>
    <author>
      <name>chad</name>
    </author>
    <category term="JavaScript" />
    <category term="YUI" />
    <summary type="html"><![CDATA[<p>In my <a href="/node/91">previous article</a> we traded in old school JavaScript habits for unobtrusive methods and the Yahoo! User Interface Library (YUI). The YUI's Event and DOM extensions greatly simplify basic DOM scripting tasks. Let's build on this foundation and improve the rollover script's performance and scalability.</p>
    ]]></summary>
    <content type="html"><![CDATA[<p>In my <a href="/node/91">previous article</a> we traded in old school JavaScript habits for unobtrusive methods and the Yahoo! User Interface Library (YUI). The YUI's Event and DOM extensions greatly simplify basic DOM scripting tasks. Let's build on this foundation and improve the rollover script's performance and scalability.<!--break--></p>
<p>Thanks to <a href="/node/91#comment-2553">Nick</a> and <a href="/node/91#comment-2558">Dirk</a> for their comments on event listeners impact on script performance. Nick points out the fact that JavaScript performance will degrade as more buttons are added to the <a href="http://2tbsp.com/system/files/yui-rollover.html">example rollover menu</a>. The use of a DOM scripting technique called event delegation can greatly reduce performance hits sustained through the assignment of event listeners. </p>
<h2>Improve Performance through Event Delegation and Bubbling</h2>
<p>The current version of the rollover script loops through the menu container and assigns event listeners to each button. Assigning listeners to child elements this way can, and will, have a negative impact on performance. </p>
<div style="float:right; width:223px; text-align:right;">
<img src="http://2tbsp.com/system/files/fig-bubble.png" alt="Illustration of the DOM bubbling effect" /></p>
<div class="caption">credit: <a href="http://www.senocular.com/flash/tutorials/buttoncapturing/">Senocular.com</a></div>
</div>
<p>Let's use event delegation to simplify things. Event delegation leverages an inherit  browser behavior called "event bubbling". By default, when an event happens to an element's descendents, the event "bubbles up" through the DOM to the parent. </p>
<p>This means that we can assign event listeners to the menu rather than every button it contains. In our rollover example we can reduce the number of event listeners from 3 to just 1. Wow! We've just cut the work required to assign listeners by 66%. You can see that the performance savings of event delegation add up quickly.</p>
<p>We'll use the same HTML markup and CSS from the previous example. </p>
<p><div class="geshifilter"><pre class="geshifilter-html4strict"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;menu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;btn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <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: #000000; font-weight: bold;">&gt;</span></span>Link 1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p&gt;</span></a></span>Description for link 1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;btn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <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: #000000; font-weight: bold;">&gt;</span></span>Link 2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p&gt;</span></a></span>Description for link 2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;btn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <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: #000000; font-weight: bold;">&gt;</span></span>Link 3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p&gt;</span></a></span>Description for link 3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li></ol></pre></div></p>
<p>Here's the updated script using the YUI's Event and DOM extensions. The script now uses Event's handy getTarget() method which captures the target of the current event.  </p>
<p><div class="geshifilter"><pre class="geshifilter-javascript"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&lt;script type=<span style="color: #3366CC;">&quot;text/javascript&quot;</span>&gt;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #009900; font-style: italic;">// Create the menu object</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #003366; font-weight: bold;">var</span> roll = <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900; font-style: italic;">// The rollover function</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  over : <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></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900; font-style: italic;">// Capture the current target element</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <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>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900; font-style: italic;">// Step through this section of the DOM looking for a btn </span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900; font-style: italic;">// in the target's ancestory, stop at the menu container</span></div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #66cc66;">&#40;</span>elTarget.<span style="color: #006600;">id</span> != <span style="color: #3366CC;">'menu'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">      <span style="color: #009900; font-style: italic;">// If we're over a btn, turn it on and stop</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">hasClass</span><span style="color: #66cc66;">&#40;</span>elTarget, <span style="color: #3366CC;">'btn'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> </div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">        YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span>elTarget, <span style="color: #3366CC;">'btn-over'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">        <span style="color: #000066; font-weight: bold;">break</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">      <span style="color: #009900; font-style: italic;">// Keep looking one level up</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">      <span style="color: #66cc66;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">        elTarget = elTarget.<span style="color: #006600;">parentNode</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">      <span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #66cc66;">&#125;</span>,</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900; font-style: italic;">// Reset menu styles</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  out : <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></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #003366; font-weight: bold;">var</span> btns = 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><span style="color: #3366CC;">'btn'</span>, <span style="color: #3366CC;">'div'</span>, <span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">removeClass</span><span style="color: #66cc66;">&#40;</span>btns, <span style="color: #3366CC;">'btn-over'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #009900; font-style: italic;">// Assign event listeners to just the menu</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">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;">'menu'</span>, <span style="color: #3366CC;">'mouseover'</span>, roll.<span style="color: #006600;">over</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">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;">'menu'</span>, <span style="color: #3366CC;">'mouseout'</span>, roll.<span style="color: #006600;">out</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&lt;/script&gt;</div></li></ol></pre></div></p>
<p>Take a look at the <a href="http://2tbsp.com/system/files/yui-rollover2.html">functioning example</a>.</p>
<h2>Allow for Growth</h2>
<p>The updated rollover script gives us a big improvement in performance, but it doesn't scale at all. The script includes hardcoded references to a single menu. What if we want to add additional menus?</p>
<p>First, let's update the stylesheet to use classes, instead of an ID, to target and style rollover menus.</p>
<p><div class="geshifilter"><pre class="geshifilter-css"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #6666ff;">.rollover</span> <span style="color: #66cc66;">&#123;</span> </div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">font</span>: <span style="color: #933;"><span style="color: #933;">85</span>%</span> Arial, Helvetica, san-<span style="color: #993333;">serif</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #6666ff;">.rollover</span> <span style="color: #6666ff;">.btn</span>, <span style="color: #6666ff;">.rollover</span> <span style="color: #6666ff;">.btn-over</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">background-color</span>: <span style="color: #cc00cc;">#e7e7e7</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">border-width</span>: <span style="color: #933;">2px</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">border-style</span>: <span style="color: #993333;">solid</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">color</span>: <span style="color: #cc00cc;">#<span style="color: #933;">666</span></span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">float</span>: <span style="color: #000000; font-weight: bold;">left</span>;</div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">margin</span>: <span style="color: #933;">0</span> <span style="color: #933;">5px</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">width</span>: <span style="color: #933;">150px</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #6666ff;">.rollover</span> <span style="color: #6666ff;">.btn</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">border-color</span>: <span style="color: #cc00cc;">#<span style="color: #933;">999</span></span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #6666ff;">.rollover</span> <span style="color: #6666ff;">.btn</span> a, <span style="color: #6666ff;">.rollover</span> <span style="color: #6666ff;">.btn-over</span> a <span style="color: #66cc66;">&#123;</span> </div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">color</span>: <span style="color: #cc00cc;">#fff</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">background-color</span>: <span style="color: #cc00cc;">#<span style="color: #933;">999</span></span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">display</span>: <span style="color: #993333;">block</span>;</div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">padding</span>: <span style="color: #6666ff;"><span style="color: #933;">.3em</span></span> <span style="color: #6666ff;"><span style="color: #933;">.5em</span></span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">text-decoration</span>: <span style="color: #993333;">none</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #6666ff;">.rollover</span> p <span style="color: #66cc66;">&#123;</span> </div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">clear</span>: <span style="color: #993333;">both</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">padding-top</span>: <span style="color: #933;">1em</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #6666ff;">.btn</span> p <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">padding</span>: <span style="color: #933;">0</span> <span style="color: #6666ff;"><span style="color: #933;">.5em</span></span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #808080; font-style: italic;">/* Rollover styles */</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #6666ff;">.rollover</span> <span style="color: #6666ff;">.btn-over</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">border-color</span>: <span style="color: #cc00cc;">#<span style="color: #933;">666</span></span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">color</span>: <span style="color: #cc00cc;">#<span style="color: #933;">000</span></span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #6666ff;">.rollover</span> <span style="color: #6666ff;">.btn-over</span> a <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">background-color</span>: <span style="color: #cc00cc;">#<span style="color: #933;">666</span></span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre></div></p>
<p>Next, apply the rollover class to a menu, or two.</p>
<p><div class="geshifilter"><pre class="geshifilter-html4strict"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;menu1&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;rollover&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;btn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <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: #000000; font-weight: bold;">&gt;</span></span>Link 1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p&gt;</span></a></span>Description for link 1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;btn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <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: #000000; font-weight: bold;">&gt;</span></span>Link 2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p&gt;</span></a></span>Description for link 2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;btn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <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: #000000; font-weight: bold;">&gt;</span></span>Link 3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p&gt;</span></a></span>Description for link 3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p&gt;</span></a></span>Here's a menu.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;menu2&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;rollover&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;btn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <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: #000000; font-weight: bold;">&gt;</span></span>Link 3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span></div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p&gt;</span></a></span>Description for link 3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;btn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <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: #000000; font-weight: bold;">&gt;</span></span>Link 4<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p&gt;</span></a></span>Description for link 4<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;btn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <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: #000000; font-weight: bold;">&gt;</span></span>Link 5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p&gt;</span></a></span>Description for link 5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p&gt;</span></a></span>Here's another menu.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li></ol></pre></div></p>
<p>The script now needs to include a method for adding event listeners to each of the rollover menus. This task is handled by menu.init() which is run once the page is loaded. The only other change is the use of the rollover class to stop our walk of the DOM.  </p>
<p><div class="geshifilter"><pre class="geshifilter-javascript"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&lt;script type=<span style="color: #3366CC;">&quot;text/javascript&quot;</span>&gt;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #009900; font-style: italic;">// Create the menu object</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #003366; font-weight: bold;">var</span> roll = <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900; font-style: italic;">// Assign event listeners to all rollover menus, </span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900; font-style: italic;">// Isn't getElementsByClassName wonderful?!</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  init : <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #003366; font-weight: bold;">var</span> menus = 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><span style="color: #3366CC;">'rollover'</span>, <span style="color: #3366CC;">'div'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    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>menus, <span style="color: #3366CC;">'mouseover'</span>, roll.<span style="color: #006600;">over</span><span style="color: #66cc66;">&#41;</span>;    </div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    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>menus, <span style="color: #3366CC;">'mouseout'</span>, roll.<span style="color: #006600;">out</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #66cc66;">&#125;</span>,</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900; font-style: italic;">// The rollover function, only change is using className instead of ID</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  over : <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></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900; font-style: italic;">// Capture the current target element</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <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>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900; font-style: italic;">// Step through this section of the DOM looking for a btn </span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900; font-style: italic;">// in the target's ancestory, stop at the menu container</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #66cc66;">&#40;</span>elTarget.<span style="color: #006600;">className</span> != <span style="color: #3366CC;">'rollover'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">      <span style="color: #009900; font-style: italic;">// If we're over a btn, turn it on and stop</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">hasClass</span><span style="color: #66cc66;">&#40;</span>elTarget, <span style="color: #3366CC;">'btn'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> </div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">        YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span>elTarget, <span style="color: #3366CC;">'btn-over'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">        <span style="color: #000066; font-weight: bold;">break</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">      <span style="color: #009900; font-style: italic;">// Keep looking one level up</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">      <span style="color: #66cc66;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">        elTarget = elTarget.<span style="color: #006600;">parentNode</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">      <span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #66cc66;">&#125;</span>,</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900; font-style: italic;">// Reset menu styles</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  out : <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></div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #003366; font-weight: bold;">var</span> btns = 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><span style="color: #3366CC;">'btn'</span>, <span style="color: #3366CC;">'div'</span>, <span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">removeClass</span><span style="color: #66cc66;">&#40;</span>btns, <span style="color: #3366CC;">'btn-over'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #009900; font-style: italic;">// Initialize all rollover menus when the page loads</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">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>, roll.<span style="color: #006600;">init</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&lt;/script&gt;</div></li></ol></pre></div></p>
<p>And here's the working example of the <a href="http://2tbsp.com/system/files/yui-rollover-multi.html">rollover script that scales</a>.</p>
<h2>Conclusion</h2>
<p>So there you have it. A few simple examples illustrating how to improve performance and scalability in JavaScript. These are basic examples and hopefully they'll help you refactor your scripts to meet your site's needs. I highly recommend reading Peter-Paul Koch's explanation of mouse events and event order in the DOM and how each browser handles them. They helped me tremendously.</p>
    ]]></content>
  </entry>
  <entry>
    <title>Introduction to Unobtrusive JavaScript, DOM Scripting, and the Yahoo! User Interface (YUI) Library</title>
    <link rel="alternate" type="text/html" href="http://2tbsp.com/node/91" />
    <id>http://2tbsp.com/node/91</id>
    <published>2008-04-24T22:07:15-07:00</published>
    <updated>2008-05-06T16:01:18-07:00</updated>
    <author>
      <name>chad</name>
    </author>
    <category term="HTML/CSS" />
    <category term="JavaScript" />
    <summary type="html"><![CDATA[<p>If you know JavaScript but have yet to make the leap to unobtrusive JavaScript, read on. I'll demonstrate how to upgrade a traditional DHTML rollover to an unobtrusive script and then to a script that leverages the Yahoo! User Interface Library's (YUI).</p>
    ]]></summary>
    <content type="html"><![CDATA[<p>If you know JavaScript but have yet to make the leap to unobtrusive JavaScript, read on. I'll demonstrate how to upgrade a traditional DHTML rollover to an unobtrusive script and then to a script that leverages the Yahoo! User Interface Library's (YUI).<!--break--></p>
<h2>What is Unobtrusive JavaScript and DOM Scripting?</h2>
<p>Rather than hardcoding event handlers as HTML attributes, unobtrusive JavaScript assigns them using the DOM. Well-structured, valid HTML is a prerequisite to DOM scripting. Documents should declare a DOCTYPE, they should validate against the DOCTYPE, and presentational styles should be applied with CSS (read "should" as "life will be easier for everyone if you do"). Use a few targeted CSS ID and class selectors to define page layout. Don't go overboard on defining selectors, remember, <a href="/content/taking_your_css_skills_next_level">descendent selectors are your friend</a>.</p>
<p>I'll use a simple text-based rollover for demonstration purposes. In a perfect world, with perfect browsers, this effect would be created using the CSS pseudo hover class but, as you've probably guessed, it doesn't work in Internet Explorer 6. Here's a screenshot of what we're building.</p>
<p><img src="http://2tbsp.com/system/files/rollover.png" /></p>
<h2>The Rollover, Obtrusively Old School Style</h2>
<p>First, the style sheet. Nothing wrong here as long as these are stored in an external CSS file.</p>
<p><div class="geshifilter"><pre class="geshifilter-css"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #cc00cc;">#menu</span> <span style="color: #66cc66;">&#123;</span> </div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">font</span>: <span style="color: #933;"><span style="color: #933;">85</span>%</span> Arial, Helvetica, san-<span style="color: #993333;">serif</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #cc00cc;">#menu</span> <span style="color: #6666ff;">.btn</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">background-color</span>: <span style="color: #cc00cc;">#e7e7e7</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">border-color</span>: <span style="color: #cc00cc;">#<span style="color: #933;">999</span></span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">border-style</span>: <span style="color: #993333;">solid</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">border-width</span>: <span style="color: #933;">2px</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">color</span>: <span style="color: #cc00cc;">#<span style="color: #933;">666</span></span>;</div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">float</span>: <span style="color: #000000; font-weight: bold;">left</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">margin</span>: <span style="color: #933;">0</span> <span style="color: #933;">5px</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">width</span>: <span style="color: #933;">150px</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #cc00cc;">#menu</span> <span style="color: #6666ff;">.btn</span> a <span style="color: #66cc66;">&#123;</span> </div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">color</span>: <span style="color: #cc00cc;">#fff</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">background-color</span>: <span style="color: #cc00cc;">#<span style="color: #933;">999</span></span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">display</span>: <span style="color: #993333;">block</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">padding</span>: <span style="color: #6666ff;"><span style="color: #933;">.3em</span></span> <span style="color: #6666ff;"><span style="color: #933;">.5em</span></span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">text-decoration</span>: <span style="color: #993333;">none</span>;</div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #cc00cc;">#menu</span> p <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">padding</span>: <span style="color: #933;">0</span> <span style="color: #6666ff;"><span style="color: #933;">.5em</span></span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre></div></p>
<p>Next the markup. The old school technique involves writing the rollover script functions and triggering them from event attributes. More complex actions required more and more stuff muddying up what was a perfectly readable HTML document. </p>
<p><div class="geshifilter"><pre class="geshifilter-html4strict"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;menu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;btn1&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;btn&quot;</span> <span style="color: #000066;">onmouseover</span>=<span style="color: #ff0000;">&quot;rollOver('btn1')&quot;</span> <span style="color: #000066;">onmouseout</span>=<span style="color: #ff0000;">&quot;rollOut('btn1')&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <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: #000000; font-weight: bold;">&gt;</span></span>Link 1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p&gt;</span></a></span>Description for link 1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;btn2&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;btn&quot;</span> <span style="color: #000066;">onmouseover</span>=<span style="color: #ff0000;">&quot;rollOver('btn2')&quot;</span> <span style="color: #000066;">onmouseout</span>=<span style="color: #ff0000;">&quot;rollOut('btn2')&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <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: #000000; font-weight: bold;">&gt;</span></span>Link 2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p&gt;</span></a></span>Description for link 2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;btn3&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;btn&quot;</span> <span style="color: #000066;">onmouseover</span>=<span style="color: #ff0000;">&quot;rollOver('btn3')&quot;</span> <span style="color: #000066;">onmouseout</span>=<span style="color: #ff0000;">&quot;rollOut('btn3')&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <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: #000000; font-weight: bold;">&gt;</span></span>Link 3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p&gt;</span></a></span>Description for link 3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li></ol></pre></div></p>
<p>Finally, the JavaScript. A function to set styles on mouseover and another to restore the original styles on mouseout. If you change a color in the stylesheet, you'll have to change it here too.</p>
<p><div class="geshifilter"><pre class="geshifilter-javascript"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&lt;script type=<span style="color: #3366CC;">&quot;text/javascript&quot;</span>&gt;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #009900; font-style: italic;">// Apply rollover styles</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #003366; font-weight: bold;">function</span> rollOver<span style="color: #66cc66;">&#40;</span>el<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900; font-style: italic;">// Get the elements</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #003366; font-weight: bold;">var</span> thisBtn = document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span>el<span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #003366; font-weight: bold;">var</span> thisLink = thisBtn.<span style="color: #006600;">getElementsByTagName</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900; font-style: italic;">// Change their styles</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  thisLink<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">style</span>.<span style="color: #006600;">backgroundColor</span> = <span style="color: #3366CC;">'#666'</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  thisBtn.<span style="color: #006600;">style</span>.<span style="color: #006600;">borderColor</span> = <span style="color: #3366CC;">'#666'</span>;</div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  thisBtn.<span style="color: #006600;">style</span>.<span style="color: #006600;">color</span> = <span style="color: #3366CC;">'#000'</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #009900; font-style: italic;">// Reset styles</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #003366; font-weight: bold;">function</span> rollOut<span style="color: #66cc66;">&#40;</span>el<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900; font-style: italic;">// Get the elements</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #003366; font-weight: bold;">var</span> thisBtn = document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span>el<span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #003366; font-weight: bold;">var</span> thisLink = thisBtn.<span style="color: #006600;">getElementsByTagName</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900; font-style: italic;">// Change their styles back again</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  thisLink<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">style</span>.<span style="color: #006600;">backgroundColor</span> = <span style="color: #3366CC;">'#999'</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  thisBtn.<span style="color: #006600;">style</span>.<span style="color: #006600;">borderColor</span> = <span style="color: #3366CC;">'#999'</span>;</div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  thisBtn.<span style="color: #006600;">style</span>.<span style="color: #006600;">color</span> = <span style="color: #3366CC;">'#666'</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&lt;script&gt;</div></li></ol></pre></div></p>
<h2>Make the Script Less Obtrusive</h2>
<p>Let's clean things up a bit. Strip those event handlers from the button containers. </p>
<p><div class="geshifilter"><pre class="geshifilter-html4strict"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;menu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;btn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <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: #000000; font-weight: bold;">&gt;</span></span>Link 1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p&gt;</span></a></span>Description for link 1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;btn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <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: #000000; font-weight: bold;">&gt;</span></span>Link 2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p&gt;</span></a></span>Description for link 2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;btn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <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: #000000; font-weight: bold;">&gt;</span></span>Link 3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p&gt;</span></a></span>Description for link 3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div></li></ol></pre></div></p>
<p>Now, make a few CSS adjustments and additions to define the hover styles where they belong, in the style sheet.</p>
<p><div class="geshifilter"><pre class="geshifilter-css"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #cc00cc;">#menu</span> <span style="color: #66cc66;">&#123;</span> </div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">font</span>: <span style="color: #933;"><span style="color: #933;">85</span>%</span> Arial, Helvetica, san-<span style="color: #993333;">serif</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #cc00cc;">#menu</span> <span style="color: #6666ff;">.btn</span>, <span style="color: #cc00cc;">#menu</span> <span style="color: #6666ff;">.btn-over</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">background-color</span>: <span style="color: #cc00cc;">#e7e7e7</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">border-width</span>: <span style="color: #933;">2px</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">border-style</span>: <span style="color: #993333;">solid</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">color</span>: <span style="color: #cc00cc;">#<span style="color: #933;">666</span></span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">float</span>: <span style="color: #000000; font-weight: bold;">left</span>;</div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">margin</span>: <span style="color: #933;">0</span> <span style="color: #933;">5px</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">width</span>: <span style="color: #933;">150px</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #cc00cc;">#menu</span> <span style="color: #6666ff;">.btn</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">border-color</span>: <span style="color: #cc00cc;">#<span style="color: #933;">999</span></span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #cc00cc;">#menu</span> <span style="color: #6666ff;">.btn</span> a, <span style="color: #cc00cc;">#menu</span> <span style="color: #6666ff;">.btn-over</span> a <span style="color: #66cc66;">&#123;</span> </div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">color</span>: <span style="color: #cc00cc;">#fff</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">background-color</span>: <span style="color: #cc00cc;">#<span style="color: #933;">999</span></span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">display</span>: <span style="color: #993333;">block</span>;</div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">padding</span>: <span style="color: #6666ff;"><span style="color: #933;">.3em</span></span> <span style="color: #6666ff;"><span style="color: #933;">.5em</span></span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">text-decoration</span>: <span style="color: #993333;">none</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #cc00cc;">#menu</span> p <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">padding</span>: <span style="color: #933;">0</span> <span style="color: #6666ff;"><span style="color: #933;">.5em</span></span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&nbsp;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #808080; font-style: italic;">/* Rollover styles */</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #cc00cc;">#menu</span> <span style="color: #6666ff;">.btn-over</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">border-color</span>: <span style="color: #cc00cc;">#<span style="color: #933;">666</span></span>;</div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">color</span>: <span style="color: #cc00cc;">#<span style="color: #933;">000</span></span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #cc00cc;">#menu</span> <span style="color: #6666ff;">.btn-over</span> a <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000000; font-weight: bold;">background-color</span>: <span style="color: #cc00cc;">#<span style="color: #933;">666</span></span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre></div></p>
<p>With that done, hook it all up with the script.</p>
<p><div class="geshifilter"><pre class="geshifilter-javascript"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&lt;script type=<span style="color: #3366CC;">&quot;text/javascript&quot;</span>&gt;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #009900; font-style: italic;">// Attach event listeners to the buttons</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #003366; font-weight: bold;">function</span> findButtons<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #003366; font-weight: bold;">var</span> menu, btns, i;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900; font-style: italic;">// Get the menu element to access the btn divs</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  menu = document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'menu'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  btns = menu.<span style="color: #006600;">getElementsByTagName</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #66cc66;">&#40;</span>i=<span style="color: #CC0000;">0</span>; i&lt;btns.<span style="color: #006600;">length</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900; font-style: italic;">// Attach event listeners for rollovers to the btns</span></div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066FF;">/btn/</span>.<span style="color: #006600;">test</span><span style="color: #66cc66;">&#40;</span>btns<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">className</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> </div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">      btns<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">onmouseover</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>roll<span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #66cc66;">&#125;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">      btns<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">onmouseout</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>roll<span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #66cc66;">&#125;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #009900; font-style: italic;">// Attached to the buttons and ready to roll</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #003366; font-weight: bold;">function</span> roll<span style="color: #66cc66;">&#40;</span>o<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900; font-style: italic;">// btn is our off state, so turn it on</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>o.<span style="color: #006600;">className</span> == <span style="color: #3366CC;">'btn'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    o.<span style="color: #006600;">className</span> = <span style="color: #3366CC;">'btn-over'</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900; font-style: italic;">// Otherwise, turn it off</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #66cc66;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    o.<span style="color: #006600;">className</span> = <span style="color: #3366CC;">'btn'</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #009900; font-style: italic;">// Initialize the rollover</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">window.<span style="color: #000066;">onload</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  findButtons<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&lt;/script&gt;</div></li></ol></pre></div></p>
<p>Everything is now in its proper place. No more JavaScript in the HTML and styles aren't defined in the JavaScript. This will be much easier to maintain.</p>
<h2>Unobtrusive, the YUI Style</h2>
<p>YUI is one of the many JavaScript libraries that have gained attention in recent years. Some say it's too big and bulky and that may be true in certain cases, like creating a simple rollover. But it's rare in our Web 2.0 world that a site's only JavaScript-powered feature is a rollover. YUI is my current JavaScript framework of choice because along with being feature-rich, it's well documented, tested, and supported.</p>
<p>So here's the rollover script powered by the YUI's Yahoo! Global Object, and DOM and Event extensions. </p>
<p><div class="geshifilter"><pre class="geshifilter-javascript"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&lt;script type=<span style="color: #3366CC;">&quot;text/javascript&quot;</span> src=<span style="color: #3366CC;">&quot;http://yui.yahooapis.com/2.5.1/build/yahoo-dom-event/yahoo-dom-event.js&quot;</span>&gt;&lt;/script&gt;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&lt;script type=<span style="color: #3366CC;">&quot;text/javascript&quot;</span>&gt;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #009900; font-style: italic;">// Create a menu object, put everything we need into it</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #003366; font-weight: bold;">var</span> menu = <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900; font-style: italic;">// Initialize the menu rollover</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  init : <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900; font-style: italic;">// Get the btn elements</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    btns = 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><span style="color: #3366CC;">'btn'</span>, <span style="color: #3366CC;">'div'</span>, <span style="color: #3366CC;">'menu'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #009900; font-style: italic;">// Assign event listeners to the btns</span></div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <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;btns.<span style="color: #006600;">length</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">      YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">addListener</span><span style="color: #66cc66;">&#40;</span>btns<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>, <span style="color: #3366CC;">'mouseover'</span>, menu.<span style="color: #006600;">roll</span>, i<span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">      YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Event</span>.<span style="color: #006600;">addListener</span><span style="color: #66cc66;">&#40;</span>btns<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>, <span style="color: #3366CC;">'mouseout'</span>, menu.<span style="color: #006600;">roll</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    <span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #66cc66;">&#125;</span>,</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #009900; font-style: italic;">// First, turn 'em all off, then turn one on</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  roll : <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>e, i<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">removeClass</span><span style="color: #66cc66;">&#40;</span>btns, <span style="color: #3366CC;">'btn-over'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    YAHOO.<span style="color: #006600;">util</span>.<span style="color: #006600;">Dom</span>.<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span>btns<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>, <span style="color: #3366CC;">'btn-over'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">  <span style="color: #66cc66;">&#125;</span></div></li><li style="font-family: monospace; font-weight: bold; font-style: italic;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #66cc66;">&#125;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #009900; font-style: italic;">// Initialize the menu</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">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>, menu.<span style="color: #006600;">init</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">&lt;/script&gt;</div></li></ol></pre></div></p>
<p>The YUI version basically works just like the unobtrusive version. Yahoo! provides hosted versions of all the YUI extensions, just add a link to yahoo-dom-event.js. With the help of the Yahoo! Global Object and DOM and Event extensions, it's easy to grab  button elements and assign event handlers that trigger the roll function. The main difference here is that everything is neatly encapsulated into the menu object. The YUI's <a href="http://developer.yahoo.com/yui/dom/#class">getElementsByClassName and remove/addClass</a> methods make DOM scripting a breeze. </p>
<p>Take a look at the <a href="http://2tbsp.com/system/files/yui-rollover.html">final product</a>.</p>
<h2>Conclusion</h2>
<p>Hopefully these basic examples serve as a kick in the rear for those clinging to those old school ways. Now that you have the basics of event handling down, <a href="/node/92">I'll show you how to improve the scripts performance through event delegation and how to make it scalable to power multiple rollover menus</a>.</p>
    ]]></content>
  </entry>
  <entry>
    <title>Subversion Administration: Separate an SVN Repository into Individual Projects</title>
    <link rel="alternate" type="text/html" href="http://2tbsp.com/node/88" />
    <id>http://2tbsp.com/node/88</id>
    <published>2008-03-20T21:02:15-07:00</published>
    <updated>2008-03-22T08:50:26-07:00</updated>
    <author>
      <name>chad</name>
    </author>
    <category term="How to" />
    <category term="Subversion" />
    <summary type="html"><![CDATA[<p>
I&#39;ve been using Subversion since the <a href="http://gallery.menalto.com/" title="The Gallery Project">Gallery Project</a>  switched to it from CVS a few years back. I decided to install Subversion to manage my freelance projects last year and am getting up to speed on repository administration. I flip-flopped for a while on whether to keep projects in a single or separate repositories. I initially decided to keep everything in one repository, but later decided to separate them out. Fortunately, the svnadmin and svndumpfilter utilities make it relatively simple to separate repositories into individual projects.
</p>
    ]]></summary>
    <content type="html"><![CDATA[<p>
I&#39;ve been using Subversion since the <a href="http://gallery.menalto.com/" title="The Gallery Project">Gallery Project</a>  switched to it from CVS a few years back. I decided to install Subversion to manage my freelance projects last year and am getting up to speed on repository administration. I flip-flopped for a while on whether to keep projects in a single or separate repositories. I initially decided to keep everything in one repository, but later decided to separate them out. Fortunately, the svnadmin and svndumpfilter utilities make it relatively simple to separate repositories into individual projects.
</p>
<p><!--break--></p>
<p>
My goal is to create two new projects from folders stored in the trunk of a single repository. My repository&#39;s history isn&#39;t complex—it doesn&#39;t contain tags or branches yet. You&#39;ll need to put a bit more thought into how to deal with existing tags and branches.
</p>
<p>
My current svn root layout is:
</p>
<p><div class="geshifilter"><pre class="geshifilter-bash"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">/svnroot</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    /branches</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    /tags</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    /trunk</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">        /project1</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">        /project2</div></li></ol></pre></div></p>
<p>
The desired layout is:
</p>
<p><div class="geshifilter"><pre class="geshifilter-bash"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">/svnroot</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    /project1</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">        /branches</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">        /tags</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">        /trunk</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    /project2</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">        /branches</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">        /tags</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">        /trunk</div></li></ol></pre></div></p>
<p>
The basic steps to separate these repositories are:
</p>
<ol>
<li>Dump the current repository</li>
<li>Separate folders in the dump to separate files with svndumpfilter</li>
<li>Create new repositories</li>
<li>Load the filtered dump files into the new repositories</li>
<li>Cleanup the new repository layout, move files from trunk/projectN to trunk/</li>
<li>Delete the original branches, tags, and trunk repositories from svnroot</li>
</ol>
<p>
I&#39;ll use tmp as working directory to dump and filter files.
</p>
<h2>Dump the current repository</h2>
<p><div class="geshifilter"><pre class="geshifilter-bash"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">svnadmin dump /svnroot/originalproject &amp;gt; /tmp/original.dump</div></li></ol></pre></div></p>
<h2>Separate folders to separate files with svndumpfilter</h2>
<p><div class="geshifilter"><pre class="geshifilter-bash"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #c20cb9; font-weight: bold;">cat</span> /tmp/original.dump | svndumpfilter --drop-empty-revs --renumber-revs include trunk/project1 &amp;gt; /tmp/project1.dump</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #c20cb9; font-weight: bold;">cat</span> /tmp/original.dump | svndumpfilter --drop-empty-revs --renumber-revs include trunk/project2 &amp;gt; /tmp/project2.dump</div></li></ol></pre></div></p>
<p>
The key here is the include option which targets the project folder(s). There&#39;s also an exclude option that might come in handy if you&#39;re trying to remove one folder from a group of many. The --drop-empty-revs and --renumber-revs options remove commit log messages for other folders and renumber the remaining revision numbers.
</p>
<h2>Create new repositories</h2>
<p><div class="geshifilter"><pre class="geshifilter-bash"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">svnadmin create /svnroot/project1</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">svnadmin create /svnroot/project2</div></li></ol></pre></div></p>
<h2>Load the filtered dump files into the new repositories</h2>
<p><div class="geshifilter"><pre class="geshifilter-bash"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">svnadmin load /svnroot/project1 &amp;lt; /tmp/project1.dump </div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">svnadmin load /svnroot/project2 &amp;lt; /tmp/project2.dump </div></li></ol></pre></div></p>
<h2>Cleanup new repository layout, move files from trunk/projectN to trunk/</h2>
<p>
Each repository layout now looks something like:
</p>
<p><div class="geshifilter"><pre class="geshifilter-bash"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">/svnroot</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    /project1</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">        /branches</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">        /tags</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">        /trunk</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">            /project1</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">                file1</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">                ...</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">                fileX</div></li></ol></pre></div></p>
<p>
But they should look like:
</p>
<p><div class="geshifilter"><pre class="geshifilter-bash"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">/svnroot</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">    /project1</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">        /branches</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">        /tags</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">        /trunk</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">            file1</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">            ...</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">            fileX</div></li></ol></pre></div></p>
<p>
Subversion doesn&#39;t allow wildcard gobling, so it&#39;s impossible to do something like:
</p>
<p><div class="geshifilter"><pre class="geshifilter-bash"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">svn move trunk/project1/* trunk/  </div></li></ol></pre></div></p>
<p>
It was possible to edit path information in the project dump files before loading them, but it seemed easier to check out a working copy of each, move things around, and then commit the changes.
</p>
<p><div class="geshifilter"><pre class="geshifilter-bash"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">svn checkout <span style="color: #c20cb9; font-weight: bold;">file</span> :///svnroot/project1 /tmp/project1</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #7a0874; font-weight: bold;">cd</span> /tmp/project1/trunk</div></li></ol></pre></div></p>
<p>
Now, a little shell work to move things around. <a href="http://subversion.tigris.org/faq.html#sorry-no-globbing">Thanks Tigris!</a> PLEASE NOTE: Use this at your own risk. Back up your repository before trying the following or be ready to use svn revert.
</p>
<p><div class="geshifilter"><pre class="geshifilter-bash"><ol><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal"><span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> /tmp/project1/trunk/project1/*; <span style="color: #000000; font-weight: bold;">do</span> svn <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #007800;">$i</span> /tmp/project1/trunk; <span style="color: #000000; font-weight: bold;">done</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">svn commit -m <span style="color: #ff0000;">&quot;Moved all project files to trunk&quot;</span></div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">svn remove /tmp/project1/trunk/project1</div></li><li style="font-family: monospace; font-weight: normal;"><div style="font-family: monospace; font-weight: normal; font-style: normal">svn commit -m <span style="color: #ff0000;">&quot;Removed obsolete project1 folder after moving its contents to trunk&quot;</span></div></li></ol></pre></div></p>
<p>
Lather, rinse, repeat.
</p>
<p>
At some point I&#39;ll probably back track and write a post on installing and configuring Subversion with MacPorts but that&#39;s it, at least for now.
</p>
    ]]></content>
  </entry>
  <entry>
    <title>Notes on Choosing a PHP Framework: A Comparison of CakePHP and the Zend Framework</title>
    <link rel="alternate" type="text/html" href="http://2tbsp.com/node/87" />
    <id>http://2tbsp.com/node/87</id>
    <published>2008-02-15T15:05:17-08:00</published>
    <updated>2008-03-20T20:19:19-07:00</updated>
    <author>
      <name>chad</name>
    </author>
    <category term="Frameworks" />
    <category term="PHP" />
    <category term="Review" />
    <summary type="html"><![CDATA[<p>
UPDATE:Parlez vous Français? Guillaume Rossolini has created a <a href="http://g-rossolini.developpez.com/comparatifs/php/cake-zf/">French translation of this article</a>.
</p>
<p>
Are you considering using a PHP framework for your next project? If so, this article&#39;s for you. I&#39;ve narrowed down my choices to CakePHP and the Zend Framework and have documented my findings  here.
</p>
    ]]></summary>
    <content type="html"><![CDATA[<p>
UPDATE:Parlez vous Français? Guillaume Rossolini has created a <a href="http://g-rossolini.developpez.com/comparatifs/php/cake-zf/">French translation of this article</a>.
</p>
<p>
Are you considering using a PHP framework for your next project? If so, this article&#39;s for you. I&#39;ve narrowed down my choices to CakePHP and the Zend Framework and have documented my findings  here.
</p>
<p>
Four or five years ago I began researching PHP application frameworks. At the time, the pickings were very slim, especially when compared to those available to Java developers. On the advice of an ex-Coldfusion developer, I settled on the PHP port of Fusebox, the de facto Coldfusion framework. Fusebox 3 provided a convenient set of loose conventions for structuring applications, organizing code, naming files, and controlling flow. It served my development team well and <a href="http://www.lanl.gov/news/index.php/fuseaction/nr.subject">applications we built with it</a> are still up and running.
</p>
<p>
I took a two-year &quot;break&quot; from development and upon my return I found more PHP frameworks than you can shake a stick at. Now I need to find a solid MVC-based framework and after reading dozens of articles and completing a few tutorials, I&#39;ve narrowed my focus to CakePHP and the Zend Framework.
</p>
<p>
Before going further, let it be known that my point of view is that of someone with plenty of application development experience but without a formal IT or CS degree. I can&#39;t go on  about the merits of one design pattern vs. another. I do, however, understand the need for convention, the advantages of modularity, and the efficiency of easily repeatable processes.
</p>
<p>
Please note that symfony, Prado, Code Ignitor, and many of the other PHP frameworks all look quite capable of meeting most project needs. If you&#39;re a fanboy of another framework, please don&#39;t espouse it&#39;s merits here. I am, however, interested in hearing about your general experience in learning a framework, particularly  in the context of your professional background.
</p>
<p>
Here&#39;s a comparison and a few conclusions after spending a bit of time with both CakePHP and the Zend Framework. Please note that my quality scale range is: poor, fair, good, excellent.
</p>
<table border="1" cellspacing="0" cellpadding="5">
<caption>
	Feature Comparison of CakePHP and the Zend Framework<br />
	</caption>
<tbody>
<tr>
<th scope="col">
<div align="left">
			Feature
			</div>
</th>
<th scope="col">CakePHP</th>
<th scope="col">Zend Framework</th>
<th scope="col">Notes</th>
</tr>
<tr>
<th scope="row">
<div align="left">
			License
			</div>
</th>
<td>
<div align="center">
			<a href="http://en.wikipedia.org/wiki/MIT_