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

<channel>
	<title>Mozilla Hacks - the Web developer blog &#187; DOM</title>
	<atom:link href="http://hacks.mozilla.org/category/dom/feed/" rel="self" type="application/rss+xml" />
	<link>http://hacks.mozilla.org</link>
	<description>hacks.mozilla.org</description>
	<lastBuildDate>Wed, 08 Feb 2012 22:52:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Introducing Aurora 11 with tons of new features and improvements</title>
		<link>http://hacks.mozilla.org/2011/12/introducing-aurora-11-with-tons-of-new-features-and-improvements/</link>
		<comments>http://hacks.mozilla.org/2011/12/introducing-aurora-11-with-tons-of-new-features-and-improvements/#comments</comments>
		<pubDate>Fri, 23 Dec 2011 17:58:26 +0000</pubDate>
		<dc:creator>Robert Nyman</dc:creator>
				<category><![CDATA[Audio]]></category>
		<category><![CDATA[Aurora]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Featured Article]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=10611</guid>
		<description><![CDATA[We have now released Aurora 11, soon to become Firefox 11, and wanted to cover all the the things we have improved in this version! Highlights To quickly edit the outerHTML of an element, we have now added support for element.outerHTML. The Battery API Storing files in IndexedDB Support for loop attributes for media elements [...]]]></description>
			<content:encoded><![CDATA[<p>We have now released <a href="http://www.mozilla.org/en-US/firefox/channel/">Aurora 11</a>, soon to become Firefox 11, and wanted to cover all the the things we have improved in this version!</p>
<p><span id="more-10611"></span></p>
<h2>Highlights</h2>
<ul>
<li>To quickly edit the outerHTML of an element, we have now added support for element.outerHTML.</li>
<li>The Battery API</li>
<li>Storing files in IndexedDB</li>
<li>Support for loop attributes for media elements</li>
<li>SPDY (off by preferences)</li>
<li>WebSockets no longer needs a prefix when you call them</li>
</ul>
<h2>All new features</h2>
<p>Below is a list of all the new improvements in Aurora 11, grouped by category together with a link to each bug in bugzilla if you are interested in reading up more about it and its process.</p>
<h3>DOM</h3>
<ul>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=92264">Support element.outerHTML property</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=280391">SVGSVGElement.getElementById not implemented</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=692642">IndexedDB: support indexedDB.cmp</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=692630">IndexedDB: multientry indexes</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=692614">IndexedDB now support all key types, including floating point numbers, dates and arrays!</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=678694">Battery API</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=651072">Support HTML parsing in XMLHttpRequest per XMLHttpRequest Level 2</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=704171">Remove the no-argument form of requestAnimationFrame</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=704175">requestAnimationFrame callback function name should be &#8220;sample&#8221;, not &#8220;onBeforePaint&#8221;</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=647518">Allow mozRequestAnimationFrame requests to be cancelable</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=708173">Use cancelRequestAnimationFrame where appropriate instead of boolean flags</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=710981">Add mozCancelAnimationFrame</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=675884">Implement Event constructors</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=708701">Implement HTML event ctors</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=709127">Implement MouseEvent and UIEvent ctors</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=661877">Enable storing files in IndexedDB</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=694138">IndexedDB: Allow passing an array as keypath</a></li>
</ul>
<h3>GFX</h3>
<ul>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=631479">Add Graphite2 to gecko</a></li>
</ul>
<h3>Layout</h3>
<ul>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=627842">-moz-text-size-adjust CSS property</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=511909">[css3-conditional] allow @-rules inside of @media and @-moz-document</a></li>
</ul>
<h3>Media</h3>
<ul>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=449157">Implement the looping attributes in media elements</a></li>
</ul>
<h3>Network</h3>
<ul>
<li><a href="https://wiki.mozilla.org/Platform/Features/SPDY">SPDY (Preffed off)</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=573043">Enable Extended Protection (channel and service binding) for NTLM authentication</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=704989">add workaround for broken Outlook Web App (OWA) attachment handling</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=700589">HTTP content type charset parameter accepts single quotes</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=703380">XMLHttpRequest can fire an abort event after a load event, but should not</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=701787">Disallow responseType and withCredentials for sync XHR</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=664179">Allow Cross-Origin URLs in EventSource (Server-Sent Events)</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=707142">Support unprefixed responseType == &#8220;json&#8221; in XMLHttpRequest</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=676439">Implement Binary Messages for Websockets</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=664284">Add HSTS support for websockets</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=710964">No longer dispatch incoming WebSocket messages in CLOSING state</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=711205">Set WebSocket message size limit to 2 GB</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=695635">Tracking bug: unprefix WebSockets</a></li>
</ul>
<h3>Performance</h3>
<ul>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=666446">Lots of animated gifs swamp us with paint events</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/12/introducing-aurora-11-with-tons-of-new-features-and-improvements/feed/</wfw:commentRss>
		<slash:comments>42</slash:comments>
		</item>
		<item>
		<title>Announcing Firefox Aurora 10</title>
		<link>http://hacks.mozilla.org/2011/11/announcing-firefox-aurora-10/</link>
		<comments>http://hacks.mozilla.org/2011/11/announcing-firefox-aurora-10/#comments</comments>
		<pubDate>Fri, 11 Nov 2011 22:34:00 +0000</pubDate>
		<dc:creator>Joe Stagner</dc:creator>
				<category><![CDATA[Aurora]]></category>
		<category><![CDATA[Developer Tools]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Feature]]></category>
		<category><![CDATA[Featured Article]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[IndexedDB]]></category>
		<category><![CDATA[WebGL]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=10176</guid>
		<description><![CDATA[We&#8217;re happy to announce the availability of Aurora 10. (Download and Test Aurora 10) In additional to the normal improvements that you&#8217;ve come to expect like performance, security and bug fixes, Aurora 10 focuses in HTML5 enhancements. New additions HTML5 Visibility API createProcessingInstruction WebGL antialiasing 3D Transforms Visibility API Document.mozFullScreenEnabled\ Developer Tools Code Editor CSS [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re happy to announce the availability of Aurora 10. <br/>(<a href="http://www.mozilla.org/en-US/firefox/channel/">Download and Test Aurora 10</a>)</p>
<p>In additional to the normal improvements that you&#8217;ve come to expect like performance, security and bug fixes, Aurora 10 focuses in HTML5 enhancements. </p>
<h2>New additions</h2>
<ul>
<li><a href="https://developer.mozilla.org/en/DOM/Using_the_Page_Visibility_API">HTML5 Visibility API </a></li>
<li><a href="https://developer.mozilla.org/en/DOM/document.createProcessingInstruction">createProcessingInstruction</a></li>
<li>WebGL antialiasing</li>
<li>3D Transforms</li>
<li>Visibility API</li>
<li>Document.mozFullScreenEnabled\</li>
</ul>
<h2>Developer Tools</h2>
<ul>
<li><a title="DevTools/Features/CodeEditor" href="https://wiki.mozilla.org/DevTools/Features/CodeEditor">Code Editor</a></li>
<li><a title="DevTools/Features/RulesView" href="https://wiki.mozilla.org/DevTools/Features/RulesView">CSS Rules View</a></li>
<li><a title="DevTools/Features/Highlighter" href="https://wiki.mozilla.org/DevTools/Features/Highlighter">Page Inspector/Highlighter</a></li>
<li><a title="DevTools/Features/ConsoleObjectCompletion" href="https://wiki.mozilla.org/DevTools/Features/ConsoleObjectCompletion">Console Object Completion</a></li>
<li><a title="DevTools/Features/StyleInspector" href="https://wiki.mozilla.org/DevTools/Features/StyleInspector">Style Inspector</a></li>
</ul>
<p>Aurora 10 also implements incremental enhancements like IndexedDB setVersion API changes. Ongoing detailed attention to evolving specifications help to keep Firefox at the front of the Web revolution. (Read more about <a href="https://developer.mozilla.org/en/IndexedDB/IndexedDB_primer">IndexedDB on MDN</a>.)</p>
<h2>DOM</h2>
<ul>
<li>We now fire a &#8220;load&#8221; event on stylesheet linking when the sheet load finishes or &#8220;error&#8221; if the load fails.</li>
<li>We turn the POSTDATA prompt into an information page (when navigating in session history).</li>
<li>We only forward event attributes on body/frameset to the window if we also forward the corresponding on* property.</li>
<li>We no longer allow more than one call to window.open() when we allow popups.</li>
<li>We fixed a bug where a success callback never fired when a position update is triggered after getCurrentPosition().</li>
<li>We removed replaceWholeText().</li>
<li>We fixed an error with createPattern(zero-size canvas).</li>
<li>We now handle putImageData(nonfinite) correctly.</li>
<li>We now throw INVALID_STATE_ERR when dispatching uninitialized events.</li>
<li>We&#8217;ve made Document.documentURI readonly.</li>
<li>We fixed document.importNode to comply with optional argument omitted.</li>
</ul>
<h2>Web workers</h2>
<ul>
<li>We now allow data URLs.</li>
<li>We implemented event.stopImmediatePropagation in workers.</li>
<li>We made XHR2 response/responseType work in Web Workers.</li>
</ul>
<h2>Graphics</h2>
<ul>
<li>We implement the WebGL OES_standard_derivatives extension.</li>
<li>We implement minimal-capabilities WebGL mode.</li>
</ul>
<h2>JavaScript</h2>
<ul>
<li>The function caller property no longer skips over eval frames.</li>
<li>We fixed E4X syntax so that it is not accepted in ES5 strict mode.</li>
<li>weakmap.set no longer returns itself instead of undefined.</li>
<li>We implemented the battery API.</li>
</ul>
<h2>Offline: IndexedDB enhancements</h2>
<ul>
<li>IndexedDB setVersion API changes</li>
<li>Added support for IDBObjectStore/IDBIndex.count</li>
<li>Various methods accept both keys and KeyRanges.</li>
<li>Added support for IDBCursor.advance.</li>
<li>Implemented deleteDatabase.</li>
<li> objectStoreNames are no longer updated on closed databases when another connection adds or removes object stores</li>
<li>IDBObjectStore.delete and IDBCursor.delete now return undefined.</li>
<li>No longer throws an error if there are unknown properties in the options objects to createObjectStore/createIndex.</li>
<li>We now the errorCode to &#8220;ABORT_ERR&#8221; for all pending requests when IDBTransaction.abort() is called.</li>
<li>Fixed the sort order for indexes.</li>
</ul>
<h2>Layout</h2>
<ul>
<li>We have updated the current rule for handling malformed media queries.</li>
<li>We now support the HTML5 &lt;bdi&gt; element and CSS property unicode-bidi: isolate.</li>
<li>The CSS3 implementation now supports unicode-bidi: plaintext.</li>
</ul>
<h2>Media</h2>
<ul>
<li>Implemented Document.mozFullScreenEnabled.</li>
<li>Enabled the DOM full-screen API on desktop Firefox by default.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/11/announcing-firefox-aurora-10/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Introducing Aurora 9</title>
		<link>http://hacks.mozilla.org/2011/09/introducing-aurora-9/</link>
		<comments>http://hacks.mozilla.org/2011/09/introducing-aurora-9/#comments</comments>
		<pubDate>Fri, 30 Sep 2011 22:22:45 +0000</pubDate>
		<dc:creator>Robert Nyman</dc:creator>
				<category><![CDATA[Aurora]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Feature]]></category>
		<category><![CDATA[Featured Article]]></category>
		<category><![CDATA[Firefox 9]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[XMLHttpRequest]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=9688</guid>
		<description><![CDATA[We have just released Aurora 9 (download and test Aurora 9), which is planned to be the upcoming Firefox 9. In it, we have a number of new things that we hope will get you excited! JavaScript Type Inference We&#8217;ve improved JavaScript performance once again with type inference. We&#8217;ve made significant improvements on both the [...]]]></description>
			<content:encoded><![CDATA[<p>We have just released Aurora 9 (<a href="http://www.mozilla.org/en-US/firefox/channel/">download and test Aurora 9</a>), which is planned to be the upcoming Firefox 9. In it, we have a number of new things that we hope will get you excited!</p>
<p><span id="more-9688"></span></p>
<h2>JavaScript Type Inference</h2>
<p>We&#8217;ve improved JavaScript performance once again with type inference. We&#8217;ve made significant improvements on both the V8 and Kraken benchmarks, and you should see real-world improvements to JavaScript performance as well.</p>
<h2>JavaScript Interface for Do Not Track</h2>
<p>Firefox 4 introduced <a href="https://developer.mozilla.org/en/The_Do_Not_Track_Field_Guide/Introduction/How_Do_Not_Track_works">Do Not Track</a>, and now there is a way to detect a users&#8217;prefence through JavaScript as well:</p>
<p><iframe style="width: 100%; height: 100px;" src="http://jsfiddle.net/robnyman/cFqXk/1/embedded/" width="320" height="240"></iframe></p>
<h2>mouseenter and mouseleave events</h2>
<p>For some time now, <a href="http://www.quirksmode.org/dom/events/mouseover.html">web developers have been struggling</a> with handling <code>mouseover</code> and <code>mouseout</code> events on elements, since when child elements have gained focus, the event has bubbled up and triggered <code>mouseout</code> on the parent element &#8211; something you would in most cases not like to happen.</p>
<p>Therefore, we are now happy to introduce support for <code>mouseenter</code> and <code>mouseleave</code> events:</p>
<p><iframe style="width: 100%; height: 300px;" src="http://jsfiddle.net/robnyman/ZVTa2/12/embedded/" width="320" height="240"></iframe></p>
<h2>Camera UI for Mobile</h2>
<p>You can now use an input element to trigger a native app to take pictures. Please try the <a href="http://jsbin.com/iwerow/27/">Camera UI for Mobile demo</a>.</p>
<h2>Support for chunked XMLHttpRequest</h2>
<p>When doing XMLHttpRequest requests with large data sets, you can now <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=687087">get partial data</a> as it arrives instead of waiting for it all to show up. You can use this to build more responsive and more efficient web sites.</p>
<h2>Other changes</h2>
<p>We&#8217;ve also got support for a bunch of other changes as well.  These will be added to the <a href="https://developer.mozilla.org/en/Firefox_9_for_developers">Firefox 9 for Developers page over the next few weeks.</a></p>
<div class="changelog">
<h4>HTML</h4>
<ul>
<li>We now support <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=654352">document.caretPositionFromPoint</a>
<li>We now support <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=683852">Node.contains(node)</a>
<li>We now <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=685139">return true for node.contains(node)</a>, as other browsers do.  (The spec says we should return false but the spec is probably wrong given the behaviour of all browsers.)
<li>We now support <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=685798">Node.parentElement</a>
<li>We no longer <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=685518">taint the canvas data when drawing images where @crossorigin is set on them</a>
<li>We now only <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=682554">fire onreadystatechange on the document element</a>
<li>There is now <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=605365">UI for HTML5 Forms Validation in Fennec</a>
<li>We now fire <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=185236">load and error events on stylesheet link elements that are loaded dynamically</a>
<li>We now support <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=543789">DOM3 composition events</a>
</ul>
</div>
<div class="changelog">
<h4>Graphics</h4>
<ul>
<li>We now support <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=600556">Vista-style ICO files</a></li>
<li>We now <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=573583">decode images only when drawing them</a>, instead of ahead of time</li>
</ul>
</div>
<div class="changelog">
<h4>Layout</h4>
<ul>
<li>We now support the <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=446569">CSS3 columns shorthand where column-count and column-width can be combined</a>
<li>We now support <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=677582">CSS3 text-overflow: &lt;left&gt; &lt;right&gt;</a>
<li>We now support the font-stretch property (<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=3512">bug number 3512, filed in 1999!</a>)
<li>You can now use <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=688405">65534 rowspans instead of 8190</a>, to match IE.
</ul>
</div>
<div class="changelog">
<h4>Video</h4>
<ul>
<li>We now support <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=648595">temporal media URIs for HTML5 video</a>.</li>
</ul>
</div>
<div class="changelog">
<h4>Network</h4>
<ul>
<li>We&#8217;ve changed the way we handle <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=665706">semicolons during URL parsing</a></li>
<li>We no longer pop up a <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=423506">download dialog bog when a server returns no data</a> and instead show a corrupted content error, to match other browsers.</li>
<li>We&#8217;ve made some <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=686312">minor</a> <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=687243">protocol</a> fixes to WebSockets</li>
<li>If you include an <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=272541">empty disposition type it&#8217;s treated as an &#8220;attachment&#8221;</a></li>
<li>We don&#8217;t <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=255119">show redirect content after denying automatic redirect to javascript: URL</a></li>
</ul>
</div>
<div class="changelog">
<h4>Tools</h4>
<ul>
<li>We now <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=612658">cache data for the window.console API</a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/09/introducing-aurora-9/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Tagging docs for sprint at JSConf.eu October 1-2</title>
		<link>http://hacks.mozilla.org/2011/09/tagging-docs-for-sprint-at-jsconf-eu-october-1-2/</link>
		<comments>http://hacks.mozilla.org/2011/09/tagging-docs-for-sprint-at-jsconf-eu-october-1-2/#comments</comments>
		<pubDate>Mon, 19 Sep 2011 17:53:58 +0000</pubDate>
		<dc:creator>Janet Swisher</dc:creator>
				<category><![CDATA[Conferences]]></category>
		<category><![CDATA[Docs]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[MDN]]></category>
		<category><![CDATA[Sprints]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=9536</guid>
		<description><![CDATA[We&#8217;re very excited to announce that Mozilla is sponsoring the Hacker Lounge at JSConf.eu and we will be holding a doc sprint at and during the conference. The focus of this doc sprint will naturally be docs for JavaScript and DOM. We hope to encourage attendees at the conference to contribute at least a little [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re very excited to announce that <a href="http://jsconf.eu/2011/mozilla_the_jsconf_eu_hacker_l.html">Mozilla is sponsoring the Hacker Lounge at JSConf.eu</a> and we will be holding a doc sprint at and during the conference. The focus of this doc sprint will naturally be docs for JavaScript and DOM. We hope to encourage attendees at the conference to contribute at least a little to improving the JS and DOM docs on MDN. I and a handful of MDN community members will be there to show them how.</p>
<p>To that end, we want to tag as many JS and DOM articles as possible, with tags indicating what work needs to be done on those articles. That way, anybody dropping in during the sprint (or anytime later) can look for tagged articles and find something to work on.</p>
<p>Here are some of the tags we use to indicate that an article needs help:</p>
<ul>
<li><strong>NeedsTechnicalReview</strong>: needs someone to verify that the technical information is complete and correct.</li>
<li><strong>NeedsExample</strong>: needs one or more illustrative code examples of the item documented.</li>
<li><strong>NeedsContent</strong>: the item is incomplete and needs to be filled out.</li>
<li><strong>NeedsJSVersion</strong>: needs information about the version of JavaScript and EcmaScript this item first appears in.</li>
<li><strong>NeedsBrowserCompatibility</strong>: needs a browser compatibility table or needs the table filled out.</li>
<li><strong>MakeBrowserAgnostic</strong>: the article is written with a focus on Gecko, when it is actually about a standard function or feature, which should be rewritten to be generic.</li>
</ul>
<p><em>Please help</em> by tagging articles in MDN that need work with the appropriate tag. You can use the Talk page for each article to elaborate on what needs to be done, if the tag is not descriptive enough. To modify tags on an article, login to MDN and click <strong>Edit Tags</strong> at the bottom of the page.</p>
<p>The <a href="https://wiki.mozilla.org/MDN/Doc_sprints/2011October">wiki page for the doc sprint</a> has links to queries for some of these tags. </p>
<p>If you will be at JSConf.eu, I look forward to seeing you there! If you will be participating remotely, I&#8217;ll see you online!</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/09/tagging-docs-for-sprint-at-jsconf-eu-october-1-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Animating with javascript: from setInterval to requestAnimationFrame</title>
		<link>http://hacks.mozilla.org/2011/08/animating-with-javascript-from-setinterval-to-requestanimationframe/</link>
		<comments>http://hacks.mozilla.org/2011/08/animating-with-javascript-from-setinterval-to-requestanimationframe/#comments</comments>
		<pubDate>Wed, 03 Aug 2011 16:05:35 +0000</pubDate>
		<dc:creator>louisremi</dc:creator>
				<category><![CDATA[Animations]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[requestAnimationFrame]]></category>
		<category><![CDATA[setInterval]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=8914</guid>
		<description><![CDATA[Animating DOM elements[1] or the content of a canvas is a classical use case for setInterval. But the interval is not as reliable as it seems, and a more suitable API is now available&#8230; Animating with setInterval To animate an element moving 400 pixels on the right with javascript, the basic thing to do is [...]]]></description>
			<content:encoded><![CDATA[<p><em>Animating DOM elements<a id="note1" href="#endnote1">[1]</a> or the content of a <a href="https://developer.mozilla.org/en/canvas">canvas</a> is a classical use case for <a href="https://developer.mozilla.org/En/Window.setInterval">setInterval</a>. But the interval is not as reliable as it seems, and a more suitable API is now available&#8230;</em></p>
<h3>Animating with setInterval</h3>
<p>To animate an element moving 400 pixels on the right with javascript, the basic thing to do is to move it 10 pixels at a time on a regular interval.<br />
<iframe style="width: 100%; height: 210px" src="http://jsfiddle.net/louisremi/brcvG/embedded/js,result"></iframe><br />
An HTML5 game based on this logic would normally run at ~60fps<a id="note2" href="#endnote2">[2]</a>, but if the animations were too complex or running on a low-spec. device (a mobile phone for instance) and processing a frame were taking more than 16ms, then the game would run at a lower framerate: when processing 1 frame takes 33ms, the game runs at 30fps and game elements move twice as slowly as they should. Animations would still look smooth enough, but the game experience would be altered.</p>
<style>
.gist { font-size: .8em; }
.gist-meta { font-size: .6em !important; }
.gist-file { margin: 0 !important; }
.code {font-size: .8em; background: #F8F8FF; border: 1px solid #DEDEDE; }
</style>
<h3>Animating at constant speed</h3>
<p>To animate at constant speed, we need to calculate the time delta since the last frame and move the element proportionally.<br />
<script src="https://gist.github.com/1114293.js?file=intervalLoop_constant.js"></script></p>
<h3>Animating with requestAnimationFrame</h3>
<p>Since the interval parameter is irrelevant in complex animations, as there&#8217;s no guarantee that it will be honored, a new API has been designed: <a href="https://developer.mozilla.org/en/DOM/window.mozRequestAnimationFrame">requestAnimationFrame</a>. It&#8217;s simply a way to tell the browser &#8220;before drawing the next frame on the screen, execute this game logic/animation processing&#8221;. The browser is in charge of choosing the best moment to execute the code, which results in a more efficient use of resources<a id="note3" href="#endnote3">[3]</a>.</p>
<p>Here&#8217;s how an animation with requestAnimationFrame would be written.<br />
<strong>Note: Following code snippets don&#8217;t include feature detections and workarounds necessary to work in current browsers. Should you want to play with them, you should try the <a href="https://gist.github.com/1114293#file_anim_loop_x.js">ready-to-use animLoop.js</a>.</strong><br />
<script src="https://gist.github.com/1114293.js?file=animLoop_raf.js"></script></p>
<h3>Dealing with inactive tabs</h3>
<p>requestAnimationFrame was built with another benefit in mind: letting the browser choose the best frame interval allows to have a long interval in inactive tabs. Users could play a CPU intensive game, then open a new tab or minimize the window, and the game would pause<a id="note4" href="#endnote4">[4]</a>, leaving resources available for other tasks.<br />
<em>Note: the potential impact of such behavior on resource and battery usage is so positive that browser vendors decided to adopt it for setTimeout and setInterval as well<a id="note5" href="#endnote5">[5]</a>.</em></p>
<p>This behavior also means that the calculated time delta might be really high when switching back to a tab containing an animation. This will result in animation appearing to jump or creating &#8220;<a href="http://en.wikipedia.org/wiki/Wormhole">wormholes</a>&#8220;<a id="note6" href="#endnote6">[6]</a>, as illustrated here:<br />
<iframe style="width: 100%; height: 150px" src="http://jsfiddle.net/louisremi/qWcMG/3/embedded/result"></iframe></p>
<p>Wormholes can be fixed by clamping the time delta to a maximum value, or not rendering a frame when the time delta is too high.<br />
<script src="https://gist.github.com/1114293.js?file=animLoop_fixed.js"></script><br />
<iframe style="width: 100%; height: 150px" src="http://jsfiddle.net/louisremi/KDaEh/2/embedded/result"></iframe></p>
<h3>Problems with animation queues</h3>
<p>Libraries such as jQuery queue animations on elements to execute them one after the other. This queue is generally only used for animations that are purposefully consecutive.<br />
But if animations are triggered by a timer, the queue might grow without bound in inactive tabs, as paused animations stack up in the queue. When switching back to affected tabs, a user will see a large number of animations playing consecutively when only one should happen on a regular interval.<br />
<iframe style="width: 100%; height: 190px" src="http://jsfiddle.net/louisremi/GXuPq/3/embedded/js,result"></iframe></p>
<p>This problem is visible in some auto-playing slideshows such as <a href="http://pupunzi.com/#mb.components/mb.gallery/gallery.html">mb.gallery</a>. To work around it, developers can empty animation queues before triggering new animations<a id="note7" href="#endnote7">[7]</a>.<br />
<iframe style="width: 100%; height: 190px" src="http://jsfiddle.net/louisremi/An834/embedded/js,result"></iframe></p>
<h3>Conclusion</h3>
<p>The delays of setTimeout and setInterval and of course requestAnimationFrame are unpredictable and much longer in inactive tabs. These facts should be taken into account not only when writing animation logic, but in fps counters, time countdowns, and everywhere time measurement is crucial.</p>
<p><small><a id="endnote1" href="#note1">[1]</a> The DOM can now be animated with <a href="https://developer.mozilla.org/en/CSS/CSS_transitions">CSS3 Transitions</a> and <a href="https://developer.mozilla.org/en/CSS/CSS_animations">CSS3 Animations</a>.<br />
<a id="endnote2" href="#note2">[2]</a> 1 frame every 16ms is 62.5 frames per second.<br />
<a id="endnote3" href="#note3">[3]</a> See the <a href="http://msdn.microsoft.com/en-us/ie/hh272906">illustration of this fact</a> on msdn.<br />
<a id="endnote4" href="#note4">[4]</a> The behavior of requestAnimationFrame in inactive tabs is still <a href="http://www.w3.org/TR/2011/WD-animation-timing-20110602/">being worked on</a> at the w3c, and might differ in other browsers.<br />
<a id="endnote5" href="#note5">[5]</a> See <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=633421">related Firefox bug</a> and <a href="http://code.google.com/p/chromium/issues/detail?id=66078">related chromium bug</a>.<br />
<a id="endnote6" href="#note6">[6]</a> This term was first coined by <a href="http://sethladd.com/">Seth Ladd</a> in his &#8220;<a href="http://io-2011-html5-games-hr.appspot.com/#36">Intro to HTML5 Game Development</a>&#8221; talk.<br />
<a id="endnote7" href="#note7">[7]</a> See documentation of your js library, such as <a href="http://api.jquery.com/category/effects/">effects</a> and <a href="http://api.jquery.com/stop">stop()</a> for jQuery.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/08/animating-with-javascript-from-setinterval-to-requestanimationframe/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A call for quality HTML5 demo markup</title>
		<link>http://hacks.mozilla.org/2010/12/a-call-for-quality-html5-demo-markup/</link>
		<comments>http://hacks.mozilla.org/2010/12/a-call-for-quality-html5-demo-markup/#comments</comments>
		<pubDate>Sun, 19 Dec 2010 20:32:29 +0000</pubDate>
		<dc:creator>Janet Swisher</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Images]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Standards]]></category>
		<category><![CDATA[Web Developers]]></category>
		<category><![CDATA[bestpractices]]></category>
		<category><![CDATA[cleancode]]></category>
		<category><![CDATA[demos]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[maintainability]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=6836</guid>
		<description><![CDATA[HTML5 is a necessary evolution to make the web better. Before the HTML5 specs were created we used (and still use) a hacked together bunch of systems meant for describing and linking documents to create applications. We use generic elements to simulate rich interaction modules used in desktop development and we make assumptions as to [...]]]></description>
			<content:encoded><![CDATA[<p>HTML5 is a necessary evolution to make the web better. Before the HTML5 specs were created we used (and still use) a hacked together bunch of systems meant for describing and linking documents to create applications. We use generic elements to simulate rich interaction modules used in desktop development and we make assumptions as to what the user agent (browser) can do for the end user. </p>
<p>The good thing about this mess is that it taught us over the last few years to be paranoid in our development approach &#8211; we realised that in order to deliver working, accessible, maintainable and scalable systems we have to be professional and intelligent in our decisions and especially in our planning and architecting. </p>
<p>The trouble is that with the excitement around the cool new HTML5 things to play with a lot of these principles get lost and discarded as outdated. They aren&#8217;t &#8211; part of the mess that the web is in is that in 1999 a lot of people discarded everything else and instead worked only on the new hotness that was Internet Explorer 6 and DHTML. Let&#8217;s not repeat that mistake.</p>
<h2>The two faces of HTML5 enthusiasm</h2>
<p>Right now there are two things to get really excited about in HTML5: the richer, more meaningful semantics of new elements and the APIs that give us deep access into the workings of the browser and the operating system. The former allows us to build much richer interfaces and the latter allows us to build much more complex applications. </p>
<p>All of this comes with the awesome of view source (or check in development tools) for debugging. You can advocate HTML5 by writing clean and useful markup. You can kill it by treating the markup as a second class citizen which is only there to serve the script. </p>
<p>The markup enthusiasts are very happy about HTML5 and make it easy for people to upgrade &#8211; lots of cool new <a href="http://nicolasgallagher.com/anatomy-of-an-html5-wordpress-theme/">blog templates</a> and <a href="http://html5boilerplate.com/">boilerplate HTML templates</a> are being built and <a href="http://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-browser-Polyfills">polyfills</a> created to allow using the new features and not leave behind outdated browsers. </p>
<p>On the app development side of things it looks much different and that is just sad. My only possible explanation is that people who come from desktop environments now tackle the HTML5 APIs without ever having to care about markup. The pragmatism of HTML5 allows a much more forgiving syntax than XHTML but it shouldn&#8217;t mean that we can just re-apply all the bad mistakes that held us back when it comes to maintenance for years. </p>
<p>During my career as a web developer I realised a few things make a lot of sense when building web apps:</p>
<ul>
<li><strong>If there is an element for a certain task &#8211; use that one</strong>. It is very likely that the element comes with accessibility and interaction features for free that you would otherwise have to simulate.</li>
<li><strong>Separate CSS, JavaScript and HTML</strong> &#8211; which means it is easy to refactor your code without having to change all of them. It also means you can work in parallel with others instead of breaking each other&#8217;s code</li>
<li><strong>Never rely on markup or content</strong> &#8211; as sooner or later some editing tool will come into place that messes with everything you created</li>
</ul>
<p>This means a lot of things: </p>
<ul>
<li>For starters it means that inline styles are simply evil as they override any settings you have in your style sheets. Only use them when this is exactly what you need to do or when you calculate them dynamically.</li>
<li>The same applies to inline scripting. If you have an <code>onclick="foo()"</code> somewhere in your HTML and <code>foo()</code> changes to <code>bar()</code> you have to rename it in every HTML document (of course nowadays it is one template, but it still means hunting down a reference you might miss)</li>
<li>If instead of using a native HTML element for a certain job you use SPANs and DIVs you&#8217;ll have to add classes to make them look and work &#8211; and simulate the keyboard accessibility, too.</li>
<li>You can&#8217;t rely on the text value of any element. A <code>&lt;button&gt;edit&lt;/button&gt;</code> using the &#8220;edit&#8221; as the trigger for certain functionality would have to have the JS localised, too when you create the German <code>&lt;button&gt;bearbeiten&lt;/button&gt;.</code></li>
</ul>
<h2>Bla bla bla&#8230; C&#8217;mon Chris, it isn&#8217;t that bad!</h2>
<p>The above best practices have been mentioned for years and a lot of people get sick of seeing them repeated. After all, this is intelligent development and standard practice in backend technologies. I came across a lot of &#8220;creative&#8221; uses lately though when doing a view-source on HTML5 demos &#8211; specifically the ones in the HTML5 advent calendar. And here is my yield of what people do.</p>
<h2>Simulating a navigation list</h2>
<p>One of the first things I encountered was a painting tool that had a gallery of created graphics as a background. Now, to me this would be a simple list:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;{link}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;{src}&quot;</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;{title}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  [...]
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The markup I found though was this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;sky&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;skyTile&quot;</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;{calculated styles};display:block&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;skyThumb&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;{src}&quot;</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;{generated styles}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;images/thumb_dropShadow.png&quot;</span> </span>
<span style="color: #009900;">         <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;skyThumbShadow&quot;</span> </span>
<span style="color: #009900;">         <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;{generated styles}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  [...]
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This, of course is generated by the backend. My first gripe is the dropshadow image, after all this is an HTML5 showcase &#8211; just use CSS3. We also have the three instances of generated styles and double classes. Granted, an extra class gives you a handle to all images instead of tiles, so why not. But as there is no real link around the image, the click handler has to read the url from somewhere. There is a lot of unnecessary back and forth between DOM and scripting here which does slow down the whole demo. Seeing that this is also the main navigation from the entry page to the editor this could be a list inside a <a href="http://www.w3.org/TR/html5/sections.html#the-nav-element"><code>nav</code></a> element. A list groups these items together, a bunch of DIVs doesn&#8217;t (screen readers for example tell you how many items there are in a list). </p>
<p>Another list I found was supposed to be links to refresh the app and have a hierarchy but instead was a flat list with classes to define hierarchy and group:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ul</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;mitems&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;hedtop&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Simple<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;mol&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>{name}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  [... repeated ...]
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;hed&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>{parent name}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;mol&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>{name}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  [... repeated ...]
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This could be OK, granted you also shift keyboard focus, but why not:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;nav<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ul</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;mitems&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>{parent name}
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;{url}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>{name}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      [... repeated ...]
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  [... repeated ...]
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/nav<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This would give you styling hooks and functionality for free. Links and buttons are great to trigger functionality &#8211; but it seems that is too easy.</p>
<h2>Click (probably) here for functionality</h2>
<p>If I build a button for an application to trigger a certain functionality this is the markup:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;button</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;button&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;edit&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Edit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/button<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><a href="http://www.w3.org/TR/html5/the-button-element.html#the-button-element">Buttons</a> are what trigger functionality &#8211; both a backend script or a JavaScript. They are keyboard accessible, have a disabled state and sensible out-of-the-box styling that nowadays can be completely overwritten. The class can be the definition of what the button should do &#8211; instead of the text which will change. You could use an ID but a class allows to repeat buttons (for example on the top and the bottom of a results list).</p>
<p>The buttons I found though were slightly different:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;homeButtonPanel&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;homeButton&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;drawHomeButton&quot;</span> </span>
<span style="color: #009900;">       <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;display: block;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Start Drawing<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;homeButton&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;viewHomeButton&quot;</span> </span>
<span style="color: #009900;">       <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;display: block;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>View the Mural<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;controlPanel&quot;</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;display: block;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;titleButton&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>{title}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;mainMenu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;mainButton mainSelected&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;drawButton&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Draw<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;inkDisplay&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;inkMeter&quot;</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;width: 94px;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;menuContainer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;menuButton drawMenuButton&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;behaviorsButton&quot;</span></span>
<span style="color: #009900;">           <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;display: block; opacity: 1;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Gesture<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Behaviors<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;menuContainer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;menuButton drawMenuButton&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;artworkButton&quot;</span> </span>
<span style="color: #009900;">           <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;display: block; opacity: 1;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Artwork<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;menuContainer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;menuButton drawMenuButton&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;resetButton&quot;</span> </span>
<span style="color: #009900;">           <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;display: block; opacity: 1;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Reset<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;menuContainer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;menuButton drawMenuButton&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;undoButton&quot;</span> </span>
<span style="color: #009900;">           <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;display: block; opacity: 1;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Undo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;menuContainer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;menuButton drawMenuButton&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;saveButton&quot;</span> </span>
<span style="color: #009900;">           <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;display: block; opacity: 1;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Save<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;mainMenu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;mainButton&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;viewButton&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>View<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;mainMenu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;secondaryButton&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;helpButton&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Help<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;mainMenu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;mainShare shareButtonSmall&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;mainTwitterButton&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;twitter&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;images/twitter_icon.png&quot;</span> </span>
<span style="color: #009900;">           <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;opacity: 0.6;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;mainShare shareButtonLarge&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;mainFacebookButton&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;facebook&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;images/facebook_icon.png&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;mainShare shareButtonSmall&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;mainEmailButton&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;email&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;images/email_icon.png&quot;</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;opacity: 0.6;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>So instead of using a nested list with classes for each button and the hierarchy in the nesting we have a lot of classes and a hand-rolled DIV construct. Instead of making buttons really disabled we rely on opacity and there is quite a mix of inline images and background images (if all were background images, they could be one sprite!). Keyboard navigation will have to be written for this and if you were to add a button you&#8217;d have to come up with another ID.</p>
<p>HTML5 actually has a construct for this. There is a <a href="http://www.w3.org/TR/html5/interactive-elements.html#menus"><code>menu</code></a> element with <a href="http://www.w3.org/TR/html5/interactive-elements.html#the-command">command</a> child elements. These are in use in Chrome&#8217;s side bar for example and should be what we use. If you want to make it work for everyone, a nested list with <code>button</code> elements is what to go for.</p>
<p>The overly complex DIV construct is quite used though &#8211; this was another example:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;uiScreen&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;startScreen&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;panelBackground&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;mainResumeButton&quot;</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;display: none;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;largeUIButton&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;resumeButton&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Resume Game<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;panelBackground&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;mainStartButton&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;largeUIButton&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;startButton&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Start Game<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>		
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;panelBackground&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;mainHelpButton&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;largeUIButton&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;helpButton&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Help<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;panelBackground&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;mainHighscoreButton&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;largeUIButton&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;highscoreButton&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>High Scores<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>When in doubt &#8211; add an ID and class to everything.</p>
<p>Other buttons I encountered were actually links pointing to <code>javascript://</code> using an inline style to indicate hierarchy:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ul</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;navlist&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;play&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;javascript://&quot;</span> </span>
<span style="color: #009900;">         <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;display: inline;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Play<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>    
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;pause&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;javascript://&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Pause<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;padding-left: 2px;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Effects:<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;bw&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;javascript://&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Bw<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;outline&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;javascript://&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Outline<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;color&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;javascript://&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Color<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;invert&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;javascript://&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Invert<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;matrix&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;javascript://&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Matrix<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;old&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;javascript://&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Old<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Talking of inline &#8211; here&#8217;s a great example of a tool generating a lot of code that could be replaced by a single event handler and <a href="http://icant.co.uk/sandbox/eventdelegation/">event delegation</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;tools&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">onmouseout</span>=<span style="color: #ff0000;">&quot;buttonOut(1)&quot;</span> <span style="color: #000066;">onmouseover</span>=<span style="color: #ff0000;">&quot;buttonOver(1)&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">onclick</span>=<span style="color: #ff0000;">&quot;buttonClicked(1)&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;button1&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;button&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;image/button/1.png&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">onmouseout</span>=<span style="color: #ff0000;">&quot;buttonOut(2)&quot;</span> <span style="color: #000066;">onmouseover</span>=<span style="color: #ff0000;">&quot;buttonOver(2)&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">onclick</span>=<span style="color: #ff0000;">&quot;buttonClicked(2)&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;button2&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;button&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;image/button/2.png&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  [...repeated 20 times...]
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;toolsSeparator&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #ddbb00;">&amp;nbsp;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;toolbarButtonSave&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;toolbarButton&quot;</span></span>
<span style="color: #009900;">     <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;javascript:saveCanvas()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;image/save.png&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;New&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;toolbarButtonNew&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;toolbarButton&quot;</span></span>
<span style="color: #009900;">     <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;javascript:newCanvas()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;New&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;image/new.png&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #808080; font-style: italic;">&lt;!--[if !IE]&gt;&lt;![IGNORE[--&gt;&lt;!--[IGNORE[]]--&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;toolbarButtonMenu&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;toolbarButton&quot;</span></span>
<span style="color: #009900;">     <span style="color: #000066;">onmouseout</span>=<span style="color: #ff0000;">&quot;closeMenuDelay()&quot;</span> <span style="color: #000066;">onmouseover</span>=<span style="color: #ff0000;">&quot;showMenuHover()&quot;</span></span>
<span style="color: #009900;">     <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;javascript:showMenu()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;&amp;gt;&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;image/menu.png&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">onmouseout</span>=<span style="color: #ff0000;">&quot;closeMenuDelay()&quot;</span> <span style="color: #000066;">onmouseover</span>=<span style="color: #ff0000;">&quot;overMenu()&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;menu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;saveMenu&quot;</span> <span style="color: #000066;">onmouseout</span>=<span style="color: #ff0000;">&quot;closeMenuDelay()&quot;</span> </span>
<span style="color: #009900;">       <span style="color: #000066;">onmouseover</span>=<span style="color: #ff0000;">&quot;overMenu()&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;javascript:saveCanvas()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      save normal
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;saveMenu&quot;</span> <span style="color: #000066;">onmouseout</span>=<span style="color: #ff0000;">&quot;closeMenuDelay()&quot;</span> </span>
<span style="color: #009900;">       <span style="color: #000066;">onmouseover</span>=<span style="color: #ff0000;">&quot;overMenu()&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;javascript:saveCanvasHi()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      save high quality
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;footnote&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #ddbb00;">&amp;nbsp;</span> (rename to *.png)<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">onmouseout</span>=<span style="color: #ff0000;">&quot;closeMenuDelay()&quot;</span> <span style="color: #000066;">onmouseover</span>=<span style="color: #ff0000;">&quot;overMenu()&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;javascript:showAbout()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      about...
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;lastMenu&quot;</span> <span style="color: #000066;">target</span>=<span style="color: #ff0000;">&quot;_top&quot;</span> <span style="color: #000066;">onmouseout</span>=<span style="color: #ff0000;">&quot;closeMenuDelay()&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">onmouseover</span>=<span style="color: #ff0000;">&quot;overMenu()&quot;</span> </span>
<span style="color: #009900;">       <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;{url}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;footnote&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;em<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>elsewhere:<span style="color: #ddbb00;">&amp;nbsp;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/em<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;em<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>a sound memory game<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/em<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #808080; font-style: italic;">&lt;!--&lt;![endif]--&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Notice that if the images for the button couldn&#8217;t be loaded for one reason or another (or you can&#8217;t see them at all) this application is very confusing indeed &#8211; no alternative text for the images and no text content to fall back to. I am also very much sure that the in and out handlers trigger visual effects CSS could handle better.</p>
<h2>Reasons and effects</h2>
<p>I know there are probably good reasons for all of this, and I am sure I will also do things wrongly when I am rushed or want to get things out quickly. What we have to understand though is that right now we have a responsibility to show the best of breed demos we can. </p>
<p>We cannot preach the open web and technologies and view-source over closed systems and at the same time forget the things we learnt in the last decade. Some of these things I found look like code Frontpage or Dreamweaver could have produced in the 90ies and resulted in a lot of badly performing, hard to maintain products that either still annoy people who have to use them or get replaced every 2 years.</p>
<p>We have a mandate to educate the new developers coming to the web. Unlearning something is much harder than learning it &#8211; so let&#8217;s not start with bloat and quick fixes that work but forget to advocate clean code and thinking about the impact your product has on the end users (thinking accessibility) and the poor sods that will have to maintain your product when you are gone. We are not here to advocate effects and products, we are here to promote the tools that allow anyone to easily build something cool that is also easy to understand. </p>
<p>HTML5 is about evolving the web as a platform &#8211; we also need to evolve with it and take more responsibility. We have app and we have markup enthusiasts. Let&#8217;s make them work together to build things that are great functionality and clean semantics.</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2010/12/a-call-for-quality-html5-demo-markup/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>HTML5 adoption stories: box.net and html5 drag and drop</title>
		<link>http://hacks.mozilla.org/2010/06/html5-adoption-stories-box-net-and-html5-drag-and-drop/</link>
		<comments>http://hacks.mozilla.org/2010/06/html5-adoption-stories-box-net-and-html5-drag-and-drop/#comments</comments>
		<pubDate>Wed, 23 Jun 2010 15:46:12 +0000</pubDate>
		<dc:creator>Christopher Blizzard</dc:creator>
				<category><![CDATA[Device APIs]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Drag and Drop]]></category>
		<category><![CDATA[FileAPI]]></category>
		<category><![CDATA[Firefox 3.6]]></category>
		<category><![CDATA[Firefox 4]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[XMLHttpRequest]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=5012</guid>
		<description><![CDATA[This is a guest post from Tomas Barreto, a developer who works at box.net. They recently adopted HTML5 drag and drop as a way to share files with other people using new features in Firefox. The included video is a pitch for the feature and service, but shows how easy it is to do simple [...]]]></description>
			<content:encoded><![CDATA[<p><em>This is a guest post from Tomas Barreto, a developer who works at <a href="http://box.net/">box.net</a>.  They recently adopted HTML5 drag and drop as a way to share files with other people using new features in Firefox.  The included video is a pitch for the feature and service, but shows how easy it is to do simple HTML5-based upload progress even with multiple files.  Tomas gives an overview of the relatively simple JavaScript required to do this, and how improvements in Firefox 4 will make things even easier.  Also have a quick look at the bottom of the post for links to additional docs and resources.<br />
</em></p>
<p>At Box.net, we’re always exploring new ways to help users get content quickly and securely onto our cloud content management platform. So when asked, “What feature would make you use Box more?” during the Box Hack Olympics in April, my colleague CJ and I decided to tackle the most intuitive way to upload files: simply dragging them from the desktop into Box.</p>
<p>We considered technologies ranging from Gears to Firefox plugins, but only HTML5 had sufficient adoption. By using some of the JavaScript APIs defined in the HTML5 standard, CJ and I could create a seamless drag and drop experience for our users on supporting browsers. Furthermore, using an HTML5-based upload feature would allow us to enable users to select multiple files at once, and also display progress on the client without polling. And with HTML5 adoption across the latest versions of three of the top four browsers, we felt confident about building an upload method based on this new technology without the trade-offs of using a third-party plug-in.</p>
<p>We rolled out the first rev of our drag and drop feature a few weeks ago, and we’re impressed with how quickly it has been adopted. It’s already one of the most popular ways to get files onto Box, and in its first week it surpassed our old AJAX upload method. You can check out our demo video to get a feel for the feature:</p>
<p><object width="500" height="304"><param name="movie" value="http://www.youtube-nocookie.com/v/rkY7MGilQZY&#038;hl=en_US&#038;fs=1&#038;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube-nocookie.com/v/rkY7MGilQZY&#038;hl=en_US&#038;fs=1&#038;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="500" height="304"></embed></object></p>
<p>To build this feature, we referenced a handful of online examples that explained how to use Firefox 3 <a href="https://developer.mozilla.org/en/DOM/FileReader">FileReader</a> object and the drag and drop file event support. Our first implementation used this object to load the file into memory and then took advantage of the latest <a href="https://developer.mozilla.org/En/Using_XMLHttpRequest">XMLHttpRequest</a> events to track progress on the client.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> files <span style="color: #339933;">=</span> event.<span style="color: #660066;">originalEvent</span>.<span style="color: #660066;">dataTransfer</span>.<span style="color: #660066;">files</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// drop event</span>
<span style="color: #003366; font-weight: bold;">var</span> reader <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FileReader<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
reader.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> file_contents <span style="color: #339933;">=</span> event.<span style="color: #660066;">target</span>.<span style="color: #660066;">result</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> request <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  ... <span style="color: #006600; font-style: italic;">// attach event listeners to monitor progress and detect errors</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> post_body <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
&nbsp;
  .. <span style="color: #006600; font-style: italic;">// build post body</span>
&nbsp;
  post_body <span style="color: #339933;">+=</span> file_contents<span style="color: #339933;">;</span>
&nbsp;
  .. <span style="color: #006600; font-style: italic;">// finish post body</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://example.com/file_upload'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> request <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  request.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span>  url<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// open asynchronous post request</span>
  request.<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'content-type'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'multipart/form-data; boundary=&quot;&quot;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// make sure to set a boundary</span>
  request.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span>post_body<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
reader.<span style="color: #660066;">readAsBinaryString</span><span style="color: #009900;">&#40;</span>files<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This approach worked well because we could use the same server processing code that we previously used for uploads. The main disadvantage here is that the FileReader object reads the entire file into memory, which is not optimal for a general upload use case. Our current HTML5 implementation uses this logic and has forced us to restrict drag and drop uploads to just 25mb.  However, thanks to recommendations from the Mozilla team, we’ll be taking an alternative approach for V2 of drag and drop, where the file is read chunks as needed by the request. Here’s how we’re going to do it:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> files <span style="color: #339933;">=</span> event.<span style="color: #660066;">originalEvent</span>.<span style="color: #660066;">dataTransfer</span>.<span style="color: #660066;">files</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// drop event</span>
<span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://example.com/file_upload'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> request <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
request.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span>  url<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// open asynchronous post request</span>
request.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span>files<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Since this approach is not formatted as a multipart form-data, it will require some adjustments on our back-end to support receiving file uploads in this way. However, it&#8217;s definitely worth the trade-off since we’ll get all the benefits of the previous method and we don&#8217;t need special file size restrictions. In the future, we&#8217;ll consider using <a href="http://hacks.mozilla.org/2010/05/formdata-interface-coming-to-firefox/">yet another way to efficiently upload files that is supported in Firefox 4 and uses the traditional multi-part form</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> files <span style="color: #339933;">=</span> event.<span style="color: #660066;">originalEvent</span>.<span style="color: #660066;">dataTransfer</span>.<span style="color: #660066;">files</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// drop event</span>
<span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://example.com/file_upload'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> request <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> fd <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FormData<span style="color: #339933;">;</span>
fd.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;myFile&quot;</span><span style="color: #339933;">,</span> files<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
request.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span>  url<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// open asynchronous post request</span>
request.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span>fd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We’re already exploring more ways to enrich the Box experience using HTML5. With HTML5, we can build faster, richer and more interactive features with native browser support, and bridge the traditional gap between desktop software and web applications. Here are just a few cool new upload-related features on our roadmap:</p>
<ul>
<li>Pause/Resume uploads using the Blob slice API to split files into chunks (this will be a huge robustness boost, especially for large uploads)</li>
<li>Allowing uploads to resume even after the browser closes by caching the file using <a href="http://hacks.mozilla.org/2010/06/comparing-indexeddb-and-webdatabase/">IndexedDB</a> support (possibly in Firefox 4)</li>
</ul>
<p>We’d also like to begin a discussion about supporting the reverse drag and drop use case: dragging files from the browser to the desktop. Based on our users’ enthusiasm around the drag and drop upload feature, we think the reverse functionality would well received. If you are interested in contributing to a specification for this feature, please let us know (html5 [-at$] box.net)!</p>
<p>Resources:</p>
<ul>
<li><a href="https://developer.mozilla.org/en/using_files_from_web_applications">Using File APIs from web applications</a>
<li><a href="https://developer.mozilla.org/En/Using_XMLHttpRequest">Using XMLHttpRequest</a>
<li><a href="https://developer.mozilla.org/En/DragDrop/Drag_and_Drop">Using HTML5 Drag and Drop</a><br />
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2010/06/html5-adoption-stories-box-net-and-html5-drag-and-drop/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Firefox 4: Better performance with Lazy Frame Construction</title>
		<link>http://hacks.mozilla.org/2010/05/better-performance-with-lazy-frame-construction/</link>
		<comments>http://hacks.mozilla.org/2010/05/better-performance-with-lazy-frame-construction/#comments</comments>
		<pubDate>Wed, 26 May 2010 09:51:56 +0000</pubDate>
		<dc:creator>Paul Rouget</dc:creator>
				<category><![CDATA[DOM]]></category>
		<category><![CDATA[Firefox 4]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=4921</guid>
		<description><![CDATA[This is a re-post from Timothy Nikkel’s blog. Lazy Frame Construction is new to Gecko and allows many DOM operations (appendChild, insertBefore, etc) to not trigger immediate reflows. This can vastly improve the interactive performance of very complex web pages. If you want to test this out, you should get a Firefox Nightly. Lazy frame [...]]]></description>
			<content:encoded><![CDATA[<p><em>This is a re-post from <a href="http://tinikkel.wordpress.com/2010/05/24/lazy-frame-construction/">Timothy Nikkel’s blog</a>.</em></p>
<p><em>Lazy Frame Construction is <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=502937">new to Gecko</a> and allows many DOM operations (appendChild, insertBefore, etc) to not trigger immediate reflows.  This can vastly improve the interactive performance of very complex web pages.  If you want to test this out, you should get a <a href="http://nightly.mozilla.com">Firefox Nightly.</a></em></p>
<p>Lazy frame construction recently landed on mozilla-central. To explain what this means and how this improves things we need some background. Each node in the DOM tree of a webpage has a frame created for it that is used to determine where on the page the node is drawn and its size. A frame corresponds closely to the concept of a <a href="http://www.w3.org/TR/CSS2/box.html">box from the CSS spec</a>. We used to create frames for DOM nodes eagerly; that is as soon as a node was inserted into the document we would create a frame for it. But this can create wasted effort in many situations. For example if a script inserts a large number of nodes into the DOM we would create a frame for each node when it is inserted. But with lazy frame construction we can process all those nodes at once in a big batch, saving overhead. Furthermore the time it takes to create those frames no longer blocks that script, so the script can go and do what it needs to and the frames will get created when they are needed. There are other situations where a script would insert nodes into the document and remove them immediately, so there is no need to ever create a frame for these as they would never be painted on screen.</p>
<p>So now when a node is inserted into a document the node is flagged for needing a frame created for it, and then the next time the refresh driver notifies (currently at 20 ms intervals) the frame is created. The refresh driver is also what drives reflow of webpages and CSS &amp; SVG animations.</p>
<p>Let&#8217;s look at two examples where lazy frame construction helps.</p>
<p>In this example we insert 80000 div elements and then we flush all pending layout to time how long it takes before the changes made by the script are done and visible to the user. The script can continue executing without flushing layout, but we do it here to measure how long the actual work takes.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> stime <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> container <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;container&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> lastchild <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;lastchild&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">80000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> div <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  container.<span style="color: #660066;">insertBefore</span><span style="color: #009900;">&#40;</span>div<span style="color: #339933;">,</span> lastchild<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
document.<span style="color: #660066;">documentElement</span>.<span style="color: #660066;">offsetLeft</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// flush layout</span>
<span style="color: #003366; font-weight: bold;">var</span> now <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> millisecondselapsed <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>now.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> stime.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><script type="text/javascript">
function test1() {
var stime = new Date();
var container = document.getElementById("container");
var lastchild = document.getElementById("lastchild");
for (var i = 0; i < 80000; i++) {
  var div = document.createElement("div");
  container.insertBefore(div, lastchild);
}
document.documentElement.offsetLeft; // flush layout
var now = new Date();
var millisecondselapsed = (now.getTime() - stime.getTime());
document.getElementById("test1").innerHTML = millisecondselapsed + "ms";
}
</script></p>
<p><button id="test1" onclick="test1()">test (can freeze your browser)</button></p>
<div id="container"></div>
<p>With lazy frame construction we are able to process the insertion of all 80000 div elements in one operation, saving the overhead of 80000 different inserts. In a build without lazy frame construction I get an average time of 1358 ms, with lazy frame construction I get 777 ms.</p>
<p>This example comes from a real webpage. We append a div and then set &#8220;div.style.position = &#8216;absolute&#8217;;&#8221;, and repeat that 2000 times, and then we flush all pending layout to time how long it takes before the changes made by the script are done and visible to the user.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> stime <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> container <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;container2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">2000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> div <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  container.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>div<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  div.<span style="color: #660066;">style</span>.<span style="color: #660066;">position</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;absolute&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
document.<span style="color: #660066;">documentElement</span>.<span style="color: #660066;">offsetLeft</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// flush layout</span>
<span style="color: #003366; font-weight: bold;">var</span> now <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> millisecondselapsed <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>now.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> stime.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><script type="text/javascript">
function test2() {
var stime = new Date();
var container = document.getElementById("container2");
for (var i = 0; i < 2000; i++) {
  var div = document.createElement("div");
  container.appendChild(div);
  div.style.position = "absolute";
}
document.documentElement.offsetLeft; // flush layout
var now = new Date();
var millisecondselapsed = (now.getTime() - stime.getTime());
document.getElementById("test2").innerHTML = millisecondselapsed + "ms";
}
</script></p>
<p><button id="test2" onclick="test2()">test (can freeze your browser)</button></p>
<div id="container2"></div>
<p>With lazy frame construction we don't even bother creating the frame for the div until after the position has been set to absolute, so we don't waste any effort. In a build without lazy frame construction I get an average time of 4730 ms, with lazy frame construction I get 130 ms.</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2010/05/better-performance-with-lazy-frame-construction/feed/</wfw:commentRss>
		<slash:comments>40</slash:comments>
		</item>
		<item>
		<title>multiple file input in Firefox 3.6</title>
		<link>http://hacks.mozilla.org/2009/12/multiple-file-input-in-firefox-3-6/</link>
		<comments>http://hacks.mozilla.org/2009/12/multiple-file-input-in-firefox-3-6/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 21:20:46 +0000</pubDate>
		<dc:creator>Paul Rouget</dc:creator>
				<category><![CDATA[Device APIs]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Feature]]></category>
		<category><![CDATA[Featured Article]]></category>
		<category><![CDATA[FileAPI]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Firefox 3.6]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=2905</guid>
		<description><![CDATA[Firefox 3.6 supports multiple file input. This new capability allows you to get several files as input at once, using standard technologies. This is a big improvement, since you used to be constrained to one file at a time, or needed to use a third party (proprietary) application. This will be particularly useful, for example, [...]]]></description>
			<content:encoded><![CDATA[<p>Firefox 3.6 supports <a href="https://developer.mozilla.org/en/DOM/Input.multiple">multiple file input</a>. This new capability allows you to get several files as input at once, using standard technologies. This is a big improvement, since you used to be constrained to one file at a time, or needed to use a third party (proprietary) application. This will be particularly useful, for example, for photo uploads.</p>
<h3>The input tag</h3>
<p>To let your user select a local file, use the input tag on your Web page. This will show the file picker to the user:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;file&quot;</span><span style="color: #66cc66;">/</span>&gt;</span></pre></div></div>

<p>In Firefox 3.6, the input tag has been expanded to support multiple files:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #000066;">multiple</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">/</span>&gt;</span></pre></div></div>

<p>The user will still see the same file picker, but will be able to select more than one file. </p>
<h3>The form tag</h3>
<p>You  can still use the classic <code>form</code> mechanism:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;upload.php&quot;</span> <span style="color: #000066;">enctype</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;multipart/form-data&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'uploads[]'</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #000066;">multiple</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Send&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></div></div>

<p>If the server side code is in PHP, don&#8217;t forget to make sure that the value of the <code>name</code> attribute has brackets. The brackets are not from the HTML specification, but are required to manipulate the result of the request as an array (see <a href="http://www.php.net/manual/en/features.file-upload.multiple.php">PHP documentation</a>). </p>
<p>Here&#8217;s an example, which goes through the file list and prints each file name:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uploads'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;li&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$filename</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Using File API</h3>
<p>Firefox 3.6 also supports <a href="http://hacks.mozilla.org/2009/12/w3c-fileapi-in-firefox-3-6/">FileAPI</a>. This allows you to do extra processing on the client slide before sending the files to the server. You can access the selected files with the <code>files</code> property of the input DOM element and then manipulate the files using the FileAPI. </p>
<p>For example, here&#8217;s how to get the name of each file selected by the user. This is done on the client side, unlike the previous PHP example.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">var</span> input <span style="color: #339933;">=</span> document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input[type='file']&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">// You've selected input.files.length files</span>
  <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> input.<span style="color: #660066;">files</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// input.files[i] is a file object</span>
    <span style="color: #003366; font-weight: bold;">var</span> li <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    li.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> input.<span style="color: #660066;">files</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #000066;">name</span><span style="color: #339933;">;</span>
    ul.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>li<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Demo</h3>
<p>See this mechanism in action in our <a href="http://demos.hacks.mozilla.org/openweb/multipleFiles/">multiple file input demo</a>. You&#8217;ll need <a href="http://www.mozilla.com/en-US/firefox/all-beta.html">Firefox 3.6</a> (beta).</p>
<h3>Documentation</h3>
<p>To learn more about multiple file input, check out the <a href="https://developer.mozilla.org/en/DOM/Input.multiple">documentation on MDC</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2009/12/multiple-file-input-in-firefox-3-6/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>arun talks about html5, fonts and india</title>
		<link>http://hacks.mozilla.org/2009/07/arun-standards-india/</link>
		<comments>http://hacks.mozilla.org/2009/07/arun-standards-india/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 00:37:38 +0000</pubDate>
		<dc:creator>Christopher Blizzard</dc:creator>
				<category><![CDATA[Canvas]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Demo]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Standards]]></category>
		<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=1491</guid>
		<description><![CDATA[Recently Arun Ranganathan, one of the members of the Mozilla Evangelism team, created a video for MozCamp Mumbai. It&#8217;s about 20 minutes long and he covers a huge number of topics: the new @font-face CSS property and how it affects the ability for people to receive properly localized content, the differences between the various standards [...]]]></description>
			<content:encoded><![CDATA[<p><em>Recently <a href="http://twitter.com/arun">Arun Ranganathan</a>, one of the members of the Mozilla Evangelism team, created a video for <a href="http://mozcamp.in/mumbai/wiki/index.php/Main_Page">MozCamp Mumbai</a>.  It&#8217;s about 20 minutes long and he covers a huge number of topics: the new @font-face CSS property and how it affects the ability for people to receive properly localized content, the differences between the various standards efforts (there&#8217;s more than just HTML5) and gives some demos that show what&#8217;s possible when you combine video with the web.</em></p>
<div align="center"><video controls><source type="video/ogg" src="http://videos.mozilla.org/serv/blizzard/MozCamp-Mumbai/Arun-320x240.ogv" /><source type="video/mp4" src="http://videos.mozilla.org/serv/blizzard/MozCamp-Mumbai/Arun-320x240.mp4" />Don&#8217;t support HTML5 video?  Try downloading it below or upgrade to <a href="http://www.getfirefox.com">Firefox 3.5</a>.</video><br />
Download: 640&#215;480 &#8211; <a href="http://videos.mozilla.org/serv/blizzard/MozCamp-Mumbai/Arun-640x480.ogv">Ogg Theora</a> or <a href="http://videos.mozilla.org/serv/blizzard/MozCamp-Mumbai/Arun-640x480.m4v">MP4</a> | 320&#215;240 &#8211; <a href="http://videos.mozilla.org/serv/blizzard/MozCamp-Mumbai/Arun-320x240.ogv">Ogg Theora</a> or <a href="http://videos.mozilla.org/serv/blizzard/MozCamp-Mumbai/Arun-320x240.mp4">MP4</a>
</div>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2009/07/arun-standards-india/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
<enclosure url="http://videos.mozilla.org/serv/blizzard/MozCamp-Mumbai/Arun-320x240.ogv" length="45838519" type="video/ogg" />
<enclosure url="http://videos.mozilla.org/serv/blizzard/MozCamp-Mumbai/Arun-640x480.ogv" length="289779850" type="video/ogg" />
		</item>
		<item>
		<title>HTML5 drag and drop in Firefox 3.5</title>
		<link>http://hacks.mozilla.org/2009/07/html5-drag-and-drop/</link>
		<comments>http://hacks.mozilla.org/2009/07/html5-drag-and-drop/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 01:57:32 +0000</pubDate>
		<dc:creator>Christopher Blizzard</dc:creator>
				<category><![CDATA[35 Days]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Feature]]></category>
		<category><![CDATA[Firefox 3.5]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=1420</guid>
		<description><![CDATA[This post is from Les Orchard, who works on Mozilla’s web development team. Introduction Drag and drop is one of the most fundamental interactions afforded by graphical user interfaces. In one gesture, it allows users to pair the selection of an object with the execution of an action, often including a second object in the [...]]]></description>
			<content:encoded><![CDATA[<p><em>This post is from <a href="http://decafbad.com/blog/lifestream">Les Orchard</a>, who works on Mozilla’s web development team.</em></p>
<p><b>Introduction</b></p>
<p>Drag and drop is one of the most fundamental interactions afforded by graphical user interfaces.  In one gesture, it allows users to pair the selection of an object with the execution of an action, often including a second object in the operation.  It&#8217;s a simple yet powerful UI concept used to support copying, list reordering, deletion (ala the Trash / Recycle Bin), and even the creation of link relationships.</p>
<p>Since it&#8217;s so fundamental, offering drag and drop in web applications has been a no-brainer ever since browsers first offered mouse events in DHTML.  But, although <code>mousedown</code>, <code>mousemove</code>, and <code>mouseup</code> made it possible, the implementation has been limited to the bounds of the browser window.  Additionally, since these events refer only to the object being dragged, there&#8217;s a challenge to find the subject of the drop when the interaction is completed.</p>
<p>Of course, that doesn&#8217;t prevent most modern JavaScript frameworks from abstracting away most of the problems and throwing in some flourishes while they&#8217;re at it.  But, wouldn&#8217;t it be nice if browsers offered first-class support for drag and drop, and maybe even extended it beyond the window sandbox?
</p>
<p>
As it turns out, this very wish is answered by the HTML 5 specification <a target="_new" href="http://www.whatwg.org/specs/web-apps/current-work/multipage/editing.html#dnd">section on new drag-and-drop events</a>, and  <a target="_new" href="https://developer.mozilla.org/En/DragDrop/Drag_and_Drop">Firefox 3.5 includes an implementation</a> of those events.</p>
<p>If you want to jump straight to the code, I&#8217;ve put together <a target="_new" target="_new" target="_new" href="http://decafbad.com/2009/07/drag-and-drop/api-demos.html">some simple demos</a> of the new events.  </p>
<p>I&#8217;ve even scratched an itch of my own and built <a target="_new" target="_new" target="_new" href="http://decafbad.com/2009/07/drag-and-drop/outline.html">the beginnings of an outline editor</a>, where every draggable element is also a drop target—of which there could be dozens to hundreds in a complex document, something that gave me some minor hair-tearing moments in the past while trying to make do with plain old mouse events.</p>
<p>And, all the above can be downloaded or cloned from <a href="http://github.com/lmorchard/fx35-drag-and-drop">a GitHub repository</a> I&#8217;ve created expecially for this article.</p>
<p><b>The New Drag and Drop Events</b></p>
<p>So, with no further ado, here are the new drag and drop events, in roughly the order you might expect to see them fired:</p>
<dl>
<dt><code>dragstart</code></dt>
<dd>
A drag has been initiated, with the dragged element as the event target.
</dd>
<dt><code>drag</code></dt>
<dd>
The mouse has moved, with the dragged element as the event target.
</dd>
<dt><code>dragenter</code></dt>
<dd>
The dragged element has been moved into a drop listener, with the drop listener element as the event target.
</dd>
<dt><code>dragover</code></dt>
<dd>
The dragged element has been moved over a drop listener, with the drop listener element as the event target.  Since the default behavior is to cancel drops, returning <code>false</code> or calling <code>preventDefault()</code> in the event handler indicates that a drop is allowed here.
</dd>
<dt><code>dragleave</code></dt>
<dd>
The dragged element has been moved out of a drop listener, with the drop listener element as the event target.
</dd>
<dt><code>drop</code></dt>
<dd>
The dragged element has been successfully dropped on a drop listener, with the drop listener element as the event target.
</dd>
<dt><code>dragend</code></dt>
<dd>
A drag has been ended, successfully or not, with the dragged element as the event target.
</dd>
</dl>
<p>Like the mouse events of yore, listeners can be attached to elements using <code>addEventListener()</code> directly or by way of your favorite JS library.  </p>
<p>Consider the following example using jQuery, <a target="_new" target="_new" target="_new" href="http://decafbad.com/2009/07/drag-and-drop/api-demos.html#newschool">also available as a live demo</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    &lt;div id=&quot;newschool&quot;&gt;
        &lt;div class=&quot;dragme&quot;&gt;Drag me!&lt;/div&gt;
        &lt;div class=&quot;drophere&quot;&gt;Drop here!&lt;/div&gt;
    &lt;/div&gt;
&nbsp;
    <span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
        $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#newschool .dragme'</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'draggable'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'true'</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dragstart'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>ev<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #003366; font-weight: bold;">var</span> dt <span style="color: #339933;">=</span> ev.<span style="color: #660066;">originalEvent</span>.<span style="color: #660066;">dataTransfer</span><span style="color: #339933;">;</span>
                    dt.<span style="color: #660066;">setData</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Text&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Dropped in zone!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dragend'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>ev<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#newschool .drophere'</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dragenter'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>ev<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    $<span style="color: #009900;">&#40;</span>ev.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dragover'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dragleave'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>ev<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    $<span style="color: #009900;">&#40;</span>ev.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dragover'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dragover'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>ev<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'drop'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>ev<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #003366; font-weight: bold;">var</span> dt <span style="color: #339933;">=</span> ev.<span style="color: #660066;">originalEvent</span>.<span style="color: #660066;">dataTransfer</span><span style="color: #339933;">;</span>
                    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>dt.<span style="color: #660066;">getData</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Text'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Thanks to the new events and jQuery, this example is both short and simple—but it packs in a lot of functionality, as the rest of this article will explain.  </p>
<p>Before moving on, there are at least three things about the above code that are worth mentioning:</p>
<ul>
<li>
Drop targets are enabled by virtue of having listeners for drop events.  But, <a target="_new" href="http://www.whatwg.org/specs/web-apps/current-work/multipage/editing.html#drag-and-drop-processing-model">per the HTML 5 spec</a>, draggable elements need an attribute of <code>draggable="true"</code>, set either in markup or in JavaScript.  </p>
<p>Thus, <code>$('#newschool&nbsp;.dragme').attr('draggable', 'true')</code>.
</li>
<li>
The original DOM event (as opposed to jQuery&#8217;s event wrapper) offers a property called <code>dataTransfer</code>. Beyond just manipulating elements, the new drag and drop events accomodate the transmission of user-definable data during the course of the interaction.
</li>
<li>
Since these are first-class events, you can apply <a target="_new" href="http://icant.co.uk/sandbox/eventdelegation/">the technique of Event Delegation</a>.</p>
<p>What&#8217;s that?  Well, imagine you have a list of 1000 list items—as part of a deeply-nested outline document, for instance.  Rather than needing to attach listeners or otherwise fiddle with all 1000 items, simply attach a listener to the parent node (eg. the <code>&lt;ul&gt;</code> element) and all events from the children will propagate up to the single parent listener. As a bonus, all new child elements added after page load will enjoy the same benefits.</p>
<p><a target="_new" target="_new" target="_new" href="http://decafbad.com/2009/07/drag-and-drop/api-demos.html#delegated">Check out this demo</a>, and <a target="_new" target="_new" href="http://decafbad.com/2009/07/drag-and-drop/js/drag-delegated.js">the associated JS code</a> to see more about these events and Event Delegation.
</li>
</ul>
<p><b>Using dataTransfer</b></p>
<p>As mentioned in the last section, the new drag and drop events let you send data along with a dragged element.  But, it&#8217;s even better than that:  Your drop targets can receive data transferred by content objects dragged into the window from other browser windows, and even <i>other applications</i>.</p>
<p>Since the example is a bit longer,  <a target="_new" target="_new" href="http://decafbad.com/2009/07/drag-and-drop/api-demos.html#data_transfer">check out the live demo</a> and <a target="_new" href="http://decafbad.com/2009/07/drag-and-drop/js/drag-datatransfer.js">associated code</a> to get an idea of what&#8217;s possible with <code>dataTransfer</code>.</p>
<p>In a nutshell, the stars of this show are the <code>setData()</code> and <code>getData()</code> methods of the <code>dataTransfer</code> property exposed by the Event object.</p>
<p>The <code>setData()</code> method is typically called in the <code>dragstart</code> listener, loading <code>dataTransfer</code> up with one or more strings of content with associated <a href="https://developer.mozilla.org/En/DragDrop/Recommended_Drag_Types#link">recommended content types</a>.</p>
<p>For illustration, here&#8217;s a quick snippet from the example code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #003366; font-weight: bold;">var</span> dt <span style="color: #339933;">=</span> ev.<span style="color: #660066;">originalEvent</span>.<span style="color: #660066;">dataTransfer</span><span style="color: #339933;">;</span>    
    dt.<span style="color: #660066;">setData</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'text/plain'</span><span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#logo'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    dt.<span style="color: #660066;">setData</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'text/html'</span><span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#logo'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    dt.<span style="color: #660066;">setData</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'text/uri-list'</span><span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#logo'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">src</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>On the other end, <code>getData()</code> allows you to query for content by type (eg. <code>text/html</code> followed by <code>text/plain</code>).  This, in turn, allows you to decide on acceptable content types at the time of the <code>drop</code> event or even during <code>dragover</code> to offer feedback for unacceptable types during the drag.</p>
<p>Here&#8217;s another example from the receiving end of the example code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #003366; font-weight: bold;">var</span> dt <span style="color: #339933;">=</span> ev.<span style="color: #660066;">originalEvent</span>.<span style="color: #660066;">dataTransfer</span><span style="color: #339933;">;</span>    
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.content_url .content'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>dt.<span style="color: #660066;">getData</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'text/uri-list'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.content_text .content'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>dt.<span style="color: #660066;">getData</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'text/plain'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.content_html .content'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>dt.<span style="color: #660066;">getData</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'text/html'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Where <code>dataTransfer</code> really shines, though, is that it allows your drop targets to receive content from sources outside your defined draggable elements and even from outside the browser altogether.  Firefox accepts such drags, and attempts to populate <code>dataTransfer</code> with appropriate content types extracted from the external object.</p>
<p>Thus, you could select some text in a word processor window and drop it into one of your elements, and at least expect to find it available as <code>text/plain</code> content.  </p>
<p>You can also select content in another browser window, and expect to see <code>text/html</code> appear in your events.  Check out the <a target="_new" href="http://decafbad.com/2009/07/drag-and-drop/outline.html">outline editing demo</a> and see what happens when you try dragging various elements (eg. images, tables, and lists) and highlighted content from other windows onto the items there.</p>
<p><b>Using Drag Feedback Images</b></p>
<p>An important aspect of the drag and drop interaction is a representation of the thing being dragged.  By default in Firefox, this is a &#8220;ghost&#8221; image of the dragged element itself.  But, the <code>dataTransfer</code> property of the original Event object exposes the method <code>setDragImage()</code> for use in customizing this representation.</p>
<p>There&#8217;s <a target="_new" href="http://decafbad.com/2009/07/drag-and-drop/api-demos.html#feedback_image">a live demo</a> of this feature, as well as <a target="_new" href="http://decafbad.com/2009/07/drag-and-drop/js/drag-feedback-images.js">associated JS code</a> available.  The gist, however, is sketched out in these code snippets:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #003366; font-weight: bold;">var</span> dt <span style="color: #339933;">=</span> ev.<span style="color: #660066;">originalEvent</span>.<span style="color: #660066;">dataTransfer</span><span style="color: #339933;">;</span>    
&nbsp;
    dt.<span style="color: #660066;">setDragImage</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#feedback_image h2'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    dt.<span style="color: #660066;">setDragImage</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#logo'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">32</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">32</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #003366; font-weight: bold;">var</span> canvas <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;canvas&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    canvas.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> canvas.<span style="color: #660066;">height</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">var</span> ctx <span style="color: #339933;">=</span> canvas.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;2d&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ctx.<span style="color: #660066;">lineWidth</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">8</span><span style="color: #339933;">;</span>
    ctx.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">25</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ctx.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">50</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ctx.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ctx.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">25</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ctx.<span style="color: #660066;">stroke</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    dt.<span style="color: #660066;">setDragImage</span><span style="color: #009900;">&#40;</span>canvas<span style="color: #339933;">,</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">25</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You can supply a DOM node as the first parameter to <code>setDragImage()</code>, which includes everything from text to images to <code>&lt;canvas&gt;</code> elements.  The second two parameters indicate at what left and top offset the mouse should appear in the image while dragging.</p>
<p>For example, since the <code>#logo</code> image is 64&#215;64, the parameters in the second <code>setDragImage()</code> method places the mouse right in the center of the image. On the other hand, the first call positions the feedback image such that the mouse rests in the upper left corner.</p>
<p><b>Using Drop Effects</b></p>
<p>As mentioned at the start of this article, the drag and drop interaction has been used to support actions such as copying, moving, and linking.  Accordingly, the HTML 5 specification accomodates these operations in the form of the <code>effectAllowed</code> and <code>dropEffect</code> properties exposed by the Event object.</p>
<p>For a quick fix, check out the <a target="_new" href="http://decafbad.com/2009/07/drag-and-drop/api-demos.html#drag_effects">a live demo</a> of this feature, as well as the <a target="_new" href="http://decafbad.com/2009/07/drag-and-drop/js/drag-effects.js">associated JS code</a>.</p>
<p>The basic idea is that the <code>dragstart</code> event listener can set a value for <code>effectAllowed</code> like so:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #003366; font-weight: bold;">var</span> dt <span style="color: #339933;">=</span> ev.<span style="color: #660066;">originalEvent</span>.<span style="color: #660066;">dataTransfer</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>ev.<span style="color: #660066;">target</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'effectdrag0'</span><span style="color: #339933;">:</span> dt.<span style="color: #660066;">effectAllowed</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'copy'</span><span style="color: #339933;">;</span> <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'effectdrag1'</span><span style="color: #339933;">:</span> dt.<span style="color: #660066;">effectAllowed</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'move'</span><span style="color: #339933;">;</span> <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'effectdrag2'</span><span style="color: #339933;">:</span> dt.<span style="color: #660066;">effectAllowed</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'link'</span><span style="color: #339933;">;</span> <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'effectdrag3'</span><span style="color: #339933;">:</span> dt.<span style="color: #660066;">effectAllowed</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'all'</span><span style="color: #339933;">;</span> <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'effectdrag4'</span><span style="color: #339933;">:</span> dt.<span style="color: #660066;">effectAllowed</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">;</span> <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The choices available for this property include the following:</p>
<dl>
<dt><code>none</code></dt>
<dd>no operation is permitted </dd>
<dt><code>copy</code></dt>
<dd>copy only </dd>
<dt><code>move</code></dt>
<dd>move only </dd>
<dt><code>link</code></dt>
<dd>link only </dd>
<dt><code>copyMove</code></dt>
<dd>copy or move only </dd>
<dt><code>copyLink</code></dt>
<dd>copy or link only </dd>
<dt><code>linkMove</code></dt>
<dd>link or move only </dd>
<dt><code>all</code></dt>
<dd>copy, move, or link </dd>
</dl>
<p>On the other end, the <code>dragover</code> event listener can set the value of the <code>dropEffect</code> property to indicate the expected effect invoked on a successful drop.  If the value does not match up with <code>effectAllowed</code>, the drop will be considered cancelled on completion.</p>
<p>In the <a target="_new" href="http://decafbad.com/2009/07/drag-and-drop/api-demos.html#drag_effects">a live demo</a>, you should be able to see that only elements with matching effects can be dropped into the appropriate drop zones.  This is accomplished with code like the follwoing:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #003366; font-weight: bold;">var</span> dt <span style="color: #339933;">=</span> ev.<span style="color: #660066;">originalEvent</span>.<span style="color: #660066;">dataTransfer</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>ev.<span style="color: #660066;">target</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'effectdrop0'</span><span style="color: #339933;">:</span> dt.<span style="color: #660066;">dropEffect</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'copy'</span><span style="color: #339933;">;</span> <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'effectdrop1'</span><span style="color: #339933;">:</span> dt.<span style="color: #660066;">dropEffect</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'move'</span><span style="color: #339933;">;</span> <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'effectdrop2'</span><span style="color: #339933;">:</span> dt.<span style="color: #660066;">dropEffect</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'link'</span><span style="color: #339933;">;</span> <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'effectdrop3'</span><span style="color: #339933;">:</span> dt.<span style="color: #660066;">dropEffect</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'all'</span><span style="color: #339933;">;</span> <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'effectdrop4'</span><span style="color: #339933;">:</span> dt.<span style="color: #660066;">dropEffect</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">;</span> <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Although the OS itself can provide some feedback, you can also use these properties to update your own visible feedback, both on the dragged element and on the drop zone itself.</p>
<p><b>Conclusion</b></p>
<p>The new first-class drag and drop events in HTML5 and Firefox make supporting this form of UI interaction simple, concise, and powerful in the browser.  But beyond the new simplicity of these events, the ability to transfer content between applications opens brand new avenues for web-based applications and collaboration with desktop software in general.</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2009/07/html5-drag-and-drop/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
		<item>
		<title>css transforms: styling the web in two dimensions</title>
		<link>http://hacks.mozilla.org/2009/07/css-transforms/</link>
		<comments>http://hacks.mozilla.org/2009/07/css-transforms/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 17:51:12 +0000</pubDate>
		<dc:creator>Eric Shepherd</dc:creator>
				<category><![CDATA[35 Days]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Demo]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Feature]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Standards]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=1365</guid>
		<description><![CDATA[One feature that Firefox 3.5 adds to its CSS implementation is transform functions. These let you manipulate elements in two dimensional space by rotating, skewing, scaling, and translating them to alter their appearance. I&#8217;ve put together a demo that shows how some of these functions work. There are four animating objects in this demo. Let&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>One feature that Firefox 3.5 adds to its CSS implementation is <a href="https://developer.mozilla.org/En/CSS/CSS_transform_functions">transform functions</a>. These let you manipulate elements in two dimensional space by rotating, skewing, scaling, and translating them to alter their appearance.</p>
<p>I&#8217;ve <a href="http://www.bitstampede.com/demos/css-transforms/">put together a demo</a> that shows how some of these functions work.</p>
<p>There are four animating objects in this demo. Let&#8217;s take a look at each of them.</p>
<p><strong>Rotating the Firefox logo</strong></p>
<p>On the left, we see the Firefox logo in a nice box, happily spinning in place. This is done by periodically setting the rotation value of the image object, whose ID is <tt>logoimg</tt>, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">var</span> logo <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;logoimg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  logoAngle <span style="color: #339933;">=</span> logoAngle <span style="color: #339933;">+</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>logoAngle <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">360</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    logoAngle <span style="color: #339933;">=</span> logoAngle <span style="color: #339933;">-</span> <span style="color: #CC0000;">360</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> style <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;-moz-transform: rotate(&quot;</span> <span style="color: #339933;">+</span> logoAngle <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;deg)&quot;</span><span style="color: #339933;">;</span>
  logo.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;style&quot;</span><span style="color: #339933;">,</span> style<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Every time the animation function is run, we rotate it by 2° around its origin by constructing a style string of the form <tt>-moz-transform: rotate(<emphasis>N</emphasis>deg)</tt>.</p>
<p>By default, all elements&#8217; origins are at their centers (that is, 50% along each axis). The origin can be changed using the <tt><a href="https://developer.mozilla.org/En/CSS/-moz-transform-origin">-moz-transform-origin</a></tt> attribute.</p>
<p><strong>Skewing text</strong></p>
<p>We have two examples of skewing in this demo; the first skews horizontally, which causes the text to &#8220;lean&#8221; back and forth along the X axis. The second skews vertically, which causes the baseline to pivot along the Y axis.</p>
<p>In both cases, the code to accomplish this animation is essentially identical, so let&#8217;s just look at the code for skewing horizontally:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  text1SkewAngle <span style="color: #339933;">=</span> text1SkewAngle <span style="color: #339933;">+</span> text1SkewOffset<span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>text1SkewAngle <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">45</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    text1SkewAngle <span style="color: #339933;">=</span> <span style="color: #CC0000;">45</span><span style="color: #339933;">;</span>
    text1SkewOffset <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>text1SkewAngle <span style="color: #339933;">&lt;</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">45</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    text1SkewAngle <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">45</span><span style="color: #339933;">;</span>
    text1SkewOffset <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  text1.<span style="color: #660066;">style</span>.<span style="color: #660066;">MozTransform</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;skewx(&quot;</span> <span style="color: #339933;">+</span> text1SkewAngle <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;deg)&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This code updates the current amount by which the text is skewed, starting at zero degrees and moving back and forth between -45° and 45° at a rate of 2° each time the animation function is called. Positive values skew the element to the right and negative values to the left.</p>
<p>Then the element&#8217;s transform style is updated, setting the transform function to be of the form <tt>skewx(<emphasis>N</emphasis>deg)</tt>, then setting the element&#8217;s <tt><a href="https://developer.mozilla.org/en/DOM/element.style">style</a>.MozTransform</tt> property to that value.</p>
<p><strong>Scaling elements</strong></p>
<p>The last of the examples included in the demo shows how to scale an element using the <tt>scale</tt> transform function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  text3Scale <span style="color: #339933;">=</span> text3Scale <span style="color: #339933;">+</span> text3ScaleOffset<span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>text3Scale <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">6</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    text3Scale <span style="color: #339933;">=</span> <span style="color: #CC0000;">6</span><span style="color: #339933;">;</span>
    text3ScaleOffset <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">0.1</span><span style="color: #339933;">;</span>
    text3.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;It's going away so fast!&quot;</span>
    text3.<span style="color: #660066;">style</span>.<span style="color: #660066;">color</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;blue&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>text3Scale <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    text3Scale <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
    text3ScaleOffset <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.1</span><span style="color: #339933;">;</span>
    text3.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;It's coming right at us!&quot;</span><span style="color: #339933;">;</span>
    text3.<span style="color: #660066;">style</span>.<span style="color: #660066;">color</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;red&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  text3.<span style="color: #660066;">style</span>.<span style="color: #660066;">MozTransform</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;scale(&quot;</span> <span style="color: #339933;">+</span> text3Scale <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;)&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This code scales the element up and down between its original size (a scale factor of 1) and a scale factor of 6, moving by 0.1 units each frame. This is done by building a transform of the form <tt>scale(<emphasis>N</emphasis>)</tt>, then setting the element&#8217;s <tt><a href="https://developer.mozilla.org/en/DOM/element.style">style</a>.MozTransform</tt> property to that value.</p>
<p>In addition, just for fun, we&#8217;re also changing the text and the color of the text in the block as we switch scaling directions, by setting the value of the block&#8217;s <tt>innerHTML</tt> property to the new contents.</p>
<p><strong>Final notes</strong></p>
<p>Three more tidbits to take away from this:</p>
<p>First, note that as the scaling text grows wider, the document&#8217;s width changes to fit it, getting wider as the text grows so that its right edge passes the edge of the document, then narrower as it shrinks again. You can see this by watching the scroll bar at the bottom of the Firefox browser window.</p>
<p>Second, note that you can actually select and copy the text not only while the elements are transformed, but the selection remains intact while the text continues to transform (although when we change the contents of the scaling example, the selection goes away).</p>
<p>Third, I didn&#8217;t cover all the possible transforms here. For example, I skipped over the <tt>translate</tt> transform function, which lets you translate an object horizontally or vertically (basically, shifting its position by an offset). You can get a <a href="https://developer.mozilla.org/En/CSS/CSS_transform_functions">full list of the supported transforms</a> on the <a href="https://developer.mozilla.org/">Mozilla Developer Center</a> web site.</p>
<p>Obviously this demo is somewhat frivolous (as demos are prone to be). However, there are genuinely useful things you can do with these when designing interfaces; for example, you can draw text rotated by 90° along the Y axis of a table in order to fit row labels in a narrow but tall space.</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2009/07/css-transforms/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>using web workers: working smarter, not harder</title>
		<link>http://hacks.mozilla.org/2009/07/working-smarter-not-harder/</link>
		<comments>http://hacks.mozilla.org/2009/07/working-smarter-not-harder/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 00:42:54 +0000</pubDate>
		<dc:creator>Eric Shepherd</dc:creator>
				<category><![CDATA[35 Days]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Feature]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Standards]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=1344</guid>
		<description><![CDATA[This article is written by Malte Ubl, who has done a lot of great work with using Web Workers as part of the bespin project. In recent years, the user experience of web applications has grown richer and richer. In-browser applications like GMail, Meebo and Bespin give us an impression of how the web will [...]]]></description>
			<content:encoded><![CDATA[<p><em>This article is written by <a href="http://www.nonblocking.io/">Malte Ubl</a>, who has done a lot of great work with <a href="http://www.nonblocking.io/search/label/WebWorkers">using Web Workers</a> as part of the <a href="https://bespin.mozilla.com/">bespin</a> project.<br />
</em><br />
In recent years, the user experience of web applications has grown richer and richer. In-browser applications like <a href="http://mail.google.com/">GMail</a>, <a href="http://www.meebo.com/">Meebo</a> and <a href="https://bespin.mozilla.com/">Bespin</a> give us an impression of how the web will look and feel in the future. One of the key aspects of creating a great user experience is to build applications that are highly responsive. Users hate to wait and they hate those moments where an application seems to work for a while, then stops responding to their input.</p>
<p>At the core of modern client-side web applications lies the JavaScript programming language. JavaScript and the DOM that it talks to is inherently single-threaded. This means that in JavaScript only one thing can happen at any given time. Even if your computer has 32 cores it will keep only one of those cores busy when it&#8217;s doing a long computation.&nbsp; For example if you calculate the perfect trajectory to get to the moon it won&#8217;t be able to render an animation that shows the trajectory at the same time and it won&#8217;t be able to react to any user events like clicks or typing on the keyboard while it&#8217;s doing that calculation.</p>
<p><strong>Concurrency</strong></p>
<p>To maintain responsiveness while performing intense computations <i>concurrency</i> is a part of most modern programming languages. In the past concurrency was often achieved by the use of <i><a href="http://en.wikipedia.org/wiki/Thread_%28computer_science%29">threads</a></i>. Threads, however, make it increasingly hard for the programmer to understand the program flow which often leads to very hard to understand bugs and chaotic behavior when different threads manipulate the same data simultaneously.</p>
<p>Web Workers, which were <a href="http://www.whatwg.org/specs/web-workers/current-work/">recommended</a> by the <a href="http://www.whatwg.org/">WHATWG</a>, were introduced in Firefox 3.5 to add concurrency to JavaScript applications without also introducing the problems associated with multithreaded programs. Starting a worker is easy &#8211; just use the new Worker interface.</p>
<p>In this example the worker.js file will be loaded and the a new thread will be created to execute that code.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Start worker from file &quot;worker.js&quot;</span>
<span style="color: #003366; font-weight: bold;">var</span> worker <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Worker<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;worker.js&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Communication between the main UI thread and workers is done by passing messages using the postMessage method. postMessage was added for cross-window communication in Firefox 3. To send a message from the worker back to the page, you just post a message:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Send a message back to the main UI thread</span>
postMessage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Hello Page!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To catch the message from the worker, you define an &#8220;onmessage&#8221; callback on the worker object. Here we just alert the event data that is passed to the callback function. In this case, &#8220;event.data&#8221; contains the &#8220;Hello Page!&#8221; string that was sent above.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">worker.<span style="color: #660066;">onmessage</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">// Send a message to the worker</span>
  worker.<span style="color: #660066;">postMessage</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Hello Worker&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>To send a message to the worker we call the postMessage method on the worker object. To receive these messages inside the worker, simply define an onmessage function that will be called every time a message is posted to the worker.</p>
<p><strong>Error Handling</strong></p>
<p>There are two levels at which you can recover from runtime errors that occur in a worker. First, you can define an onerror function within the worker. Second, you can handle errors from the outside the worker by attaching an onerror handler on to the worker object:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">worker.<span style="color: #000066;">onerror</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  event.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The event.preventDefault() method prevents the default action, which would be to display the error to the user or at least show it in the error console. Here we alert the error message instead.</p>
<p><strong>Shared Nothing</strong></p>
<p>Workers share absolutely no state with the page they are associated with or with any other workers; the only way they can interact at all is through postMessage. Workers also have no access to the DOM, so they can not directly manipulate the web page. There is thus no risk of problems with data integrity when multiple workers want to manipulate the same data at once.</p>
<p>A standard setup that is using workers would consist of a page JavaScript component that is listening for user events. When an event occurs that triggers an intensive calculation a message is sent to the worker which then starts the computation. The script on the page, however, can terminate immediately and listen for more user events. As soon as the worker is done, it sends a return message to the page which can then, for example, display the result.</p>
<div style="margin-left:30px; margin-right:30px">
<img src="http://hacks.mozilla.org/wp-content/uploads/2009/07/never-again.jpg" width="370" height="78" class="alignnone size-full wp-image-1346" /><br />
The unresponsive script warning that is being displayed by browsers when a script is taking a long time to execute is a thing of the past when using web workers.
</div>
<p><strong>The Fibonacci Example</strong></p>
<p>Next is an example of a worker that calculates the Fibonacci numbers from 0 to 99 in the background. Actually, because calculating Fibonacci numbers using this very inefficient method can take a lot of time for larger numbers (as in greater than something like 30) the script might never finish on your computer (or crash because it blows out the stack), but when doing it in a worker this has no effect on the responsiveness of the main web page. So you can still draw a complex animation to make the waiting time for the next number a little more fun.</p>
<p>This HTML page contains a script that starts a worker from the file &#8220;fib-worker.js&#8221;. Messages from the worker are displayed on the browser&#8217;s console using console.log.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Web Worker API Demo<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
        var worker = new Worker(&quot;fib-worker.js&quot;);
        worker.onmessage = function (event) {
          console.log(event.data.index +&quot; -&gt; &quot; + event.data.value)
        }
      <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>  
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>The JavaScript file that implements the worker contains a loop that calculates Fibonacci numbers and sends the result to the page.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// File fib-worker.js</span>
<span style="color: #003366; font-weight: bold;">function</span> fib<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">return</span> n <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">2</span> <span style="color: #339933;">?</span> n <span style="color: #339933;">:</span> fib<span style="color: #009900;">&#40;</span>n<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> fib<span style="color: #009900;">&#40;</span>n<span style="color: #339933;">-</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   postMessage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
      index<span style="color: #339933;">:</span> i<span style="color: #339933;">,</span>
      value<span style="color: #339933;">:</span> fib<span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In the example above we see that we can also pass complex objects to the postMessage. These objects can contain everything that can be transmitted via <a href="http://json.org/">JSON</a>. This means that functions cannot be passed across worker boundaries and that the objects are passed by value rather than by reference.</p>
<p><strong>Worker APIs</strong></p>
<p>Workers support a function called importScripts. You can use this to load more source files into the worker.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">importScripts<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;file.js&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
importScripts<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;foo.js&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;bar.js&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>When you pass multiple parameters to the function the scripts will be downloaded in parallel but executed in the order of definition. The function does not return until all scripts have been downloaded and executed.</p>
<p>Here we load an external JavaScript file that calculates <a href="http://en.wikipedia.org/wiki/SHA-1">SHA-1</a> hash sums from strings and then we use it to hash responses from AJAX requests. We also use the standard <a href="https://developer.mozilla.org/en/XmlHttpRequest">XMLHttpRequest</a> object to retrieve the content of the URL which is passed in via the onmessage event. The interesting part is that we don&#8217;t have to worry about making the AJAX request asynchronous because the worker itself is asynchronous with respect to page rendering, so a little waiting for the HTTP request does not hurt as much.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">importScripts<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;sha1.js&quot;</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> onmessage<span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> xhr <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    xhr.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'GET'</span><span style="color: #339933;">,</span> event.<span style="color: #660066;">data</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    xhr.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    postMessage<span style="color: #009900;">&#40;</span>sha1<span style="color: #009900;">&#40;</span>xhr.<span style="color: #660066;">responseText</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Other APIs Available to Workers</strong></p>
<p>Workers may use XMLHttpRequest for AJAX requests as seen above and access the client sided database using <a href="https://developer.mozilla.org/en/Storage">web storage API</a>. Here the APIs are identical to their usage in regular JavaScript.</p>
<p>The setTimeout and setInterval (and the clearTimeout and clearInterval friends) functions, which enable executing code after a given period of time or at certain intervals, are also available within the worker as is the well known <a href="https://developer.mozilla.org/en/DOM/window.navigator">navigator object</a>, which can be inspected to get information about the current browser.</p>
<p>More APIs may be added in the future.</p>
<p><strong>Browser Compatibility</strong></p>
<p>As of this writing (and to the knowledge of the author), Firefox 3.5 is the only browser that supports the ability to pass complex objects via postMessage and that implements the extended APIs defined above. Safari 4 implements a very basic version of the Worker API. For other browsers it is possible to use Workers via <a href="http://gears.google.com/">Google Gears</a>, which originally introduced the concept to browsers.</p>
<p><strong>Real World Usage</strong></p>
<p>In the <a href="https://bespin.mozilla.com/">Bespin project</a>, which is a browser based source code editor, we successfully used workers to implement CPU intensive features like real-time source code error checking and code completion. We also created <a href="http://www.nonblocking.io/2009/03/offloading-arbitrary-js-objects-to.html">a shim</a> that implements the Worker API in terms of Google Gears and which adds the missing features to the worker implementation of Safari 4 and also <a href="http://www.nonblocking.io/2009/05/one-event-loop-to-rule-them-all.html">moved to using transparent custom events</a> on top of the postMessage interface. These components will be released as a stand-alone library to be usable in other projects in the future.</p>
<p>Web Workers will play an important role in making the Open Web an even more powerful platform for sophisticated applications. Because in the end all they do is execute JavaScript, it&#8217;s easy to make scripts work on clients which do yet have the luxury of web workers. So go ahead and add them to your applications today to make them feel just a little more responsive and more pleasant to use.</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2009/07/working-smarter-not-harder/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>exploring music with the audio tag</title>
		<link>http://hacks.mozilla.org/2009/06/exploring-music-audio/</link>
		<comments>http://hacks.mozilla.org/2009/06/exploring-music-audio/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 00:28:49 +0000</pubDate>
		<dc:creator>Christopher Blizzard</dc:creator>
				<category><![CDATA[35 Days]]></category>
		<category><![CDATA[Audio]]></category>
		<category><![CDATA[Demo]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Firefox 3.5]]></category>
		<category><![CDATA[SVG]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=1066</guid>
		<description><![CDATA[Today&#8217;s demo comes to us from Samuel Goldszmidt. He&#8217;s a web developer specializing in audio applications at Institut de Recherche et Coordination Acoustique/Musique (IRCAM). IRCAM is a European institute covering science, sound and avant garde electro-acoustical art music. The demo uses XML to describe the various segments of a piece of music &#8211; Florence Baschet&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p><em>Today&#8217;s <a href="http://apm.ircam.fr/page/audio-tag/">demo</a> comes to us from Samuel Goldszmidt.  He&#8217;s a web developer specializing in audio applications at Institut de Recherche et Coordination Acoustique/Musique (<a href="http://www.ircam.fr/ircam.html?&#038;L=1">IRCAM</a>).  IRCAM is a European institute covering science, sound and avant garde electro-acoustical art music.</em></p>
<p>The demo uses XML to describe the various segments of a piece of music &#8211; Florence Baschet&#8217;s <em>StreicherKreis (Circle of Strings)</em>.  The music itself is a combination of stringed instruments and electronic effects.  From the XML, SVG is generated for each section of the music.  You can click on each section to listen to that part of the piece and a description is shown on how that particular section was created.</p>
<p>As far as demos go, this is relatively simple.  But it&#8217;s worth highlighting because it shows how easy it is to build a timeline around a piece of music and add descriptive information.  In this case, it&#8217;s information meant to teach people how a particular effect was created.  But it could be anything, from showing different camera angles of people playing the music to links about different covers of a popular piece.  Opening up media to the web means that we can combine it with text, images and other media.  This is just a small example.</p>
<div align="center" style="font-size: 120%"><a target="_blank" href="http://apm.ircam.fr/page/audio-tag/">View the Demo in Firefox 3.5<br/><img src="http://hacks.mozilla.org/wp-content/uploads/2009/06/ircam.png"/></a></div>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2009/06/exploring-music-audio/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>the script defer attribute</title>
		<link>http://hacks.mozilla.org/2009/06/defer/</link>
		<comments>http://hacks.mozilla.org/2009/06/defer/#comments</comments>
		<pubDate>Sat, 27 Jun 2009 04:00:34 +0000</pubDate>
		<dc:creator>Christopher Blizzard</dc:creator>
				<category><![CDATA[35 Days]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Feature]]></category>
		<category><![CDATA[Firefox 3.5]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=953</guid>
		<description><![CDATA[This post is by Olivier Rochard. Olivier does research at Orange Labs in France. In HTML, the script element allows authors to include dynamic script in their documents. The defer attribute is boolean attribute that indicates how the script should be executed. If the defer attribute is present, then the script is executed when the [...]]]></description>
			<content:encoded><![CDATA[<p><em>This post is by Olivier Rochard.  Olivier does research at Orange Labs in France.</em></p>
<p>In HTML, the <code>script</code> element allows authors to include dynamic script in their documents. The <a href="http://www.w3.org/TR/html5/semantics.html#attr-script-defer"><code>defer</code></a> attribute is boolean attribute that indicates how the script should be executed.  If the <code>defer</code> attribute is present, then the script is executed when the page has finished parsing. The element is added to the end of the list of scripts that will execute when the document has finished parsing. Think about a FIFO processing queue : the first script element to be added to the queue will be the first script to be executed, then processing proceeds sequentially in the same order.</p>
<p>There is one very good reason for using the <code>defer</code> attribute: performance.  If you include a <code>script</code> element in your HTML page the script must be evaluated immediately while the page is being parsed.  This means that objects have to be created, styles must be flushed, etc.  This can make page loading slower.  The <code>defer</code> attribute implies that the script has no side effects on the document as it&#8217;s being loaded and can safely be evaluated at the end of the page load.</p>
<p>The <code>defer</code> attribute was first introduced in Internet Explorer 4, and added in the <a href="http://www.w3.org/TR/html401/interact/scripts.html#h-18.2.1">HTML 4 specification</a>.</p>
<p><strong>A simple test.</strong></p>
<p>Here is a simple first test to see how the attribute works. The following lines are in the head element of a page:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">‹script›
	var test1 = &quot;Test 1 : fail&quot;;
‹/script›
‹script defer›
  	console.log(test1);
‹/script›
‹script›
	test1 = &quot;Test 1 : pass&quot;;
‹/script›</pre></div></div>

<p>If the <code>defer</code> attribute for the script element is correctly implemented the browser will:</p>
<ol>
<li>Render the page.
<li>Execute the second script element after all the others.
<li>Display &#8220;Test 1 : pass&#8221; on the Firebug console.
</ol>
<p>If the console displays &#8220;Test 1 : fail&#8221; it&#8217;s because the scripts are executed in the same order as in the source code.</p>
<p>Note that the correct syntax for XHTML documents is:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">defer</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;defer&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p><strong>A more advanced test</strong></p>
<p>This second test is a way to see how the feature works in a webpage with multiple script elements inserted:</p>
<ul>
<li>Inline in the <code>head</code> and <code>body</code> elements
<li>External via <code>src</code> attribute in <code>head</code> and <code>body</code> elements
<li>With dynamic DOM insertion
</ul>
<p>Here is partial source code of a webpage that tests how defer affects script loading and parsing order:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">‹!doctype html›
‹html›
    ‹head›
        ‹title› Test 2 ‹/title›
        ‹script› var test2 = &quot;Test 2 :\n\n&quot;; ‹/script›
        ‹script› document.addEventListener(&quot;DOMContentLoaded&quot;,
                function(){
                        test2 += &quot;\tDOMContentLoaded\n&quot;;
                }, false);
        ‹/script›
        ‹script defer› test2 += &quot;\tInline HEAD deferred\n&quot;; ‹/script›
        ‹script› test2 += &quot;\tInline HEAD\n&quot;; ‹/script›
        ‹script src=&quot;script1.js&quot; defer›
                // External HEAD deferred (script1.js) 
        ‹/script›
        ‹script src=&quot;script2.js&quot;›
                // External HEAD  (script2.js)
        ‹/script›
	‹script›
            // Dynamic DOM insertion of a script (script3.js)
            head = document.getElementsByTagName('head')[0];
            script3 = document.createElement('script');
            script3.setAttribute('src', 'script3.js');
            head.appendChild(script3);
            // Dynamic DOM insertion of a deferred script (script4.js)
            script4 = document.createElement('script');
            script4.setAttribute('defer', 'defer');
            script4.setAttribute('src', 'script4.js');
            head.appendChild(script4);
	‹/script›
	‹script defer›
            // Deferred dynamic DOM insertion of a script (script5.js)
            head = document.getElementsByTagName('head')[0];
            script5 = document.createElement('script');
            script5.setAttribute('src', 'script5.js');
            head.appendChild(script5);
            // Deferred dynamic DOM insertion of a deferred script
            // (script6.js)
            script6 = document.createElement('script');
            script6.setAttribute('defer', 'defer');
            script6.setAttribute('src', 'script6.js');
            head.appendChild(script6);
	‹/script›
    ‹/head›
    ‹body onload=&quot;test2 += '\tBody onLoad\n';&quot;›
        ‹script defer› test2 += &quot;\tInline BODY deferred\n&quot;; ‹/script›
        ‹script› test2 += &quot;\tInline BODY\n&quot;; ‹/script›
&nbsp;
	... other body content ...
&nbsp;
		&lt;a onclick=&quot;alert(test2);&quot;&gt;Launch test 2&lt;/a&gt;
&nbsp;
	... other body content ...
&nbsp;
        ‹script src=&quot;script7.js&quot; defer›
                // External BODY deferred (script7.js)
        ‹/script›
        ‹script src=&quot;script8.js&quot;›
                // External BODY (script8.js)
        ‹/script›
    ‹/body›
‹/html›</pre></div></div>

<p>When you click on the &#8220;Launch test 2&#8243; link in the document a pop-up appears with a list in it. This list shows the order of script elements loaded during the session.</p>
<p>The test also displays the <code>DOMContentLoaded</code> and <code>body.onload</code> events when they are fired.</p>
<p>If the <code>defer</code> attribute is correctly implemented in the browser, all the deferred lines should be near the bottom of the list.</p>
<p>Results of the second test for each browser are below (deferred scripts are in green color) :</p>
<ul>
<li> <b>The <tt>defer</tt> attribute behavior in the Firefox 3.5 browser is correct</b>:</p>
<ol>
<li>Inline HEAD
</li>
<li>External HEAD (script2.js)
</li>
<li>Dynamic DOM insertion of a script (script3.js)
</li>
<li>Inline BODY
</li>
<li>External BODY (script8.js)
</li>
<li><font style="color: green;">Inline HEAD deferred</font>
</li>
<li><font style="color: green;">External HEAD deferred (script1.js)</font>
</li>
<li><font style="color: green;">Dynamic DOM insertion of a deferred script (script4.js)</font>
</li>
<li><font style="color: green;">Inline BODY deferred</font>
</li>
<li><font style="color: green;">External BODY deferred (script7.js)</font>
</li>
<li><font style="color: green;">Deferred dynamic DOM insertion of a script (script5.js)</font>
</li>
<li><font style="color: green;">Deferred dynamic DOM insertion of a deferred script (script6.js)</font>
</li>
<li>DOMContentLoaded
</li>
<li>Body onLoad
</li>
</ol>
</li>
</ul>
<ul>
<li> <b>The <tt>defer</tt> attribute behavior in the IE 8 browser is <i>erratic</i>: the order is different at each reload</b>&nbsp;:</p>
<ol>
<li>Inline HEAD
</li>
<li>External HEAD (script2.js)
</li>
<li>Inline BODY
</li>
<li>External BODY (script8.js)
</li>
<li>Dynamic DOM insertion of a script (script3.js)
</li>
<li><font style="color: green;">Dynamic DOM insertion of a deferred script (script4.js)</font>
</li>
<li><font style="color: green;">Inline HEAD deferred</font>
</li>
<li><font style="color: green;">External HEAD deferred (script1.js)</font>
</li>
<li><font style="color: green;">Inline BODY deferred</font>
</li>
<li><font style="color: green;">External BODY deferred (script7.js)</font>
</li>
<li>Body onLoad
</li>
<li><font style="color: green;">Deferred dynamic DOM insertion of a script (script5.js)</font>
</li>
<li><font style="color: green;">Deferred dynamic DOM insertion of a deferred script (script6.js)</font>
</li>
</ol>
</li>
</ul>
<ul>
<li> <b>The <tt>defer</tt> attribute behavior in a WebKit browser (Safari 4.0) is <i>erratic</i>&nbsp;: the order is different at each reload</b>&nbsp;:</p>
<ol>
<li><font style="color: green;">Inline HEAD deferred</font>
</li>
<li>Inline HEAD
</li>
<li><font style="color: green;">External HEAD deferred (script1.js)</font>
</li>
<li>External HEAD (script2.js)
</li>
<li><font style="color: green;">Inline BODY deferred</font>
</li>
<li>Inline BODY
</li>
<li><font style="color: green;">External BODY deferred (script7.js)</font>
</li>
<li><font style="color: green;">Deferred dynamic DOM insertion of a script (script5.js)</font>
</li>
<li><font style="color: green;">Dynamic DOM insertion of a deferred script (script4.js)</font>
</li>
<li><font style="color: green;">Deferred dynamic DOM insertion of a deferred script (script6.js)</font>
</li>
<li>Dynamic DOM insertion of a script (script3.js)
</li>
<li>External BODY (script8.js)
</li>
<li>DOMContentLoaded
</li>
<li>Body onLoad
</li>
</ol>
</li>
</ul>
<ul>
<li> <b>The <tt>defer</tt> attribute behavior in the Opera 10.00 Beta browser</b>:</p>
<ol>
<li><font style="color: green;">Inline HEAD deferred</font>
</li>
<li>Inline HEAD
</li>
<li><font style="color: green;">External HEAD deferred (script1.js)</font>
</li>
<li>External HEAD (script2.js)
</li>
<li>Dynamic DOM insertion of a script (script3.js)
</li>
<li><font style="color: green;">Dynamic DOM insertion of a deferred script (script4.js)</font>
</li>
<li><font style="color: green;">Deferred dynamic DOM insertion of a script (script5.js)</font>
</li>
<li><font style="color: green;">Deferred dynamic DOM insertion of a deferred script (script6.js)</font>
</li>
<li><font style="color: green;">Inline BODY deferred</font>
</li>
<li>Inline BODY
</li>
<li><font style="color: green;">External BODY deferred (script7.js)</font>
</li>
<li>External BODY (script8.js)
</li>
<li>DOMContentLoaded
</li>
<li>Body onLoad
</li>
</ol>
</li>
</ul>
<p>We hope that this has been a useful introduction to how the <code>defer</code> attribute works in Firefox 3.5.  The tests above will also help you predict behavior in other browsers as well.</p>
<p><strong>Resources</strong></p>
<ul>
<li> The fixed bug 28293 &#8211; (defer) scripts with defer attribute not deferred&nbsp;: <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=28293" class="external text" title="https://bugzilla.mozilla.org/show_bug.cgi?id=28293" rel="nofollow">bug 28293</a>
<li> The &#8220;JavaScript: Defer Execution&#8221; article on the WebsiteOptimization website&nbsp;: <a href="http://www.websiteoptimization.com/speed/tweak/defer/" class="external free" title="http://www.websiteoptimization.com/speed/tweak/defer/" rel="nofollow">http://www.websiteoptimization.com/speed/tweak/defer/</a>
<li> The <tt>script</tt> article on Mozilla Developer Center&nbsp;: <a href="https://developer.mozilla.org/En/HTML/Element/Script" class="external free" title="https://developer.mozilla.org/En/HTML/Element/Script" rel="nofollow">https://developer.mozilla.org/En/HTML/Element/Script</a>
<li> The &#8220;<tt>script</tt> element&#8221; part of the HTML 5 Draft&nbsp;: <a href="http://www.whatwg.org/specs/web-apps/current-work/#script" class="external free" title="http://www.whatwg.org/specs/web-apps/current-work/#script" rel="nofollow">http://www.whatwg.org/specs/web-apps/current-work/#script</a>
<li> The &#8220;Parsing HTML documents&nbsp;: the end&#8221; part of the HTML 5 Draft&nbsp;: <a href="http://www.whatwg.org/specs/web-apps/current-work/#the-end" class="external free" title="http://www.whatwg.org/specs/web-apps/current-work/#the-end" rel="nofollow">http://www.whatwg.org/specs/web-apps/current-work/#the-end</a>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2009/06/defer/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>saving data with localStorage</title>
		<link>http://hacks.mozilla.org/2009/06/localstorage/</link>
		<comments>http://hacks.mozilla.org/2009/06/localstorage/#comments</comments>
		<pubDate>Fri, 26 Jun 2009 03:58:27 +0000</pubDate>
		<dc:creator>Christopher Blizzard</dc:creator>
				<category><![CDATA[35 Days]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Feature]]></category>
		<category><![CDATA[Firefox 3.5]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=889</guid>
		<description><![CDATA[This post was written by Jeff Balogh. Jeff works on Mozilla&#8217;s web development team. New in Firefox 3.5, localStorage is a part of the Web Storage specification. localStorage provides a simple Javascript API for persisting key-value pairs in the browser. It shouldn&#8217;t be confused with the SQL database storage proposal, which is a separate (and [...]]]></description>
			<content:encoded><![CDATA[<p><em>This post was written by <a href="http://blog.jeffbalogh.org/">Jeff Balogh</a>.  Jeff works on Mozilla&#8217;s <a href="http://blog.mozilla.com/webdev/">web development team</a>.</em></p>
<p>New in Firefox 3.5, <a href="https://developer.mozilla.org/en/DOM/Storage#localStorage"><code>localStorage</code></a> is a part of the <a href="http://www.w3.org/TR/webstorage/#the-storage-interface">Web Storage</a> specification.  <code>localStorage</code> provides a simple Javascript <abbr title="Application Programming Interface">API</abbr> for persisting key-value pairs in the browser.  It shouldn&#8217;t be confused with the <a href="http://www.w3.org/TR/webstorage/#sql">SQL database storage</a> proposal, which is a separate (<a href="http://blog.vlad1.com/2009/04/06/html5-web-storage-and-sql/">and more contentious</a>) part of the Web Storage spec.  Key-value pairs could conceivably be stored in cookies, but you wouldn&#8217;t want to do that.  Cookies are sent to the server with every request, presenting performance issues with large data sets and the potential for security problems, and you have to write your own interface for treating cookies like a database.</p>
<p>Here&#8217;s a small demo that stores the content of a <code>textarea</code> in <code>localStorage</code>.  You can change the text, open a new tab, and find your updated content.  Or you can restart the browser and your text will still be there.</p>
<p><textarea id="persisted-text" rows=5 cols=50></textarea><br />
<script type="text/javascript">
  var supported = 'This text will be saved locally, forever.',
      unsupported = 'Oh no! Your browser does not support localStorage.';
  if (window.localStorage) {
      var p = document.querySelector('#persisted-text');
      if (localStorage.text == null) {
          localStorage.text = p.value = supported;
      } else {
          p.value = localStorage.text;
      }
      p.addEventListener('keyup', function(){ localStorage.text = p.value; }, false);
  } else {
      document.getElementById('persisted-text').value = unsupported;
  }
</script></p>
<p>The easiest way to use <code>localStorage</code> is to treat it like a regular object:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&gt;&gt;&gt;</span> localStorage.<span style="color: #660066;">foo</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'bar'</span>
<span style="color: #339933;">&gt;&gt;&gt;</span> localStorage.<span style="color: #660066;">foo</span>
<span style="color: #3366CC;">&quot;bar&quot;</span>
<span style="color: #339933;">&gt;&gt;&gt;</span> localStorage.<span style="color: #660066;">length</span>
<span style="color: #CC0000;">1</span>
<span style="color: #339933;">&gt;&gt;&gt;</span> localStorage<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>
<span style="color: #3366CC;">&quot;foo&quot;</span>
<span style="color: #339933;">&gt;&gt;&gt;</span> localStorage<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'foo'</span><span style="color: #009900;">&#93;</span>
<span style="color: #3366CC;">&quot;bar&quot;</span>
<span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #000066; font-weight: bold;">delete</span> localStorage<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'foo'</span><span style="color: #009900;">&#93;</span>
<span style="color: #339933;">&gt;&gt;&gt;</span> localStorage.<span style="color: #660066;">length</span>
<span style="color: #CC0000;">0</span>
<span style="color: #339933;">&gt;&gt;&gt;</span> localStorage.<span style="color: #660066;">not_set</span>
<span style="color: #003366; font-weight: bold;">null</span></pre></div></div>

<p>There&#8217;s also a more wordy API for people who like that sort of thing:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&gt;&gt;&gt;</span> localStorage.<span style="color: #660066;">clear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #339933;">&gt;&gt;&gt;</span> localStorage.<span style="color: #660066;">setItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'bar'</span><span style="color: #009900;">&#41;</span>
<span style="color: #339933;">&gt;&gt;&gt;</span> localStorage.<span style="color: #660066;">getItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo'</span><span style="color: #009900;">&#41;</span>
<span style="color: #3366CC;">&quot;bar&quot;</span>
<span style="color: #339933;">&gt;&gt;&gt;</span> localStorage.<span style="color: #660066;">key</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
<span style="color: #3366CC;">&quot;foo&quot;</span>
<span style="color: #339933;">&gt;&gt;&gt;</span> localStorage.<span style="color: #660066;">removeItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo'</span><span style="color: #009900;">&#41;</span>
<span style="color: #339933;">&gt;&gt;&gt;</span> localStorage.<span style="color: #660066;">length</span>
<span style="color: #CC0000;">0</span></pre></div></div>

<p>If you want to have a <code>localStorage</code> database mapped to the current session, you can use <code>sessionStorage</code>.  It has the same interface as <code>localStorage</code>, but the lifetime of <code>sessionStorage</code> is limited to the current browser window.  You can follow links around the site in the same window and <code>sessionStorage</code> will be maintained (going to different sites is fine too), but once that window is closed the database will be deleted.  <code>localStorage</code> is for long-term storage, as the w3c spec instructs browsers to consider the data &#8220;potentially user-critical&#8221;.</p>
<p>I was a tad disappointed when I found out that <code>localStorage</code> only supports storing strings, since I was hoping for something more structured.  But with <a href="https://developer.mozilla.org/En/Using_native_JSON">native JSON</a> support it&#8217;s easy to create an object store on top of <code>localStorage</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Storage.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">setObject</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setItem</span><span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span> JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
Storage.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">getObject</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> JSON.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getItem</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><code>localStorage</code> databases are scoped to an <a href="http://www.w3.org/TR/html5/browsers.html#origin-0">HTML5 origin</a>, basically the tuple <code>(scheme, host, port)</code>.  This means that the database is shared across all pages on the same domain, even concurrently by multiple browser tabs.  However, a page connecting over <code>http://</code> cannot see a database that was created during an <code>https://</code> session.</p>
<p><code>localStorage</code> and <code>sessionStorage</code> are supported by Firefox 3.5, Safari 4.0, and IE8.  You can find more compatibility details on <a href="http://www.quirksmode.org/dom/html5.html#localstorage">quirksmode.org</a>, including more detail on the <a href="http://www.quirksmode.org/blog/archives/2009/06/html5_storage_t.html">storage event</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2009/06/localstorage/feed/</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
		<item>
		<title>DOM Traversal in Firefox 3.5</title>
		<link>http://hacks.mozilla.org/2009/06/dom-traversal/</link>
		<comments>http://hacks.mozilla.org/2009/06/dom-traversal/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 14:29:59 +0000</pubDate>
		<dc:creator>John Resig</dc:creator>
				<category><![CDATA[DOM]]></category>
		<category><![CDATA[Firefox 3.5]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[w3c]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=734</guid>
		<description><![CDATA[Firefox 3.5 includes new support for two W3C DOM traversal specifications. The first, the Element Traversal API, focuses on making element-by-element traversal easier, the second, the NodeIterator interface which makes finding all node types much easier. Element Traversal API The purpose of the Element Traversal API is to make it easier for developers to traverse [...]]]></description>
			<content:encoded><![CDATA[<p>Firefox 3.5 includes new support for two W3C DOM traversal specifications. The first, the <a href="http://www.w3.org/TR/ElementTraversal/">Element Traversal API</a>, focuses on making element-by-element traversal easier, the second, the <a href="http://www.w3.org/TR/DOM-Level-2-Traversal-Range/traversal">NodeIterator</a> interface which makes finding all node types much easier.</p>
<p><strong>Element Traversal API</strong></p>
<p>The purpose of the <a href="http://www.w3.org/TR/ElementTraversal/">Element Traversal API</a> is to make it easier for developers to traverse through DOM elements without having to worry about intermediary text nodes, comment nodes, etc. This has long been a bane of web developers, in particular, with cases like <code>document.documentElement.firstChild</code> yielding different results depending on the whitespace structure of a document.</p>
<p>The Element Traversal API introduces a number of <a href="http://dev.w3.org/2006/webapi/ElementTraversal/publish/ElementTraversal.html#ecmascript-bindings">new DOM node properties</a> which can make this traversing much simpler.</p>
<p>Here&#8217;s a full break-down of the existing DOM node properties and their new counterparts:</p>
<table>
<tr>
<th>Purpose</th>
<th>All DOM Nodes</th>
<th>Just DOM Elements</th>
</tr>
<tr>
<td>First</td>
<td>.firstChild</td>
<td>.firstElementChild</td>
</tr>
<tr>
<td>Last</td>
<td>.lastChild</td>
<td>.lastElementChild</td>
</tr>
<tr>
<td>Previous</td>
<td>.previousSibling</td>
<td>.previousElementSibling</td>
</tr>
<tr>
<td>Next</td>
<td>.nextSibling</td>
<td>.nextElementSibling</td>
</tr>
<tr>
<td>Length</td>
<td>.childNodes.length</td>
<td>.childElementCount</td>
</tr>
</table>
<p>These properties provide a fairly simple addition to the DOM specification (and, honestly, they&#8217;re something that should&#8217;ve been in the specification to begin with).</p>
<p>There is one property that is conspicuously absent, though: <code>.childElements</code> (as a counterpart to <code>.childNodes</code>). This property (which contained a live NodeSet of the child elements of the DOM element) was in previous iterations of the specification but it seems to have gone on the cutting room floor at some point in the interim.</p>
<p>But all is not lost. Right now Internet Explorer, Opera, and Safari all support a <code>.children</code> property which provides a super-set of the functionality that was supposed to have been made possible by <code>.childElements</code>. When support for the Element Traversal API was finally landed for Firefox 3.5, support for <code>.children</code> was included. This means that every major browser now supports this property (far in advance of all browsers supporting the rest of the true Element Traversal specification).</p>
<p>Some examples of the Element Traversal API (and <code>.children</code>) in action:</p>
<p>Show next element when a click occurs:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">someElement.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">nextSiblingElement</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Add classes to all of the child elements:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> someElement.<span style="color: #660066;">children</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    someElement.<span style="color: #660066;">children</span><span style="color: #009900;">&#91;</span> i <span style="color: #009900;">&#93;</span>.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;active&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>NodeIterator API</strong></p>
<p>NodeIterator is a relatively old API that hasn&#8217;t seen wide adoption, and has just been implemented in Firefox 3.5. Specifically, the <a href="https://developer.mozilla.org/En/DOM/NodeIterator">NodeIterator API</a> is designed to allow for easy traversal of all nodes in a DOM document (this includes text nodes, comments, etc.).</p>
<p>The API itself is rather convoluted (containing a number of features that aren&#8217;t immediately important to most developers) but if you wish to use it for some simpler tasks it be quite easy.</p>
<p>The API works by creating a NodeIterator (using <code>document.createNodeIterator</code>) and passing in a series of filters. The NodeIterator is capable of returning all nodes in a document (or within the context of a given node) thus you&#8217;ll want to filter it down to only show the ones that you desire. A simple example of this can be found below.</p>
<p>Construct a NodeIterator for iterating through all the comment nodes in a document.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> nodeIterator <span style="color: #339933;">=</span> document.<span style="color: #660066;">createNodeIterator</span><span style="color: #009900;">&#40;</span>
    document<span style="color: #339933;">,</span>
    NodeFilter.<span style="color: #660066;">SHOW_COMMENT</span><span style="color: #339933;">,</span>
    <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span>
    <span style="color: #003366; font-weight: bold;">false</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> node<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span>node <span style="color: #339933;">=</span> nodeIterator.<span style="color: #660066;">nextNode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    node.<span style="color: #660066;">parentNode</span>.<span style="color: #660066;">removeChild</span><span style="color: #009900;">&#40;</span> node <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Once constructed the NodeIterator is bi-directional (you can move in any direction, using <a href="https://developer.mozilla.org/En/DOM/NodeIterator.previousNode">previousNode</a> or <a href="https://developer.mozilla.org/En/DOM/NodeIterator.nextNode">nextNode</a>).</p>
<p>Perhaps the best use of the API is in traversing over commonly-used (but difficult to traverse) nodes like comments and text nodes. Since there already exist a few APIs for traversing DOM elements (such as <code>getElementsByTagName</code>) this does come as a welcomed respite to the normal means of node traversal.</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2009/06/dom-traversal/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>DOM selectors API in Firefox 3.5</title>
		<link>http://hacks.mozilla.org/2009/06/dom-selectors-api/</link>
		<comments>http://hacks.mozilla.org/2009/06/dom-selectors-api/#comments</comments>
		<pubDate>Sat, 13 Jun 2009 22:21:53 +0000</pubDate>
		<dc:creator>John Resig</dc:creator>
				<category><![CDATA[35 Days]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Feature]]></category>
		<category><![CDATA[Firefox 3.5]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=116</guid>
		<description><![CDATA[The Selectors API recommendation, published by the W3C, is a relatively new effort that gives JavaScript developers the ability to find DOM elements on a page using CSS selectors. This single API takes the complicated process of traversing and selecting elements from the DOM and unifies it under a simple unified interface. Out of all [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://dev.w3.org/2006/webapi/selectors-api/">Selectors API</a> recommendation, published by the W3C, is a relatively new effort that gives JavaScript developers the ability to find DOM elements on a page using CSS selectors. This single API takes the complicated process of traversing and selecting elements from the DOM and unifies it under a simple unified interface.</p>
<p>Out of all the recent work to come out of the standards process this is one of the better-supported efforts across all browsers: Usable today in Internet Explorer 8, Chrome, and Safari and arriving in Firefox 3.5 and Opera 10.</p>
<p><strong>Using <code>querySelectorAll</code></strong></p>
<p>The Selectors API provides two methods on all DOM documents, elements, and fragments: <code>querySelector</code> and <code>querySelectorAll</code>. The methods work virtually identically, they both accept a CSS selector and return the resulting DOM elements (the exception being that <code>querySelector</code> only returns the first element).</p>
<p>For example, given the following HTML snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;id&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;class&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>First paragraph.<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Second paragraph.<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>We would be able to use <code>querySelectorAll</code> to make the background of all the paragraphs, inside the div with the ID of &#8216;id&#8217;, red.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> p <span style="color: #339933;">=</span> document.<span style="color: #660066;">querySelectorAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#id p&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> p.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    p<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;red&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Or we could find the first child paragraph of a div that has a class of &#8216;class&#8217; and give it a class name of &#8216;first&#8217;.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div.class &gt; p:first-child&quot;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;first&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Normally these types of traversals would be very tedious in long-form JavaScript/DOM code, taking up multiple lines and queries each.</p>
<p>While the actual use of the Selectors API methods is relatively simple (each taking a single argument) the challenging part comes in when choosing which CSS selectors to use. The Selectors API taps in to the native CSS selectors provided by the browser, for use in styling elements with CSS. For most browsers (Firefox, Safari, Chrome, and Opera) this means that you have access to the full gamut of <a href="http://www.w3.org/TR/css3-selectors/">CSS 3 selectors</a>. Internet Explorer 8 provides a more-limited subset that encompasses <a href="http://www.w3.org/TR/CSS2/selector.html">CSS 2 selectors</a> (which are still terribly useful).</p>
<p>The biggest hurdle, for most new users to the Selectors API, is determining which CSS selectors are appropriate for selecting the elements that you desire &#8211; especially since most developers who write cross-browser code only have significant experience with a limited subset of fully-working CSS 1 selectors.</p>
<p>While the <a href="http://www.w3.org/TR/CSS2/selector.html">CSS 2</a> and <a href="http://www.w3.org/TR/css3-selectors/">CSS 3 selector</a> specifications can serve as a good start for learning more about what&#8217;s available to you there also exist a number of useful guides for learning more:</p>
<ul>
<li><a href="http://www.456bereastreet.com/archive/200601/css_3_selectors_explained/">CSS 3 Selectors Explained</a></li>
<li><a href="http://www.xml.com/pub/a/2003/06/18/css3-selectors.html">XML.com: CSS 3 Selectors</a></li>
<li><a href="http://docs.jquery.com/Selectors">jQuery Selector Reference</a> (Note: Also includes custom, non-standard, selectors.)</li>
</ul>
<p><strong>Implementations in the Wild</strong></p>
<p>The most compelling use case of the Selectors API is not its direct use by web developers, but its use by 3rd-party libraries that already provide DOM CSS selector functionality. The trickiest problem towards adopting the use of the Selectors API, today, is that it isn&#8217;t available in all browsers that users develop for (this includes IE 6, IE 7 and Firefox 3). Thus, until those browsers are no longer used, we must use some intermediary utility to recreate the full DOM CSS selector experience.</p>
<p>Thankfully, a number of libraries already exist that provide an API compatible with the Selectors API (in fact, much of the inspiration for the Selectors API comes from the existence of these libraries in the first place). Additionally, many of these implementations already use the Selectors API behind the scenes. This means that you can use using DOM CSS selectors in all browsers that you support AND get the benefit of faster performance from the native Selectors API implementation, with no work to you.</p>
<p>Some existing implementations that gracefully use the new Selectors API are:</p>
<ul>
<li><a href="http://jquery.com/">jQuery</a></li>
<li><a href="http://prototypejs.org/">Prototype</a></li>
<li><a href="http://dojotoolkit.org/">Dojo</a></li>
<li><a href="http://mootools.net/">MooTools</a></li>
</ul>
<p>It&#8217;s important to emphasize the large leap in performance that you&#8217;ll gain from using this new API (in comparison to the traditional mix of DOM and JavaScript that you must employ). You can really see the difference when you look at the improvement that occurred when JavaScript libraries began to implement the new Selectors API.</p>
<p>When some tests were <a href="http://ejohn.org/blog/queryselectorall-in-firefox-31/">run previously</a> the results were as follows:</p>
<p><a href="http://ejohn.org/files/qsa-blog.full.png"><img src="http://ejohn.org/files/qsa-blog.png" width="500"></a></p>
<p>You can see the dramatic increase in performance that occurred once the libraries began using the native Selectors API implementations &#8211; it&#8217;s quite likely that this performance increase will happen in your applications, as well.</p>
<p><strong>Test Suite</strong></p>
<p>To coincide with the definition of the <a href="http://dev.w3.org/2006/webapi/selectors-api/">Selectors API specification</a> a Selectors API test suite was created by John Resig of Mozilla. This <a href="http://ejohn.org/apps/selectortest/">test suite</a> can be used as a way to determine the quality of the respective Selectors API implementations in the major browsers.</p>
<p>The current results for the browsers that support the API are:</p>
<ul>
<li>Firefox 3.5: 99.3%</li>
<li>Safari 4: 99.3%</li>
<li>Chrome 2: 99.3%</li>
<li>Opera 10b1: 97.5%</li>
<li>Internet Explorer 8: 47.4%</li>
</ul>
<p>Internet Explorer 8, as mentioned before, is missing most CSS 3 selectors &#8211; thus failing most of the associated tests.</p>
<p>As it stands, the Selectors API should serve as a simple, and fast, way of selecting DOM elements on a page. It&#8217;s already benefiting those who use JavaScript libraries that provide similar functionality so feel free to dig in today and give the API a try.</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2009/06/dom-selectors-api/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>

