<?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>Ryan Morr</title>
	<atom:link href="http://ryanmorr.com/feed" rel="self" type="application/rss+xml" />
	<link>http://ryanmorr.com</link>
	<description>JavaScript, CSS, and Web Apps</description>
	<lastBuildDate>Wed, 10 Mar 2010 02:03:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>FX 2.0: The Full-Featured Animation Framework</title>
		<link>http://ryanmorr.com/archives/fx-2-0-the-full-featured-animation-framework</link>
		<comments>http://ryanmorr.com/archives/fx-2-0-the-full-featured-animation-framework#comments</comments>
		<pubDate>Sat, 04 Jul 2009 04:44:39 +0000</pubDate>
		<dc:creator>Ryan Morr</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[FX]]></category>

		<guid isPermaLink="false">http://ryanmorr.com/?p=1443</guid>
		<description><![CDATA[It has by far been the most popular thing to come out of this blog since its inception, and I am very proud to officially release version 2, and this time I&#8217;ve included all the bells and whistles you&#8217;ve been asking for! The framework now boasts support for an impressive amount of transitions, CSS multi-unit [...]]]></description>
			<content:encoded><![CDATA[<p>It has by far been the most popular thing to come out of this blog since its inception, and I am very proud to officially release version 2, and this time I&#8217;ve included all the bells and whistles you&#8217;ve been asking for! The framework now boasts support for an impressive amount of transitions, CSS multi-unit support (em, %, ex, and all the rest), and many more optimizations for increased performance to get those smooth animations we all want.<span id="more-1443"></span></p>
<h3>How to Use?</h3>
<p>Not much has changed in the way of syntax from the <a href="http://ryanmorr.com/archives/fx-lightweight-and-standalone-animation">previous version</a>. The first argument accepts either the id of the element you wish to animate or the element itself. The second argument specifies the attributes of the animation, by defining the style and an ending value (&#8220;to&#8221;) and optionally a starting value (&#8220;from&#8221;) which otherwise defaults to the elements currently computed style:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> fx <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FX<span style="color: #000000;">&#40;</span>el<span style="color: #000000;">,</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #3366CC;">'font-size'</span><span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>to<span style="color: #000000;">:</span> <span style="color: #CC0000;">14</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    <span style="color: #3366CC;">'background-color'</span><span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>from<span style="color: #000000;">:</span> <span style="color: #3366CC;">'FF0000'</span><span style="color: #000000;">,</span> to<span style="color: #000000;">:</span> <span style="color: #3366CC;">'0000FF'</span><span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>Additionally, you can optionally specify a duration (defaults to 0.7 seconds) as the third argument, the transition (brand new) as the fourth argument, and finally a callback method and the context you wish to execute the callback in as the fifth and sixth parameters:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> fx <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FX<span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'element'</span><span style="color: #000000;">,</span> <span style="color: #000000;">&#123;</span>
    top<span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>to<span style="color: #000000;">:</span> <span style="color: #CC0000;">200</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    left<span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>to<span style="color: #000000;">:</span> <span style="color: #CC0000;">200</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    width<span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>from<span style="color: #000000;">:</span> <span style="color: #CC0000;">100</span><span style="color: #000000;">,</span> to<span style="color: #000000;">:</span> <span style="color: #CC0000;">200</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    height<span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>from<span style="color: #000000;">:</span> <span style="color: #CC0000;">100</span><span style="color: #000000;">,</span> to<span style="color: #000000;">:</span> <span style="color: #CC0000;">200</span><span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span> <span style="color: #CC0000;">1.3</span><span style="color: #000000;">,</span> <span style="color: #3366CC;">'easeOut'</span><span style="color: #000000;">,</span> callback<span style="color: #000000;">,</span> <span style="color: #003366; font-weight: bold;">this</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>The only method you need to concern yourself with is the start method, which starts the animation and requires no arguments:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> fx <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FX<span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'element'</span><span style="color: #000000;">,</span> <span style="color: #000000;">&#123;</span>height<span style="color: #000000;">:</span> <span style="color: #CC0000;">400</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
fx.<span style="color: #006600;">start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>That&#8217;s it! In just a couple of lines, you have the ability to animate any element any which way you want! What more could you ask for? I&#8217;d actually like to know!</p>
<h3>CSS Multi-Unit Support</h3>
<p>Brand new to the framework, FX now supports multiple CSS units for all scalable styles such as width and height. By defining an optional &#8220;units&#8221; property for each attribute you can specify which unit to apply to that specific style:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> fx <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FX<span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'box'</span><span style="color: #000000;">,</span> <span style="color: #000000;">&#123;</span>
    width<span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>to<span style="color: #000000;">:</span> <span style="color: #CC0000;">20</span><span style="color: #000000;">,</span> units<span style="color: #000000;">:</span> <span style="color: #3366CC;">'em'</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    height<span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>to<span style="color: #000000;">:</span> <span style="color: #CC0000;">60</span><span style="color: #000000;">,</span> units<span style="color: #000000;">:</span> <span style="color: #3366CC;">'%'</span><span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>Any and all CSS units are supported, including; em, pt, %, ex, in, mm, cm, and more. If no unit is specified for the attribute, it defaults to px.</p>
<h3>Transitions</h3>
<p>A feature promised and often asked for, FX now includes a much greater support for various transitional easing methods. Included in the library are your four basic transitions; linear, easeIn, easeOut, and easeInOut which is defined as an optional fourth parameter of the constructor, defaulting to easeInOut if not provided:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> fx <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FX<span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'component'</span><span style="color: #000000;">,</span> <span style="color: #000000;">&#123;</span>
    opacity<span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>to<span style="color: #000000;">:</span> <span style="color: #CC0000;">1</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    left<span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>to<span style="color: #000000;">:</span> <span style="color: #CC0000;">300</span><span style="color: #000000;">,</span> units<span style="color: #000000;">:</span> <span style="color: #3366CC;">'ex'</span><span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span> <span style="color: #CC0000;">1</span><span style="color: #000000;">,</span> <span style="color: #3366CC;">'easeIn'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>In case that isn&#8217;t enough, you can also download the optional transitions pack, which adds an additional 30 transitions to the framework such as; quint, expo, circ, elastic, bounce, and many more!</p>
<h3>Optimizations</h3>
<p>Version two features over a dozen optimizations designed to improve performance, usability, browser support (still without a trace of browser sniffing), and overall efficiency. For you this means increased speed and a smoother animation.</p>
<h3>Demo</h3>
<p>To try an illustrative example of all the old features mixed with the new, click <a href="http://www.ryanmorr.com/tests/fx2/">here</a>. The example demonstrates multiple tweens against multiple styles using multiple units, and multiple transitions &#8211; all of which are randomly generated.</p>
<h3>What&#8217;s Next?</h3>
<p>The framework itself still has many different directions it can go, and I&#8217;m sure in time we&#8217;ll gain support for any and all features we&#8217;ve yet to attain. From here I hope to create additional functionality exclusively related to elements and element control, this including animation queues, delays, and a wealth of helper methods for effects such as fading, sliding, highlighting, motion, morphing, and many more. If you have any suggestions as to what you would like to see in upcoming versions, as always, I welcome your input and look forward to supporting this framework for a long time to come.</p>
<h3>Download</h3>
<ul>
<li><a onclick="javascript:pageTracker._trackPageview(’/Links/http://ryanmorr.com/modules/fx-compressed.js’);" href="http://ryanmorr.com/modules/fx2/fx-compressed.js">Compressed (5kb)</a></li>
<li><a onclick="javascript:pageTracker._trackPageview(’/Links/http://ryanmorr.com/modules/fx.js’);" href="http://ryanmorr.com/modules/fx2/fx.js">Fully Documented (10kb)</a></li>
</ul>
<h3>Transitions</h3>
<ul>
<li><a onclick="javascript:pageTracker._trackPageview(’/Links/http://ryanmorr.com/modules/fx-compressed.js’);" href="http://ryanmorr.com/modules/fx2/transitions-compressed.js">Compressed (4kb)</a></li>
<li><a onclick="javascript:pageTracker._trackPageview(’/Links/http://ryanmorr.com/modules/fx.js’);" href="http://ryanmorr.com/modules/fx2/transitions.js">Uncompressed (5kb)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://ryanmorr.com/archives/fx-2-0-the-full-featured-animation-framework/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Detecting Browser CSS Style Support</title>
		<link>http://ryanmorr.com/archives/detecting-browser-css-style-support</link>
		<comments>http://ryanmorr.com/archives/detecting-browser-css-style-support#comments</comments>
		<pubDate>Sun, 03 May 2009 18:42:19 +0000</pubDate>
		<dc:creator>Ryan Morr</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[Feature Testing]]></category>

		<guid isPermaLink="false">http://ryanmorr.com/?p=1190</guid>
		<description><![CDATA[Now I&#8217;d normally be the first one to speak out in favour of unobtrusive JavaScript and attempting to keep CSS styling out of JavaScript. Rarely should you need to directly impose CSS styles on an element via the style object, after all that is what class names are for. However there are some styles that [...]]]></description>
			<content:encoded><![CDATA[<p>Now I&#8217;d normally be the first one to speak out in favour of unobtrusive JavaScript and attempting to keep CSS styling out of JavaScript. Rarely should you need to directly impose CSS styles on an element via the style object, after all that is what class names are for. However there are some styles that are commonly and justifiably manipulated within JavaScript; display, visibility, width, height, top, and left are just a few of the styles often directly manipulated within JavaScript typically for some type of UI component. Of course, some of these styles are not supported in all the mainstream browsers, such as opacity in IE and -moz-transform in Firefox &lt; 3.5.<span id="more-1190"></span></p>
<p>Since browser detection is entirely unreliable and unnecessary, we need to find something better. Therefore, we will instead test to see if the browser is capable of computing the style we are trying to determine support for.</p>
<p>In IE we can use the little known runtimeStyle object inherit to all elements in IE, it allows us to set the cascaded style (represented in currentStyle) which will override any and all other styling influences (stylesheets, inline styles, etc.) on the element. However we won&#8217;t be using runtimeStyle to set the style, but to check the value it holds to determine whether or not the style was properly computed. By simply examining the value held for the style in the runtimeStyle object, we find that it returns undefined for unsupported styles:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">typeof</span> el.<span style="color: #006600;">runtimeStyle</span>.<span style="color: #006600;">width</span> <span style="color: #000000;">!==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// true</span>
<span style="color: #003366; font-weight: bold;">typeof</span> el.<span style="color: #006600;">runtimeStyle</span>.<span style="color: #006600;">opacity</span> <span style="color: #000000;">!==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// false</span></pre></div></div>

<p>As for any standards compliant browsers, we check for support in a similar fashion; using the <em>getComputedStyle</em> method we can check to see if the style is capable of being computed/parsed. As with runtimeStyle in IE, only unsupported styles will return a value of undefined when we check the element&#8217;s computed style:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">typeof</span> view.<span style="color: #006600;">getComputedStyle</span><span style="color: #000000;">&#40;</span>el<span style="color: #000000;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span>.<span style="color: #006600;">test</span> <span style="color: #000000;">!==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// false</span>
<span style="color: #003366; font-weight: bold;">typeof</span> view.<span style="color: #006600;">getComputedStyle</span><span style="color: #000000;">&#40;</span>el<span style="color: #000000;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span>.<span style="color: #006600;">minWidth</span> <span style="color: #000000;">!==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// true</span></pre></div></div>

<p>Thats it! When we combine these techniques we get a robust method to detect CSS style support in all the mainstream browsers. Throw in some caching for increased performance against multiple tests of the same style and <em>isStyleSupported</em> is born:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">isStyleSupported <span style="color: #000000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> cache <span style="color: #000000;">=</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> el <span style="color: #000000;">=</span> document.<span style="color: #006600;">createElement</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span>style<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>style <span style="color: #003366; font-weight: bold;">in</span> cache<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">return</span> cache<span style="color: #000000;">&#91;</span>style<span style="color: #000000;">&#93;</span><span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span><span style="color: #003366; font-weight: bold;">else</span><span style="color: #000000;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> supported <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #000000;">;</span>
            <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>el.<span style="color: #006600;">runtimeStyle</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
                supported <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">typeof</span> el.<span style="color: #006600;">runtimeStyle</span><span style="color: #000000;">&#91;</span>style<span style="color: #000000;">&#93;</span> <span style="color: #000000;">!==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #000000;">;</span>
            <span style="color: #000000;">&#125;</span><span style="color: #003366; font-weight: bold;">else</span><span style="color: #000000;">&#123;</span>
                <span style="color: #003366; font-weight: bold;">var</span> view <span style="color: #000000;">=</span> document.<span style="color: #006600;">defaultView</span><span style="color: #000000;">;</span>
                <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>view <span style="color: #000000;">&amp;&amp;</span> view.<span style="color: #006600;">getComputedStyle</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
                    <span style="color: #003366; font-weight: bold;">var</span> cs <span style="color: #000000;">=</span> view.<span style="color: #006600;">getComputedStyle</span><span style="color: #000000;">&#40;</span>el<span style="color: #000000;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span>style<span style="color: #000000;">&#93;</span><span style="color: #000000;">;</span>
                    supported <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">typeof</span> cs <span style="color: #000000;">!==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #000000;">;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #003366; font-weight: bold;">return</span> cache<span style="color: #000000;">&#91;</span>style<span style="color: #000000;">&#93;</span> <span style="color: #000000;">=</span> supported<span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>Perfect right? Well not so fast, we don&#8217;t have to stop there, why not extend the method to not only determine support for styles, but assignable style values? Fixed positioning, currently not supported in IE6, can be a valuable style for UI components such as overlays, so how can we test for that? Rather than using browser detection or adding an event listener to dynamically reposition the overlay every time the user scrolls the window, we can just employ fixed positioning for those browsers that support it!</p>
<p>Amazingly, IE makes this quite easy using the technique we&#8217;ve already employed for determining style support. All we need to add is value assignment via the style object and wrap the test case in a try-catch block as IE will throw an error for unsupported style values.</p>
<p>However, if we are looking to determine support for the style values in standards compliant browsers, than <em>getComputedStyle</em> proves to be unreliable. Take for instance any number of scalable styles such as width and height, the method will always convert any units back to pixels, meaning a quick comparison would fail:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">el.<span style="color: #006600;">style</span>.<span style="color: #006600;">width</span> <span style="color: #000000;">=</span> <span style="color: #3366CC;">&quot;1em&quot;</span><span style="color: #000000;">;</span>
document.<span style="color: #006600;">defaultView</span>.<span style="color: #006600;">getComputedStyle</span><span style="color: #000000;">&#40;</span>el<span style="color: #000000;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span>.<span style="color: #006600;">width</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// 16px</span></pre></div></div>

<p>In addition to that testing against unsupported style values will not always return a definitive result where as support can be easily determined. For example, setting an element&#8217;s color to an unrecognized value will always result in the return of the rgb representation of the color black:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">el.<span style="color: #006600;">style</span>.<span style="color: #006600;">color</span> <span style="color: #000000;">=</span> <span style="color: #3366CC;">&quot;test&quot;</span><span style="color: #000000;">;</span>
document.<span style="color: #006600;">defaultView</span>.<span style="color: #006600;">getComputedStyle</span><span style="color: #000000;">&#40;</span>el<span style="color: #000000;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span>.<span style="color: #006600;">color</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// rgb(0,0,0)</span></pre></div></div>

<p>To quickly resolve this and avoid making some elaborate parsing method, we need to use a secondary test case. We create a new element as part of our base element&#8217;s innerHTML and add the styling we wish to test for as inline styles. When we retrieve the new element and examine its style, we find that unsupported style values will always return an empty string:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">el.<span style="color: #006600;">innerHTML</span> <span style="color: #000000;">=</span> <span style="color: #3366CC;">'&lt;div style=&quot;color:test ; width:10px&quot;&gt;&lt;/div&gt;'</span><span style="color: #000000;">;</span>
el.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">style</span>.<span style="color: #006600;">color</span> <span style="color: #000000;">!==</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// false</span>
el.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">style</span>.<span style="color: #006600;">width</span> <span style="color: #000000;">!==</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// true</span></pre></div></div>

<p>Using this technique means any styles supplied must be in standard hyphenated CSS format (background-color) as opposed to the previous method which must be handed camel-cased styles (backgroundColor). This means that we need to include a function to handle camel case conversions; a small hit on performance yes, but also some syntastic sugar for the method &#8211; give and take I suppose.</p>
<p>Throwing these new techniques together results in a method capable of determining support for not only styles but their supported assignable/recognizable values by supplying an optional second parameter.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">isStyleSupported <span style="color: #000000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> cache <span style="color: #000000;">=</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> el <span style="color: #000000;">=</span> document.<span style="color: #006600;">createElement</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">var</span> toCamelCase <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span>str<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> parts <span style="color: #000000;">=</span> str.<span style="color: #006600;">split</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'-'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">,</span> camel <span style="color: #000000;">=</span> parts<span style="color: #000000;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">;</span>
        <span style="color: #003366; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #000000;">=</span><span style="color: #CC0000;">1</span><span style="color: #000000;">,</span> len<span style="color: #000000;">=</span>parts.<span style="color: #006600;">length</span><span style="color: #000000;">;</span> i <span style="color: #000000;">&lt;</span> len<span style="color: #000000;">;</span> i<span style="color: #000000;">++</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
            camel <span style="color: #000000;">+=</span> parts<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #006600;">charAt</span><span style="color: #000000;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #000000;">&#41;</span>.<span style="color: #006600;">toUpperCase</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">+</span> parts<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #006600;">substring</span><span style="color: #000000;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #003366; font-weight: bold;">return</span> camel<span style="color: #000000;">;</span>
    <span style="color: #000000;">&#125;</span><span style="color: #000000;">;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span>style<span style="color: #000000;">,</span> value<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> key <span style="color: #000000;">=</span> style <span style="color: #000000;">+</span> value <span style="color: #000000;">||</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #000000;">;</span>
        <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>key <span style="color: #003366; font-weight: bold;">in</span> cache<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">return</span> cache<span style="color: #000000;">&#91;</span>key<span style="color: #000000;">&#93;</span><span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span><span style="color: #003366; font-weight: bold;">else</span><span style="color: #000000;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> supported <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #000000;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> camel <span style="color: #000000;">=</span> toCamelCase<span style="color: #000000;">&#40;</span>style<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
            <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>el.<span style="color: #006600;">runtimeStyle</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
                <span style="color: #003366; font-weight: bold;">try</span><span style="color: #000000;">&#123;</span>
                    el.<span style="color: #006600;">style</span><span style="color: #000000;">&#91;</span>camel<span style="color: #000000;">&#93;</span> <span style="color: #000000;">=</span> value <span style="color: #000000;">||</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #000000;">;</span>
                    supported <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">typeof</span> el.<span style="color: #006600;">runtimeStyle</span><span style="color: #000000;">&#91;</span>camel<span style="color: #000000;">&#93;</span> <span style="color: #000000;">!==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #000000;">;</span>
                <span style="color: #000000;">&#125;</span><span style="color: #003366; font-weight: bold;">catch</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">;</span>
            <span style="color: #000000;">&#125;</span><span style="color: #003366; font-weight: bold;">else</span><span style="color: #000000;">&#123;</span>
                <span style="color: #003366; font-weight: bold;">var</span> view <span style="color: #000000;">=</span> document.<span style="color: #006600;">defaultView</span><span style="color: #000000;">;</span>
                <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>view <span style="color: #000000;">&amp;&amp;</span> view.<span style="color: #006600;">getComputedStyle</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
                    <span style="color: #003366; font-weight: bold;">var</span> cs <span style="color: #000000;">=</span> view.<span style="color: #006600;">getComputedStyle</span><span style="color: #000000;">&#40;</span>el<span style="color: #000000;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span>camel<span style="color: #000000;">&#93;</span><span style="color: #000000;">;</span>
                    supported <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">typeof</span> cs <span style="color: #000000;">!==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #000000;">;</span>
                    <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>value<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
                        el.<span style="color: #006600;">innerHTML</span> <span style="color: #000000;">=</span> <span style="color: #3366CC;">'&lt;div style=&quot;'</span><span style="color: #000000;">+</span>style<span style="color: #000000;">+</span><span style="color: #3366CC;">':'</span><span style="color: #000000;">+</span>value<span style="color: #000000;">+</span><span style="color: #3366CC;">'&quot;&gt;&lt;/div&gt;'</span><span style="color: #000000;">;</span>
                        supported <span style="color: #000000;">=</span> supported <span style="color: #000000;">&amp;&amp;</span> el.<span style="color: #006600;">firstChild</span>.<span style="color: #006600;">style</span><span style="color: #000000;">&#91;</span>camel<span style="color: #000000;">&#93;</span> <span style="color: #000000;">!==</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #000000;">;</span>
                    <span style="color: #000000;">&#125;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #003366; font-weight: bold;">return</span> cache<span style="color: #000000;">&#91;</span>key<span style="color: #000000;">&#93;</span> <span style="color: #000000;">=</span> supported<span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>So how reliable are the methods? Well in all my testing in all the browsers I could get my hands on &#8211; I have yet to encounter a single false positive in any case. So to help prove the reliability, I have put together a simple <a href="http://ryanmorr.com/tests/isstylesupported/index.html" target="_self">test page</a> which includes various styles and their corresponding results when tested against <em>isStyleSupported</em>. In addition to that, I have included two free-form text inputs so you can try any styles you wish.</p>
<p>Hopefully we&#8217;ll see some of the developers of the mainstream libraries take notice and employ these techniques in future versions of their respective frameworks and finally rid themselves of the unreliable browser detection so many of them still utilize today in cases such as this.</p>
]]></content:encoded>
			<wfw:commentRss>http://ryanmorr.com/archives/detecting-browser-css-style-support/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Env: Feature Testing</title>
		<link>http://ryanmorr.com/archives/env-feature-testing</link>
		<comments>http://ryanmorr.com/archives/env-feature-testing#comments</comments>
		<pubDate>Sun, 19 Apr 2009 23:05:27 +0000</pubDate>
		<dc:creator>Ryan Morr</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Canvas]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Event]]></category>
		<category><![CDATA[Feature Testing]]></category>
		<category><![CDATA[SVG]]></category>

		<guid isPermaLink="false">http://ryanmorr.com/?p=1009</guid>
		<description><![CDATA[As a followup to my last post regarding browser detection, I have decided to release a new standalone and lightweight library that will hopefully put browser sniffing to rest once and for all. In a project I&#8217;ve entitled Env (short for environment), you now have the power of knowing exactly what your browser environment is [...]]]></description>
			<content:encoded><![CDATA[<p>As a followup to my last post regarding <a href="http://ryanmorr.com/archives/brower-detection-necessary-or-negligent">browser detection</a>, I have decided to release a new standalone and lightweight library that will hopefully put browser sniffing to rest once and for all. In a project I&#8217;ve entitled <em>Env</em> (short for environment), you now have the power of knowing exactly what your browser environment is and is not capable of without actually knowing what browser it is. Many of the features and bugs that are often encountered during runtime no longer need to be sniffed out by the user agent, despite the fact that some of the mainstream libraries still insist upon it. In addition to bug and feature testing, the library also supports advanced type detection and event support detection to further enhance your usability, but also separate your dependence on browser sniffing. Goodbye <em>navigator.userAgent</em> and good riddens &#8211; you&#8217;ve broken my app for the last time!<span id="more-1009"></span></p>
<h3>Support</h3>
<p>The bread and butter of the library is of course the capability tests, <em>Env.support</em> is designed to provide a reliable test case for some of the most popular browser features available today that are difficult to detect and/or not well supported amongst the various browsers. Tested features include; canvas, flash, native array conversion, dynamic script eval, mutation events, the box model, and more!</p>
<p>By default, any features that cannot be immediately resolved to a Boolean value are set to false and must prove their support. For instance, the test cases for box model support and CSS fixed positioning support require DOM node injection, this means the DOM needs to be at least partially loaded. Currently, we poll for the body element and once found execute a method to inject the nodes into the DOM and complete the test cases. However due to inconsistent and unreliable results and conflicts in execution order with the load event listener of the window and onDOMready-related methods, Env.onReady is provided to manually execute the method and finish any DOM related capability tests.</p>
<p>It should be noted that for the most part, I intentionally disregarded any feature tests that can otherwise be detected with simple object or typeof inference. Don&#8217;t expect objects or methods such as addEventListener or querySelectorAll to be supported, this suite is exclusive to capabilities tests that are typically difficult to detect.</p>
<h3>Bugs</h3>
<p>Although they are few and relatively easy to avoid, being able to harness the native capabilities of the browsers that do not possess a flawed implementation or bug versus those that do can work wonders in terms of ease and performance. For example, if you ever wanted to know if your browser supports DOM injection via innerHTML for table or select elements &#8211; now you do, and can fully exploit the faster technique for each browser. In addition to that, bugs pertaining to getElementById, getElementsByTagName, querySelectorAll, and getElementsByClassName are also all supported.</p>
<h3>Type Detection</h3>
<p>As a means of determining exactly what a specific object is at any given time, <em>Env</em> supports advanced type detection for a wide variety of objects including arrays, functions, elements, numbers, and strings, just to name a few. However, aside from the more rudimentary objects, there is also support for the much more unique objects such as nodelists, events, windows, and native methods. This allows for a definitive and reliable test for almost all objects you will encounter within the JavaScript environment:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Env.<span style="color: #006600;">isArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// true</span>
Env.<span style="color: #006600;">isNodeList</span><span style="color: #000000;">&#40;</span>element.<span style="color: #006600;">childNodes</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// true</span></pre></div></div>

<p>In addition to that, <em>Env.isEmpty</em> is a very useful method that can be used to test if a variable holds a value. Inspired by PHP&#8217;s <em>empty</em> function, <em>Env.isEmpty</em> can be tested against strings, numbers, booleans, arrays, nodelists, objects, and null and undefined values:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Env.<span style="color: #006600;">isEmpty</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// true</span>
Env.<span style="color: #006600;">isEmpty</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">&quot;   &quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// true</span></pre></div></div>

<p>Of course, what would be a feature testing library without the three methods made famous by <a href="http://peter.michaux.ca/" target="_blank">Peter Michaux</a>&#8217;s classic article on <a href="http://peter.michaux.ca/articles/feature-detection-state-of-the-art-browser-scripting" target="_blank">feature detection</a>. <em>Env.isHostMethod</em>, <em>Env.isHostCollection</em>, and <em>Env.isHostObject</em> are three helpful methods designed to determine if a member of a host is what you think it is:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Env.<span style="color: #006600;">isHostObject</span><span style="color: #000000;">&#40;</span>element<span style="color: #000000;">,</span> <span style="color: #3366CC;">'firstChild'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// true</span>
Env.<span style="color: #006600;">isHostMethod</span><span style="color: #000000;">&#40;</span>element<span style="color: #000000;">,</span> <span style="color: #3366CC;">'appendChild'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// true</span>
Env.<span style="color: #006600;">isHostCollection</span><span style="color: #000000;">&#40;</span>element<span style="color: #000000;">,</span> <span style="color: #3366CC;">'childNodes'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// true</span></pre></div></div>

<p>Finally, what might be the most innovative method to the library is <em>Env.type</em> which is quite simply a much greater expansion of <em>the typeof</em> operator. Now you have the ability of knowing exactly what any object is by simply supplying it to this method and receiving a string representation of its type:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Env.<span style="color: #006600;">type</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #000000;">,</span> <span style="color: #CC0000;">2</span><span style="color: #000000;">,</span> <span style="color: #CC0000;">3</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// array</span>
Env.<span style="color: #006600;">type</span><span style="color: #000000;">&#40;</span>element.<span style="color: #006600;">childNodes</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span> <span style="color: #009900; font-style: italic;">// nodelist</span></pre></div></div>

<p>Ultimately, if no match is found for the object provided, the method will default to the <em>typeof</em> operator.</p>
<h3>Event Support</h3>
<p>One of the most difficult aspects to feature testing is determining support for event listeners of specific elements. Whether it be mutation events, contextmenu, or IE&#8217;s much desired mousenter and mouseleave; there is a wealth of varying event support amongst the respective browsers with no standard means of detecting support for them, which ultimate leads many developers to browser sniffing.</p>
<p><em>Env.support.event</em> aims to fix this by providing a reliable test case suited for all modern day browsers. Testing for event support is simple:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Env.<span style="color: #006600;">support</span>.<span style="color: #006600;">event</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
Env.<span style="color: #006600;">support</span>.<span style="color: #006600;">event</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'DOMNodeInserted'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>By default, all event types are tested against a standard div element created within the context of a method call. As an optional second parameter, you can specify element inference by providing either a tag name (which will be used to create the element), or a raw element itself:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Env.<span style="color: #006600;">support</span>.<span style="color: #006600;">event</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'abort'</span><span style="color: #000000;">,</span> <span style="color: #3366CC;">'img'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
Env.<span style="color: #006600;">support</span>.<span style="color: #006600;">event</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'load'</span><span style="color: #000000;">,</span> window<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>Currently, the only mutation events supported are DOMNodeInserted, DOMNodeRemoved, DOMAttrModified, and DOMSubtreeModified, none of which are actually tested for within the context of a <em>Env.support.event</em> method call, but rather the context of <em>Env</em> on initial load and are available as part of the <em>Env.support</em> namespace (<em>Env.support.DOMNodeInserted</em>).</p>
<p>Much of the credit for the techniques utilized within this method goes to <a href="http://thinkweb2.com/projects/prototype/">Juriy Zaytsev</a> and his recent post concerning <a href="http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/" target="_blank">event support detection</a> along with the talented developers involved in the <a href="http://ajaxian.com/archives/detecting-event-support-in-browsers" target="_blank">discussion over at Ajaxian</a> for all the information and inspiration, thanks!</p>
<h3>CSS Style Support</h3>
<p>With all the revolutionary things we&#8217;re seeing with CSS these days including transforms, transitions, rounded-corners, and the truly awesome effects such as gradients and reflectors coming out of webkit, why the hell are we still stuck looking for support for opacity, min-width, or fixed positioning. Wouldn&#8217;t it be nice to harness these latest CSS styles without resorting to browser detection or some nasty hacks?</p>
<p>Well wait no longer,  if you&#8217;re simply looking to determine support for a CSS property, just call the method and get your answer &#8211; no browser sniffing, no elaborate test case, no false positives, nothing but a definitive result, what else could you ask for?</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Env.<span style="color: #006600;">support</span>.<span style="color: #006600;">style</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'min-width'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
Env.<span style="color: #006600;">support</span>.<span style="color: #006600;">style</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'-moz-border-radius-topleft'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>As if that wasn&#8217;t enough, you can also determine support for a specific style&#8217;s assignable values, such as fixed positioning. By simply providing the value as an optional second argument, you now have a 100% reliable test case for not only the CSS styles themselves but their supported values:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Env.<span style="color: #006600;">support</span>.<span style="color: #006600;">style</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'position'</span><span style="color: #000000;">,</span> <span style="color: #3366CC;">'fixed'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
Env.<span style="color: #006600;">support</span>.<span style="color: #006600;">style</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'-webkit-transform'</span><span style="color: #000000;">,</span> <span style="color: #3366CC;">'2ex'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>Now were golden, so go start using those CSS styles you&#8217;ve longed to try! For a detailed description of how this method works, see the post here; <a href="http://ryanmorr.com/archives/detecting-browser-css-style-support">http://ryanmorr.com/archives/detecting-browser-css-style-support</a>.</p>
<h3>Example</h3>
<p>To see a simple example of all the tests available within the <em>Env.support</em> and <em>Env.bugs</em> namespaces, try clicking <a href="http://www.ryanmorr.com/tests/env/" target="_self">here</a> and see how your browser stacks up!</p>
<h3>Summary</h3>
<p>My greatest hope here that the community of developers will realize (if they haven&#8217;t already) where and when feature detection can and should be utilized. It has always been my contention that browser detection has no place within a script, not now and not ever. Despite some long dilemmas and unique workarounds, I for one am still waiting for that immense problem that can only be solved via browser detection &#8211; in three years of developing in Javascript (which I know isn&#8217;t very long) I haven&#8217;t encountered anything to that effect yet, and <em>Env</em> will help to ensure that I never do. So this is it people, no more excuses, defending browser detection is like defending gas-powered cars &#8211; it may be faster, but the world is moving to a much cleaner and environmentally safer approach, or at least I hope they are!</p>
<p>Please report any bugs or false positives you may experience, and if you have any suggestions of bugs or features to test for in future versions, I would love to hear them.</p>
<h3>Download</h3>
<ul>
<li><a href="http://ryanmorr.com/modules/env-compressed.js">Compressed (11kb)</a></li>
<li><a href="http://ryanmorr.com/modules/env.js">Fully Documented (27kb)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://ryanmorr.com/archives/env-feature-testing/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Browser Detection: Necessary or Negligent?</title>
		<link>http://ryanmorr.com/archives/brower-detection-necessary-or-negligent</link>
		<comments>http://ryanmorr.com/archives/brower-detection-necessary-or-negligent#comments</comments>
		<pubDate>Sun, 29 Mar 2009 22:27:28 +0000</pubDate>
		<dc:creator>Ryan Morr</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Event]]></category>

		<guid isPermaLink="false">http://ryanmorr.com/?p=819</guid>
		<description><![CDATA[It is still a controversial issue today despite the recent evolutions in the industry including new modern browsers and new resources such as jQuery (as of version 1.3) that now entirely base their implementations on feature testing. However, somehow the practice of browser detection seems to still be alive and well, whether it be new [...]]]></description>
			<content:encoded><![CDATA[<p>It is still a controversial issue today despite the recent evolutions in the industry including new modern browsers and new resources such as jQuery (as of version 1.3) that now entirely base their implementations on feature testing. However, somehow the practice of browser detection seems to still be alive and well, whether it be new browsers bringing about new bugs, features that are difficult to detect, or simply shortcuts spawned out of pure laziness, the practice is not only being used but defended. Browser sniffing can be found around just about every corner you look &#8211; from the smallest of websites, to your favourite JavaScript library (Mootools, Prototype, YUI, etc.) utilized in corporate giants such as Chrysler, Gamespot, and Yahoo!<span id="more-819"></span></p>
<h3>What is Browser Detection?</h3>
<p>Browser detection or sniffing is the practice of detecting the browser runtime environment in which JavaScript is being executed. This helps to restrict execution of certain fragments of code to that browser based on features or bugs that are believed to be associated with that specific browser. This exercise is not only restricted to the type of browser (IE, Firefox, Opera, Safari, etc.) but also the browser version and the operating system. The most common method of detecting the browser is via the <em>userAgent</em> string of the <em>navigator</em> object, for example, the following is the output of the <em>userAgent</em> string of your browser:</p>
<p><script type="text/javascript"><!--
document.write(navigator.userAgent);
// --></script></p>
<p>As you can tell, you will be required to parse that string to get a definitive browser and version out of it.  Some developers will choose to ignore the user agent electing instead to facilitate unrelated object inference to detect the browser, such as ActiveXObject for Internet Explorer, despite common belief this should not be considered object detection.</p>
<p>The goal of browser detection is too provide a simple method in which to detect a browser&#8217;s native feature or method support (XMLHttpRequest, ActiveX, etc.), object support (event.clientX, event.pageX, etc.), and most importantly bugs (IE can confuse id and name attributes when using document.getElementById).</p>
<h3>The Dangers</h3>
<p>The practice of browser detection is nothing new and from the perspective of a relatively new developer to the language, it is a justified solution to obtain cross-browser compatibility which can be so difficult to achieve. However at some point along the way, the exercise has been abused, quickly becoming common routine rather than a last resort, finding its way into scripts where simple object detection would suffice.</p>
<p>The fundamental problem with browser detection in any capacity is the inherent dependency on what is widely believed to be true associations between a browser and a bug or feature. In other words, you are making an assumption about every browser of every user that visits the page, and assuming every feature and bug that is being tested for is a correct affiliation with that browser. These assumptions as always can be very dangerous, your application as a whole may just depend on it.</p>
<p>The flaw in browser detects themselves are their static nature, whether your testing the user agent or relying on a specific object  (ActiveXObject) to identify the browser, a static browser detect is only as reliable as your knowledge of the browser environments you currently support at the time of implementation. As browsers continue to evolve, new browser and browser versions released, and API&#8217;s updated, your application will become a fragile implementation at the mercy of the evolution. What were previously thought to be true associations between a browser and a feature or bug are now false and inconsistent.</p>
<p>Browser spoofing is an issue developers have been tackling ever since the days of Internet Explorer and Netscape Navigator. What is browser spoofing? Well, quite simply it is the ability for one browser to attempt to imitate another via the <em>userAgent</em> string. If you re-examine the example I provided above of what your user agent is, you may notice (depending on the browser your using) hints of other browsers. A quick test in the various browsers for me reveals words such as &#8220;Mozilla&#8221; in IE7 and  &#8220;Gecko&#8221; and &#8220;Safari&#8221; in Chrome. This makes it very difficult to create a reliable and consistent browser detect, requiring a developer to constantly change their methods of browser detection. To view the history of the user agent string and browser spoofing, see <a href="http://webaim.org/blog/user-agent-string-history/" target="_blank">http://webaim.org/blog/user-agent-string-history/</a></p>
<p>The ultimate goal of any application is to increase and maintain traffic, this starts with attempting to support as many browsers as possible. Typically, browser detection infers a specific list of supported browsers as is the case with most JavaScript libraries. However, this not only isolates your browser support but also alienates alternative browsers and browser versions that may otherwise be fully capable of supporting your application. Upgrading your browser version may be all that is required to break the browser sniff and all subsequent dependencies. Ideally, you should aim to support any browser that supports only the features your application utilizes, and offers a graceful degradation for those that do not.</p>
<p>It should also be noted that the greatest discrepancies between browsers is in rendering of the web page, which is not so much a JavaScript issue but a CSS and markup issue, so lets keep it that way. Making adjustments to various measured CSS properties such as width and height in JavaScript based on the type of browser should not be necessary. Of course there are the well known asterisk and underscore hacks, as well as many other browser specific workarounds in CSS, but that is another topic for another day.</p>
<h3>Feature Testing</h3>
<p>The ultimate solution and alternative to browser detection is feature testing, the simple means of testing for a &#8220;feature&#8221; before utilizing it in some capacity. The technique is often applied as direct object, method, or typeof inference as it relates to the feature within the browser environment during runtime. Ultimately, the reliability of such a test case lies solely in it&#8217;s ability to execute successfully and provide a definitive boolean value in which to compare against. However, there is a right way and wrong way in which to approach this technqiue, for instance, evaluting support for event handlers typically will involve a test case for standards compliant browsers such as Firefox and falls back to IE&#8217;s implementation if addEventListener is not supported:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> addEvent<span style="color: #000000;">&#40;</span>el<span style="color: #000000;">,</span> type<span style="color: #000000;">,</span> fn<span style="color: #000000;">&#41;</span>
    <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>el.<span style="color: #006600;">addEventListener</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        el.<span style="color: #006600;">addEventListener</span><span style="color: #000000;">&#40;</span>type<span style="color: #000000;">,</span> fn<span style="color: #000000;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
    <span style="color: #000000;">&#125;</span><span style="color: #003366; font-weight: bold;">else</span><span style="color: #000000;">&#123;</span>
        el.<span style="color: #006600;">attachEvent</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'on'</span> <span style="color: #000000;">+</span> type<span style="color: #000000;">,</span> fn<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>However, even a perfectly suited feature test as common as this is making an assumption about the user&#8217;s browser; if it doesn&#8217;t support addEventListener, it must be IE, so lets use the attachEvent implementation. Ideally, a test case should be used for each browser specific implementation, and provide a final fallback on the off chance of a failed test case in an alien browser:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> addEvent<span style="color: #000000;">&#40;</span>el<span style="color: #000000;">,</span> type<span style="color: #000000;">,</span> fn<span style="color: #000000;">&#41;</span>
    <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>el.<span style="color: #006600;">addEventListener</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        el.<span style="color: #006600;">addEventListener</span><span style="color: #000000;">&#40;</span>type<span style="color: #000000;">,</span> fn<span style="color: #000000;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
    <span style="color: #000000;">&#125;</span><span style="color: #003366; font-weight: bold;">else</span> <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>el.<span style="color: #006600;">attachEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        el.<span style="color: #006600;">attachEvent</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'on'</span> <span style="color: #000000;">+</span> type<span style="color: #000000;">,</span> fn<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
    <span style="color: #000000;">&#125;</span><span style="color: #003366; font-weight: bold;">else</span><span style="color: #000000;">&#123;</span>
        el<span style="color: #000000;">&#91;</span><span style="color: #3366CC;">'on'</span> <span style="color: #000000;">+</span> type<span style="color: #000000;">&#93;</span> <span style="color: #000000;">=</span> fn<span style="color: #000000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The only problem here is that the third case actually sets the event not adds to it, so it effectively removes previously bound handlers of that event type. However this can be quickly solved by caching event handlers of a specific event type, in which case addEventListener and attachEvent would be completely unnecessary, such is the case with <a href="http://dean.edwards.name/weblog/2005/10/add-event/" target="_blank">Dean Edwards&#8217; solution</a>. Ultimately, this method combined with an unobtrusive implementation will almost certainly provide a reliable application suited for both browsers that support the method and those that don&#8217;t via graceful degradation, including the crowd that has disabled JavaScript.</p>
<p>The wild card are the various bugs or quirks; supported implementations that either do not perform or behave as expected. More often than not, a bug exists within a host method, meaning the only way it can possibly be tested is to actually execute the method in a meaningful test case, for example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">try</span><span style="color: #000000;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> children <span style="color: #000000;">=</span> document.<span style="color: #006600;">documentElement</span>.<span style="color: #006600;">childNodes</span><span style="color: #000000;">;</span>
        <span style="color: #003366; font-weight: bold;">return</span> <span style="color: #009999;">Array</span>.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">slice</span>.<span style="color: #006600;">call</span><span style="color: #000000;">&#40;</span>children<span style="color: #000000;">&#41;</span> <span style="color: #003366; font-weight: bold;">instanceof</span> <span style="color: #009999;">Array</span><span style="color: #000000;">;</span>
    <span style="color: #000000;">&#125;</span><span style="color: #003366; font-weight: bold;">catch</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
    <span style="color: #003366; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #000000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This particular test determines support for a native nodelist to array conversion. The method utilizes a try-catch block which typically should be avoided, primarily because in a context such as this, an assumption is being made about what it means when a test fails and the exception is caught. We assume that the exception was thrown and the method returned false because the browser doesn&#8217;t support native array conversions. However the method would return the same if an error occurred due to a bug else where in the try-catch block, this makes for fragile code in future browser environments. Although the solution is not ideal, it is the best JavaScript will allow us do at this point in time so it will have to do.</p>
<p>Despite difficult and often cumbersome, there is almost always a test case available in some capacity capable of determining browser support. To view more common bugs as well as their respective test cases, have a look at <a href="http://yura.thinkweb2.com/cft/" target="_blank">Juriy Zaytsev&#8217;s fantastic resource</a>.</p>
<p>Feature testing isn&#8217;t the perfect solution &#8211; there is no perfect solution, however from a practical standpoint, it is your best bet to ensure a reliable code base. As it relates to testing bugs, the dependency in the test will lie solely in the method in which you test the feature. The bottom line is if there is one thing that a developer should know; it&#8217;s the environment that he or she is dealing with. It is of far greater importance that a developer know the bugs and how to individually avoid them than to lump them all together based on assumptions in an eventual failure. To read more about feature testing, see <a href="http://peter.michaux.ca" target="_blank">Peter Michaux</a>&#8217;s excellent and all inclusive article on <a href="http://peter.michaux.ca/articles/feature-detection-state-of-the-art-browser-scripting" target="_blank">feature detection</a>.</p>
<h3>Summary</h3>
<p>Feature testing may not be simple and in some cases may require an elaborate and complicated test case, however it is the obvious answer. As oppossed to browser detection, it doesn&#8217;t draw on associations, assumptions, or false positives that are believed to be true affilations with a specific browser to create fragile test cases. Instead, feature testing relies on what it knows about the browser environment during runtime, not the precieved browser support at the time of implementation. The philosophy here is simple; if your unsure that a specific object/method is supported in the native environment or performs as expected, try it and let the test case tell you &#8211; don&#8217;t tell it. Resorting to browser detection should no longer be considered a last resort, but bad practice.</p>
]]></content:encoded>
			<wfw:commentRss>http://ryanmorr.com/archives/brower-detection-necessary-or-negligent/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Library or no Library, That is the Question!</title>
		<link>http://ryanmorr.com/archives/library-or-no-library-that-is-the-question</link>
		<comments>http://ryanmorr.com/archives/library-or-no-library-that-is-the-question#comments</comments>
		<pubDate>Sun, 08 Mar 2009 08:41:55 +0000</pubDate>
		<dc:creator>Ryan Morr</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Event]]></category>
		<category><![CDATA[FX]]></category>

		<guid isPermaLink="false">http://ryanmorr.com/?p=720</guid>
		<description><![CDATA[It is rarely even a question anymore; deciding whether to use a JavaScript library on your next project typically goes without saying, the real question developers find themselves asking is which library should it be? However the decision to adopt any framework for a project should not be taken lightly and deserves a proper evaluation [...]]]></description>
			<content:encoded><![CDATA[<p>It is rarely even a question anymore; deciding whether to use a JavaScript library on your next project typically goes without saying, the real question developers find themselves asking is which library should it be? However the decision to adopt any framework for a project should not be taken lightly and deserves a proper evaluation of requirements before making a decision based on assumptions. Often a library may ask of a developer, &#8220;Does the JavaScript library help me to write JavaScript?&#8221;, but shouldn&#8217;t they ask if the library helps them to learn JavaScript? Learning the syntax of your favourite JavaScript library does nothing for the developer&#8217;s understanding of the language itself or the impact on performance, memory, and reliability of their implementations. For this article, I will be looking at both sides of the argument, but hiding my bias will not be easy, my hope here is that perhaps a few developer&#8217;s may rethink their approach.<span id="more-720"></span></p>
<h3>Why use a JavaScript Library?</h3>
<p>The driving force behind JavaScript libraries and their very rise in popularity is based on two things; cross-browser compatibility and the lack of various native implementations. They create an easy-to-use implementation which helps to normalize behaviour across browsers and add additional functionality, specifically as it relates to the the four core aspects of most JavaScript libraries; event handling, animation, DOM (manipulation and traversal), and AJAX.</p>
<p>Additionally, some libraries also feature, as a plugin or as part of the core, various user interface (UI) components and widgets, most notably jQuery UI, YUI, Digit (Dojo), and Ext.</p>
<p>The typical reasoning as to why a developer adopts any JavaScript library for their project falls back to the common expression; why reinvent the wheel?</p>
<h3>The Criteria</h3>
<p>Evaluating a JavaScript library is no different than an evaluation of your own code, after all, once implemented, you will assume responsibility for it. Performance, compatibility, and bugs will immediately become your problem &#8211; it is you who will hear the complaints of your end users, not the authors of the third party software you&#8217;ve decided to utilize. Therefore, it is important to understand the flaws inherent in all JavaScript libraries and to properly justify your choice of implementation for a specific project.</p>
<p>With that in mind, it is important to outline the criteria when considering which framework to adopt or whether or not to use one at all. Much of the criteria including the weight of each measure will be specific to the project in question, most of which stemming from the scale of the project and the desired functionality. Does your project require certain UI components such as a grid, toolbar, or tabbed interface? Will my application require support for HTML, XML, SVG, flash, or canvas? These are just a few of the questions that should be evaluated on a case by case basis. In addition to that, there are several general questions a developer should ask of himself and the pending project that are relevant to any and all projects:</p>
<ul>
<li>the ease of implementation/updates (API)</li>
<li>browser support</li>
<li>file size (bytes)</li>
<li>documentation</li>
<li>performance</li>
<li>extensibility</li>
<li>robustness and modularity</li>
<li>stability and reliability</li>
<li>the developer&#8217;s overall capacity to understand and progress in their understanding of their implementations</li>
</ul>
<p>The last point is especially important and should not be glossed over, dependency is something you should steer away from &#8211; to understand all the code is to know how to fix/improve it, even if it isn&#8217;t your code.</p>
<h3>Advantages</h3>
<p>Without question, a well developed JavaScript library has many advantages over the alternative and provides the developer a simple layer of abstraction on which to develop their application.</p>
<p>One of the primary advantages to a JavaScript framework is the ease and simplicity of development and implementation. Whether it be prototypical/classical inheritance, CSS selectors, animation and effects, or creating a wrapper class for things like elements, AJAX requests, or what have you, a library typically allows you to make quick implementations in little time and little bytes.</p>
<p>As I mentioned above, cross-browser compatibility and normalization is perhaps the greatest attribute of any library. The developer need not worry about the flawed implementations of the various browsers or what functionality is supported or isn&#8217;t, let the library worry about that.</p>
<p>If you are to adopt any third-party software, well documented code and a highly adaptive API are key. Although it is completely dependent on the framework at hand, typically a library will supply solid documentation set with examples.</p>
<p>Plugins, extensions, and user interface widgets are just a few of the byproducts of a healthy and active community. Extensibility plays a large role in developing and maintaining a community of developers for a specific library, and nobody does this better than jQuery. The library boasts an impressive amount of plugins ranging anywhere from drag-and-drop and layouts to data stores and media. With a community as active as that, virtually anything you could ever want is already available and ready for implementation.</p>
<p>Most of the JavaScript libraries available today are completely open-sourced, including; jQuery, Mootools, Prototype, YUI, Dojo, Ext, and more. Not only does this help to create an active community but can also help a developer in understanding how the framework implemented a certain feature by simply viewing the source and reading the documentation. It proves to be a very useful resource for anyone looking to learn and understand advanced JavaScript.</p>
<h3>Disadvantages</h3>
<p>At some point in the last four or five years, as the library revolution started to take over, too much attention has been placed on the positives and not enough focused on the negatives. These libraries are often portrayed as perfect, but they are not, and you should understand all the disadvantages inherent in their adoption for a project.</p>
<p>First and foremost, browser detection is a common problem amongst JavaScript libraries, and virtually all still utilize it. There is no feature/bug that can&#8217;t be detected, tested for, or worked around in some manner. Sacrificing bytes for shortcuts should never be permitted especially amongst the very best the industry has to offer. The goal should be to try and support as many browsers as possible, otherwise you could be turning away potential visitors without even knowing it. I would be remissed if I didn&#8217;t mention that jQuery (as of version 1.3) no longer utilizes browser sniffing as part of the base library, however many plugins still do!</p>
<p>Bugs are a common problem with any code, however when it isn&#8217;t your code and you don&#8217;t understand the implementations, your limited in your ability to resolve the bug. Such is the case with JavaScript libraries, a developer may find himself waiting for a new version or update of a specific framework to solve his bug as visitors continue to encounter the problem. Once that new version is released and implemented, he better cross his fingers and hope it gels well with not only his code, but any other plugins that also rely on that particular library. Often a library will release a version that unexpectedly drops support for specific methods, properties, and/or classes, forcing plugin developers to update their versions (which you will also have to wait for) unless you try and do it yourself.</p>
<p>Another common problem various JavaScript libraries internally struggle with is the battle between usability/simplicity (API) and performance. The layer of abstraction your working with may be on such a high level that the length and intensity of each method call is slower than it otherwise should be, specifically as it relates to DOM manipulation. For instance, jQuery contains all currently selected elements within an internal array that must be looped through on each method call, whether the array contains 100 items or 1 or 2, this creates unnecessary overhead. Although it is true that libraries consistently improve in this regard, performance still lacks where typical manual DOM manipulation excels, especially in IE. Just look at these performance results conducted by <a href="http://webreflection.blogspot.com/2009/04/taskspeed-dom-vs-libraries.html">Anrea Giammarchi </a>in a test that pits the various JavaScript libraries against the DOM.</p>
<p>A JavaScript framework is often guilty of trying to do too much, resulting in a bloated library of methods and classes that are rarely utilized, trying to answer questions nobody has asked.  Whats worse, is the encouragement and support for bad programming, trying to normalize behaviour not only amongst browsers but developers too. Of course, this results in a larger file size full of unused code &#8211; wasted bytes using unnecessary bandwidth. On a related note, many developers will use a JavaScript library solely for its support of a specific plugin, such as a lightbox or date picker. This requires them to import a large framework the majority of which is never utilized and therefore never justified.</p>
<p>As I mentioned before, the adoption of any JavaScript library does not progress a developer in the understanding of the language or the various implementations. It creates a certain level of dependence on methods, classes, and plugins that may not be there forever, as you fall at the mercy of the third party software. Sooner or later, the JavaScript language along with browser support will grow and improve, JavaScript libraries will quickly become obsolete in that environment, and any developers who rely on them will be left out in the cold &#8211; with their app of course.</p>
<h3>The Solution</h3>
<p>Ultimately the solution lies in learning the language and not a library, to know the bugs, the browser inconsistencies, and the supported implementations and knowing what to do about it. However, I realize that it is a time consuming goal that may not be realistic for the short term, that is why I propose a middle man; the module, think of it as a plugin for the language not a library.</p>
<p>Perhaps its a lack of support for lightweight and standalone modules that have driven people to the all inclusive framework and the various plugins that lie on top of them, in part this blog is dedicated to fill that void. Still third party software, a module however is small and concise and serves only one purpose, whether it be DOM manipulation, event handling, AJAX requests, or what have you. This makes for a much simpler approach and therefore easy debugging, not to mention, the author is more likely to update with greater frequency, plus no pesky dependencies.</p>
<p>Naturally, as many modules are thrown together, cohesion is likely to be effected resulting in code duplication and loose coupling, that is where you come in. Make it cohesive, merge the modules and create something custom for each project, your not reinventing the wheel but your also not relying on someone else&#8217;s. Take the tire, the rim, nuts and bolts and put it together yourself! You might learn something in the process!</p>
<p>Development is about one thing; to have a better understanding of what is going on behind the scenes, know your implementations and understand their impacts. The ultimate goal is the satisfaction of your end-user, and at the end of the day reliability should be your number one concern. For this, the answer is clear.</p>
]]></content:encoded>
			<wfw:commentRss>http://ryanmorr.com/archives/library-or-no-library-that-is-the-question/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Class Types and Invariants</title>
		<link>http://ryanmorr.com/archives/class-types-and-invariants</link>
		<comments>http://ryanmorr.com/archives/class-types-and-invariants#comments</comments>
		<pubDate>Mon, 23 Feb 2009 00:57:04 +0000</pubDate>
		<dc:creator>Ryan Morr</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[Experiment]]></category>

		<guid isPermaLink="false">http://ryanmorr.com/?p=550</guid>
		<description><![CDATA[Right off the bat, I should say I don&#8217;t expect this project to be used in any real-world applications, as the amount of overhead is substantial and performance could suffer, so use at your own risk. Instead, this project is about demonstrating the versatility of JavaScript; it is common practice these days to build on [...]]]></description>
			<content:encoded><![CDATA[<p>Right off the bat, I should say I don&#8217;t expect this project to be used in any real-world applications, as the amount of overhead is substantial and performance could suffer, so use at your own risk. Instead, this project is about demonstrating the versatility of JavaScript; it is common practice these days to build on top of the language and extend upon its native capabilities due to a lack of support for various functions and implementations found in other languages. This has lead to many of the countless hacks and extensions found in most modern day libraries, such as inheritance, native object extensions (Array), dynamic script injection, animation, and CSS selector engines to name a few. Now you can add one more to the list; class type and invariant declaration and validation.<span id="more-550"></span></p>
<h3>Aurora</h3>
<p>The goal of the project is to provide a means of defining instance variables bound to a specific data type as you would find in Java or a variety of other languages. All subsequent calls to the methods of the instance result in automatic variable validation after the method call is complete. If a type violation is found, a descriptive exception is thrown and that specific variables value will be reverted back to what it was before the method call took place.</p>
<p>In addition to that, there is added support for class invariants, inspired by <a href="http://www.eiffel.com/" target="_blank">Eiffel</a>, each class can declare a set of invariants for each instance variable which outlines the rules of that variable. For example, a variable called month in the class calendar must be an integer between the values of 1 and 12, if that rule is broken an exception is thrown.</p>
<h3>Data Types</h3>
<p>By default, there are 12 declared data types; String, Date, Number, RegExp, Function, Boolean, Array, Object, Null, Element, Event, and NodeList. In addition to that, a variable can not only be declared as type Array, but the contents of the Array can also be constrained to a specific data type:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">items<span style="color: #000000;">:</span> <span style="color: #000000;">&#91;</span><span style="color: #009999;">Object</span><span style="color: #000000;">&#93;</span>
collection<span style="color: #000000;">:</span> <span style="color: #009999;">Array</span><span style="color: #000000;">&#40;</span>Element<span style="color: #000000;">&#41;</span></pre></div></div>

<p>Also, any declared class will also automatically become a declared data type, meaning a class can define a variable to have the data type of another class. For example, a Menu class may have a variable assigned to hold an Array of instances of the class Menu.Item:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">menuItems<span style="color: #000000;">:</span> <span style="color: #000000;">&#91;</span>Menu.<span style="color: #003366; font-weight: bold;">Item</span><span style="color: #000000;">&#93;</span></pre></div></div>

<p>The define method allows you to add your own additional custom data types for the purpose of variable declaration. The method requires only two arguments; the name of the new data type as a string which does support namespacing and a function that will be used to validate all variables against.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Aurora.<span style="color: #006600;">define</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'Hash'</span><span style="color: #000000;">,</span> Aurora.<span style="color: #006600;">isHash</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
Aurora.<span style="color: #006600;">define</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'Integer'</span><span style="color: #000000;">,</span> Aurora.<span style="color: #006600;">isInteger</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
Aurora.<span style="color: #006600;">define</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'Decimal'</span><span style="color: #000000;">,</span> Aurora.<span style="color: #006600;">isDecimal</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>Without initialing them, there is a variety of functions available to allow you to easily add additional support for a variety of different data types. Almost all objects often encountered within Javascript have an associated validator included in Aurora to enhance the data type declarations, validators include:</p>
<ul>
<li>Aurora.isHash</li>
<li>Aurora.isInteger</li>
<li>Aurora.isDecimal</li>
<li>Aurora.isXML</li>
<li>Aurora.isXMLDocument</li>
<li>Aurora.isXMLNode</li>
<li>Aurora.isWindow</li>
<li>Aurora.isDocument</li>
<li>Aurora.isNode</li>
<li>Aurora.isArrayLike</li>
<li>Aurora.isVoid</li>
<li>Aurora.isNative</li>
</ul>
<h3>Invariants</h3>
<p>The class invariant is used to set constraints on specific variables to prevent unwanted data from being assigned to an instance variable. All invariants are defined in an invariant object, each invariant is assigned the same name as the variable they represent and are to constrain. They must take the form of a function and it also must return a boolean; true if the variable is valid and false if it is not. The following example demonstrates class invariants you might find in a calendar class:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">invariant<span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>
    day<span style="color: #000000;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">this</span>.<span style="color: #006600;">day</span> <span style="color: #000000;">&gt;=</span> <span style="color: #CC0000;">1</span> <span style="color: #000000;">&amp;&amp;</span> <span style="color: #003366; font-weight: bold;">this</span>.<span style="color: #006600;">day</span> <span style="color: #000000;">&lt;=</span> <span style="color: #CC0000;">31</span><span style="color: #000000;">;</span>
    <span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    month<span style="color: #000000;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">this</span>.<span style="color: #006600;">month</span> <span style="color: #000000;">&gt;=</span> <span style="color: #CC0000;">1</span> <span style="color: #000000;">&amp;&amp;</span> <span style="color: #003366; font-weight: bold;">this</span>.<span style="color: #006600;">month</span> <span style="color: #000000;">&lt;=</span> <span style="color: #CC0000;">12</span><span style="color: #000000;">;</span>
    <span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    year<span style="color: #000000;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">this</span>.<span style="color: #006600;">year</span> <span style="color: #000000;">&gt;=</span> <span style="color: #CC0000;">1</span> <span style="color: #000000;">&amp;&amp;</span> <span style="color: #003366; font-weight: bold;">this</span>.<span style="color: #006600;">year</span> <span style="color: #000000;">&lt;=</span> <span style="color: #003366; font-weight: bold;">new</span> <span style="color: #009999;">Date</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #006600;">getFullYear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h3>Classes</h3>
<p>The declare method is used to define a new class and has a similar syntax to the dojo.declare method. The method can accept either two or three arguments depending on whether the new class will inherit from a superclass. The first argument is a string name of the class which also supports namespacing, if there is a superclass it will become the second argument, otherwise the property object will follow. The property object is where the constructor, all declared data types, methods, and the invariant will be declared. Both the constructor and the invariant are not required, additionally, any properties that are not declared with a data type will be treated like a typical property and not subject to type checking.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Aurora.<span style="color: #006600;">declare</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'Clock'</span><span style="color: #000000;">,</span> <span style="color: #000000;">&#123;</span>
&nbsp;
    hour<span style="color: #000000;">:</span> <span style="color: #009999;">Number</span><span style="color: #000000;">,</span>
    minute<span style="color: #000000;">:</span> <span style="color: #009999;">Number</span><span style="color: #000000;">,</span>
    second<span style="color: #000000;">:</span> <span style="color: #009999;">Number</span><span style="color: #000000;">,</span>
&nbsp;
    constructor<span style="color: #000000;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span>hour<span style="color: #000000;">,</span> minute<span style="color: #000000;">,</span> second<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">this</span>.<span style="color: #006600;">hour</span> <span style="color: #000000;">=</span> hour<span style="color: #000000;">;</span>
        <span style="color: #003366; font-weight: bold;">this</span>.<span style="color: #006600;">minute</span> <span style="color: #000000;">=</span> minute<span style="color: #000000;">;</span>
        <span style="color: #003366; font-weight: bold;">this</span>.<span style="color: #006600;">second</span> <span style="color: #000000;">=</span> second<span style="color: #000000;">;</span>
    <span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
&nbsp;
    invariant<span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>
        hour<span style="color: #000000;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">this</span>.<span style="color: #006600;">hour</span> <span style="color: #000000;">&gt;=</span> <span style="color: #CC0000;">1</span> <span style="color: #000000;">&amp;&amp;</span> <span style="color: #003366; font-weight: bold;">this</span>.<span style="color: #006600;">hour</span> <span style="color: #000000;">&lt;=</span> <span style="color: #CC0000;">12</span><span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
        minute<span style="color: #000000;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">this</span>.<span style="color: #006600;">minute</span> <span style="color: #000000;">&gt;=</span> <span style="color: #CC0000;">0</span> <span style="color: #000000;">&amp;&amp;</span> <span style="color: #003366; font-weight: bold;">this</span>.<span style="color: #006600;">minute</span> <span style="color: #000000;">&lt;=</span> <span style="color: #CC0000;">59</span><span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
        second<span style="color: #000000;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">this</span>.<span style="color: #006600;">second</span> <span style="color: #000000;">&gt;=</span> <span style="color: #CC0000;">0</span> <span style="color: #000000;">&amp;&amp;</span> <span style="color: #003366; font-weight: bold;">this</span>.<span style="color: #006600;">second</span> <span style="color: #000000;">&lt;=</span> <span style="color: #CC0000;">59</span><span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<h3>Inheritance</h3>
<p>Classical inheritance is also supported and maintains the same behaviour and functionality that you would expect. However, in addition to the inherited methods and the ability to call superclass methods, both declared types and invariants of a superclass will also be inherited by all sublcasses. Any defined invariants with the same name as an invariant inherited by a sueprclass will automatically be overwritten. However, they maintain the ability to call the invariant of the superclass method for additional validation.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Aurora.<span style="color: #006600;">declare</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'Component'</span><span style="color: #000000;">,</span> <span style="color: #000000;">&#123;</span>
&nbsp;
    value<span style="color: #000000;">:</span> <span style="color: #009999;">Number</span><span style="color: #000000;">,</span>
&nbsp;
    constructor<span style="color: #000000;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span>value<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">this</span>.<span style="color: #006600;">value</span> <span style="color: #000000;">=</span> value<span style="color: #000000;">;</span>
    <span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
&nbsp;
    invariant<span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>
        value<span style="color: #000000;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">this</span>.<span style="color: #006600;">value</span> <span style="color: #000000;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
Aurora.<span style="color: #006600;">declare</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'Module'</span><span style="color: #000000;">,</span> Component<span style="color: #000000;">,</span> <span style="color: #000000;">&#123;</span>
&nbsp;
    name<span style="color: #000000;">:</span> <span style="color: #009999;">String</span><span style="color: #000000;">,</span>
&nbsp;
    constructor<span style="color: #000000;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span>name<span style="color: #000000;">,</span> value<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">this</span>.<span style="color: #006600;">name</span> <span style="color: #000000;">=</span> name<span style="color: #000000;">;</span>
        Module.<span style="color: #006600;">superclass</span>.<span style="color: #006600;">constructor</span>.<span style="color: #006600;">call</span><span style="color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;">this</span><span style="color: #000000;">,</span> value<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
    <span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
&nbsp;
    invariant<span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>
        name<span style="color: #000000;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">this</span>.<span style="color: #006600;">name</span> <span style="color: #000000;">!=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
        value<span style="color: #000000;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">this</span>.<span style="color: #006600;">value</span> <span style="color: #000000;">&lt;</span> <span style="color: #CC0000;">10</span> <span style="color: #000000;">&amp;&amp;</span> Module.<span style="color: #006600;">superclass</span>.<span style="color: #006600;">invariant</span>.<span style="color: #006600;">value</span>.<span style="color: #006600;">call</span><span style="color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;">this</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<h3>Exceptions</h3>
<p>Whenever a violation occurs, whether it be a type or invariant violation, an exception is thrown that indicates the type of violation, the class and method in which the violation occurred, the variable that failed the validation, and in the case of type violations the expected type. For example, a type violation in Firefox&#8217;s error console looks like so:</p>
<p><img class="alignnone size-full wp-image-640" title="type violation" src="http://ryanmorr.com/wp-content/uploads/2009/02/type.png" alt="type violation" width="535" height="35" /></p>
<p>Likewise, a violation on a variable with type Array with constrained contents will produce this error message:</p>
<p><img class="alignnone size-full wp-image-641" title="array violation" src="http://ryanmorr.com/wp-content/uploads/2009/02/array.png" alt="array violation" width="585" height="34" /></p>
<p>Finally, if any variables break the <span><span>contraints</span></span> set by its invariant, an exception will be thrown:</p>
<p><img class="alignnone size-full wp-image-642" title="invariant violation" src="http://ryanmorr.com/wp-content/uploads/2009/02/invariant-violation.png" alt="invariant violation" width="558" height="36" /></p>
<h3>Example</h3>
<p>The example demonstrates a typical type violation and the exception thrown when one occurs. Click <a href="http://ryanmorr.com/tests/aurora/aurora.html" target="_blank">here</a> to see it in action and be sure to open your error console.</p>
<h3>Summary</h3>
<p>As I mentioned in the beginning of the article, Aurora is a demonstration of the flexibility of JavaScript and not intended for serious use. Like many of the mainstream libraries available today, hacks and extensions are the name of the game, and there is almost nothing that can&#8217;t be simulated or imitated in one fashion or another. There is still much room for improvement within Aurora, the next most logical improvement would be adding validation to both method arguments and return values. No matter where Aurora goes from here, the most I can hope for is that other developers more creative and skilled than me can provide their own work of magic to the community.</p>
<h3>Download</h3>
<ul>
<li><a href="http://ryanmorr.com/modules/aurora-compressed.js">Compressed (7kb)</a></li>
<li><a href="http://ryanmorr.com/modules/aurora.js">Fully Documented (17kb)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://ryanmorr.com/archives/class-types-and-invariants/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Scope/Context in Javascript</title>
		<link>http://ryanmorr.com/archives/scope-context-in-javascript</link>
		<comments>http://ryanmorr.com/archives/scope-context-in-javascript#comments</comments>
		<pubDate>Mon, 09 Feb 2009 03:09:00 +0000</pubDate>
		<dc:creator>Ryan Morr</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Event]]></category>

		<guid isPermaLink="false">http://ryanmorr.com/?p=464</guid>
		<description><![CDATA[Scope or context as it is often referred to as is one of the fundamental aspects of Javascript, playing a significant role unlike that of any other language. Scope refers to the accessibility of variables and its execution context which belongs to the object or function in which it was defined. However this does not [...]]]></description>
			<content:encoded><![CDATA[<p>Scope or context as it is often referred to as is one of the fundamental aspects of Javascript, playing a significant role unlike that of any other language. Scope refers to the accessibility of variables and its execution context which belongs to the object or function in which it was defined. However this does not always hold true as scope can be lost or changed depending on how and where it used, specifically as it applies to functional execution.<span id="more-464"></span></p>
<p>Each invocation of a function establishes a unique execution context that lasts only for the duration of that function call. Each execution context appends its local scope to the scope chain in which it was defined. Name conflicts amongst variables within a specific context are resolved by climbing up the scope chain, moving locally to globally. This means that local variables with the same name as variables higher up the scope chain take precedence.</p>
<h3>Global/Local Scope</h3>
<p>A variable can be defined in either local or global scope, which establishes the variables&#8217; accessibility and context within the runtime environment. Any defined global variables, meaning any variables declared outside of a function body, live throughout the life of the page and can be accessed and altered in any context.</p>
<p>Local variables exist only within a function body of which they are defined. A local variable has a different execution context for every invocation of the function to which it belongs; it is subject for value assignment, retrieval, and manipulation only within that call and is non-accessible outside of that execution context.</p>
<h3>Losing Scope</h3>
<p>It&#8217;s a common problem developers find themselves dealing with when they delve into advanced techniques in Javascript, specifically as it refers to event handling and object orientation. Here the infamous <em>this</em> keyword plays an important role, but knowing what it is referring to at all times can be confusing.</p>
<p>Event handlers added using Internet Explorer&#8217;s attachEvent method causes a loss of scope when that event is fired and the callback is invoked, as the function is executed in the context of the window object. As a result, <em>this</em> will reference the window and not the element which registered the event handler as is the case with standards compliant browsers such as Firefox. Alternatively, when an event handler is added as an expando property (element.onclick), the callback will execute in the context of the element, even in IE. The reason for this is the scope chain, because the expando is a  property of the source element, it is appended at the top of the scope chain for each execution context. Meanwhile, due to a flaw in attachEvent, the window object is the only object associated with the callback and therefore is the only context available in the callback&#8217;s scope chain.</p>
<p>To better demonstrate this, try adding a function as a unique expando property and then execute it; <em>this</em> will now refer back to the source element:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> fn <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
    <span style="color: #003366;">alert</span><span style="color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;">this</span>.<span style="color: #006600;">id</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
<span style="color: #000000;">&#125;</span><span style="color: #000000;">;</span>
&nbsp;
element<span style="color: #000000;">&#91;</span><span style="color: #3366CC;">'e'</span><span style="color: #000000;">+</span>fn<span style="color: #000000;">&#93;</span> <span style="color: #000000;">=</span> fn<span style="color: #000000;">;</span>
element<span style="color: #000000;">&#91;</span><span style="color: #3366CC;">'e'</span><span style="color: #000000;">+</span>fn<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>Scope is also lost when dealing with object-orientation and event handling, which is not so much a flaw in the language but the implementation. Event handlers can only be added to DOM nodes which means (in standards-compliant browsers) <em>this </em>will always refer back to the source element, which is the way it should be. However if the callback happens to be an instance method, there is no way to properly call that method without altering scope. A quick solution to this is to declare a variable with the value of <em>this</em> and simply use a nested function to redirect the call to the desired method, 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> self <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">this</span><span style="color: #000000;">;</span>
element.<span style="color: #006600;">onclick</span> <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
    self.<span style="color: #006600;">instanceMethod</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>However, this can be cumbersome, the real solution involves what many believe to be one of the most useful techniques of the Javascript langauge; manual scope adjustment (more on than a little further down in the article).</p>
<h3>Closures</h3>
<p>One of the most powerful aspects to Javascript is the flexibility of functions; they can be sent as arguments, nested inside other functions, executed in any context desired, and encapsulate their own execution context. Closures can serve as quite the tool when scope becomes an issue as they can take on many different forms and can encapsulate and preserve a specific execution context for further manipulation.  In simplest terms, closures are variable functions local to another function that have the unique ability to retain their values between function calls. The most popular closure is what is widely known as the module pattern; it allows you to simulate public, private, and privileged members.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Module <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> privateProp <span style="color: #000000;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #000000;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> privateMethod <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span>args<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        <span style="color: #009900; font-style: italic;">//do something</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">return</span> <span style="color: #000000;">&#123;</span>
&nbsp;
        publicProp<span style="color: #000000;">:</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #000000;">,</span>
&nbsp;
        publicMethod<span style="color: #000000;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span>args<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
            <span style="color: #009900; font-style: italic;">//do something</span>
        <span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
&nbsp;
        privilegedMethod<span style="color: #000000;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span>args<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
            privateFunction<span style="color: #000000;">&#40;</span>args<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>The module acts as if it were a singleton executed as soon as the compiler interprets it (hence the opening and closing parenthesis at the end of the function). The only available members outside of the execution context of the closure are your public methods and properties located in the return object (ex. Module.publicMethod). However, all private properties and methods will live throughout the life of the application as the execution context is preserved, meaning variables are subject to further manipulation and execution via the public methods. Another type of closure is what I like to call a global closure which is nothing more than a self-invoked anonymous function executed in the context of the window:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #009900; font-style: italic;">//Use this or window to define a global variable</span>
    <span style="color: #003366; font-weight: bold;">var</span> Module <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">this</span>.<span style="color: #006600;">Module</span> <span style="color: #000000;">=</span> window.<span style="color: #006600;">Module</span> <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        <span style="color: #009900; font-style: italic;">//do something</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    Module.<span style="color: #006600;">publicProp</span> <span style="color: #000000;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #000000;">;</span>
    Module.<span style="color: #006600;">publicMethod</span> <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        <span style="color: #009900; font-style: italic;">//do something</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">var</span> privateProp <span style="color: #000000;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #000000;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> privateMethod <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        <span style="color: #009900; font-style: italic;">//do something</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>The global closure is most useful when attempting to preserve the global namespace as any variables declared within the function body will be local to the closure but will still live throughout runtime, often one namespace is enough for interaction outside of the closure.</p>
<h3>Call and Apply</h3>
<p>If you want to know how all the mainstream libraries do it, well it&#8217;s simple; call and apply. These two very simple methods inherent to all functions allow you to execute any function in any desired context. Before I continue, it is important to know that everything in Javascript inherently has its own context, including DOM nodes, objects, arrays, strings, functions, etc. Meaning, any function can be executed in the context of any object using call and apply. This may seem insignificant, but understand it is this implementation that serves as an underlying bridge between the object-oriented and functional methodologies in the Javascript language.</p>
<p>The call method contains an unspecified amount of arguments, the first is the object whose context the function is to be executed in, all subsequent arguments will serve as the arguments for the callback function in question. Apply works similarly with one exception, rather than supplying each argument individually, an array will be supplied that will automatically build the arguments for the function.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">method.<span style="color: #006600;">call</span><span style="color: #000000;">&#40;</span>scope<span style="color: #000000;">,</span> arg1<span style="color: #000000;">,</span> arg2<span style="color: #000000;">,</span> arg3<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
method.<span style="color: #006600;">apply</span><span style="color: #000000;">&#40;</span>scope<span style="color: #000000;">,</span> <span style="color: #000000;">&#91;</span>args<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>This is ultimately what should be used when referring to event handlers especially as it relates to object-orientation. One of the more popular functions used to quickly adjust context is the bind function, which utilizes closures by accepting the scope as well as arguments for the callback function and returning an anonymous function that behaves appropriately.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">Function</span>.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">bind</span> <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> method <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">this</span><span style="color: #000000;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> context <span style="color: #000000;">=</span> arguments<span style="color: #000000;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> args <span style="color: #000000;">=</span> <span style="color: #009999;">Array</span>.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">slice</span>.<span style="color: #006600;">call</span><span style="color: #000000;">&#40;</span>arguments<span style="color: #000000;">,</span> <span style="color: #CC0000;">1</span><span style="color: #000000;">&#41;</span>
    <span style="color: #003366; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        method.<span style="color: #006600;">apply</span><span style="color: #000000;">&#40;</span>context<span style="color: #000000;">,</span> args<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>It is most commonly used where context is most commonly lost; event handling as well as a few other implementations such as  setInterval and setTimeout. The following example demonstrates a callback method bound to a specific context for execution for each loop of an interval:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">setInterval<span style="color: #000000;">&#40;</span>callback.<span style="color: #006600;">bind</span><span style="color: #000000;">&#40;</span>obj<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>What is important to note here is that setInterval is not executing the bind function or the callback function. In fact it is the closure that was returned by the bind function that is being called on each loop of the interval. It promptly redirects the call to the callback function in the context that was supplied to the bind function: &#8220;obj&#8221;. While reviewing the source of the bind function, you may have also noticed what appears to be a relatively simple line of code involving a method of the Array object&#8217;s prototype:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> args <span style="color: #000000;">=</span> <span style="color: #009999;">Array</span>.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">slice</span>.<span style="color: #006600;">call</span><span style="color: #000000;">&#40;</span>arguments<span style="color: #000000;">,</span> <span style="color: #CC0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>What is interesting to note here is that the <em>arguments</em> object is not actually an array at all, however it is often described as an array-like object much like a nodelist (anything returned by getElementsByTagName or element.childNodes). They contain a length property and indexed values but they are still not arrays, and subsequently don&#8217;t support any of the native methods of array such as slice and push. However, because of their similar behaviour, the methods of Array can be hijacked and executed in the context of an array-like object as is the case above.</p>
<p>This technique also applies to the methods of classes; making it easy to execute a method of a prototype chain in the context of a another instance and have all properties and methods of that instance immediately recognized and subject to execution and manipulation. The following example demonstrates a method of the Module class calling a method of the Component class in the context of the Module instance. This is also what makes calls to a superclass possible when simulating inheritance in Javascript.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Module.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">method</span> <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
    <span style="color: #009900; font-style: italic;">//call another method of another class in the context of this instance</span>
    Component.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">method</span>.<span style="color: #006600;">call</span><span style="color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;">this</span><span style="color: #000000;">,</span> arg1<span style="color: #000000;">,</span> arg2<span style="color: #000000;">,</span> arg3<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h3>Summary</h3>
<p>Versatility is the name of the game here, in the attempt to try and do everything Javascript fails to present developers with a well integrated language, resulting in a very loose implementation. However, there is a method to the madness, and although these little quirks and workarounds are not ideal they are necessary if you want that efficient and effective app that we all strive for. All the techniques I&#8217;ve discussed here today are powerful ones, and they can work against you if implemented in the wrong fashion or the wrong place. Closures for example can reserve a lot of memory and ultimately cause memory leaks in IE. However, when done right, developing will never be easier and it will show, knowing your scope at all times is important and to know it is to utilize it, which will save you a world of headaches now and in the future.</p>
<h3>References</h3>
<ul>
<li><a href="http://www.digital-web.com/articles/scope_in_javascript/" target="_blank">http://www.digital-web.com/articles/scope_in_javascript/</a></li>
<li><a href="http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/" target="_blank">http://www.robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/</a></li>
<li><a href="http://www.jibbering.com/faq/faq_notes/closures.html" target="_blank">http://www.jibbering.com/faq/faq_notes/closures.html</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://ryanmorr.com/archives/scope-context-in-javascript/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FX; Lightweight and Standalone Animation</title>
		<link>http://ryanmorr.com/archives/fx-lightweight-and-standalone-animation</link>
		<comments>http://ryanmorr.com/archives/fx-lightweight-and-standalone-animation#comments</comments>
		<pubDate>Fri, 23 Jan 2009 01:57:25 +0000</pubDate>
		<dc:creator>Ryan Morr</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[FX]]></category>

		<guid isPermaLink="false">http://ryanmorr.com/?p=281</guid>
		<description><![CDATA[Its one of those unnecessary things, but it sure does look pretty, I&#8217;m of course talking about animation. Flash or Javascript based, I find it adds that little some extra for the end-user&#8217;s experience; allowing them to fully understand the actions taking place by visually seeing components in transition. Most all mainstream libraries support animation [...]]]></description>
			<content:encoded><![CDATA[<p>Its one of those unnecessary things, but it sure does look pretty, I&#8217;m of course talking about animation. Flash or Javascript based, I find it adds that little some extra for the end-user&#8217;s experience; allowing them to fully understand the actions taking place by visually seeing components in transition. Most all mainstream libraries support animation in one way or another, and they are all quite impressive, despite the fact the core of each library essentially works the same. <span id="more-281"></span></p>
<p>If your anything like me then what your really looking for is something standalone. After googling for an extensive amount of time, I think I found five pretty good ones: <a href="http://developers.facebook.com/animation/" target="_blank">Facebook animation</a>, <a href="http://coderepos.org/share/wiki/JSTweener" target="_blank">JSTweener</a>, <a href="http://aka-fotos.de/run/" target="_blank">RUN</a>, <a href="http://www.devpro.it/bytefx/" target="_blank">Bytefx</a>, and Bernard Sumption&#8217;s <a href="http://www.berniecode.com/writing/animator.html" target="_blank">Animator</a>. These are all great solutions and fairly lightweight tipping in somewhere between 15 and 25 kb (uncompressed), and also support most of the bells and whistles found in the major Javascript libraries. The only knock against them is well actually, only a knock against Facebook&#8217;s version, simply because they decided to sniff out Firefox in just one segment of code, close but no cigar.</p>
<p>Now with no further delay, I present to you my version appropriately named FX. The motive here was to create a simple script for your basic animations and of course to be library independent, with emphasis on lightweight. Where I have strayed away from the other solutions I mentioned above is that I&#8217;ve stripped out most of the extra features, electing instead for the core functionality. The syntax is similar to that which you would find in YUI, I find it simple yet covers all the bases pretty well. I drew inspiration for the programming behind it from Mootools due to its simplicity as a whole. To use it is pretty simple; just tell it what to do, give it a duration, optional callback, and then let it fly.</p>
<h3>Features</h3>
<ul>
<li>Perform tween on almost all CSS properties</li>
<li>Supports both scroll and color animations</li>
<li>Allows for both to and from values for each attribute</li>
<li>Supports transitional easing</li>
<li>Optionally assign a callback and context to execute the function in upon animation completion</li>
<li>Supports IE6+, FF, Opera, Safari, and Chrome</li>
<li>No browser detection</li>
</ul>
<h3>Example</h3>
<p>The syntax is pretty straigth forward, as I mentioned before it looks similar to what you would find in YUI. The following example demonstrates just some of the attributes supported for animation. For a more impressive sampling of FX, see this <a href="http://www.ryanmorr.com/tests/fx/" target="_self">example</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> fx <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FX<span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'element'</span><span style="color: #000000;">,</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #3366CC;">'top'</span><span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>to<span style="color: #000000;">:</span> <span style="color: #CC0000;">500</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    <span style="color: #3366CC;">'left'</span><span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>to<span style="color: #000000;">:</span> <span style="color: #CC0000;">300</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    <span style="color: #3366CC;">'width'</span><span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>from<span style="color: #000000;">:</span> <span style="color: #CC0000;">100</span><span style="color: #000000;">,</span> to<span style="color: #000000;">:</span> <span style="color: #CC0000;">300</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    <span style="color: #3366CC;">'height'</span><span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>from<span style="color: #000000;">:</span> <span style="color: #CC0000;">100</span><span style="color: #000000;">,</span> to<span style="color: #000000;">:</span> <span style="color: #CC0000;">200</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    <span style="color: #3366CC;">'scrollLeft'</span><span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>from<span style="color: #000000;">:</span> <span style="color: #CC0000;">0</span><span style="color: #000000;">,</span> to<span style="color: #000000;">:</span> <span style="color: #CC0000;">50</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span>
    <span style="color: #3366CC;">'background-color'</span><span style="color: #000000;">:</span> <span style="color: #000000;">&#123;</span>from<span style="color: #000000;">:</span> <span style="color: #3366CC;">'#FF0000'</span><span style="color: #000000;">,</span> to<span style="color: #000000;">:</span> <span style="color: #3366CC;">'#0000FF'</span><span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span><span style="color: #000000;">,</span> <span style="color: #CC0000;">1.5</span><span style="color: #000000;">,</span> callback<span style="color: #000000;">,</span> context<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
fx.<span style="color: #006600;">start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>At the moment, only pixels are supported as the default units for all animations, and there is only one type of transitional easing available. At some later point in time I will be updating to add support for both of these features, so check back soon!</p>
<h3>Download</h3>
<ul>
<li><a href="http://ryanmorr.com/modules/fx/fx-compressed.js">Compressed (4kb)</a></li>
<li><a onclick="javascript:pageTracker._trackPageview(’/Links/http://ryanmorr.com/modules/fx.js’);" href="http://ryanmorr.com/modules/fx/fx.js">Fully Documented (8kb)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://ryanmorr.com/archives/fx-lightweight-and-standalone-animation/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>onDOMReady: No Browser Sniffing!</title>
		<link>http://ryanmorr.com/archives/ondomready-no-browser-sniffing</link>
		<comments>http://ryanmorr.com/archives/ondomready-no-browser-sniffing#comments</comments>
		<pubDate>Mon, 12 Jan 2009 05:01:09 +0000</pubDate>
		<dc:creator>Ryan Morr</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Event]]></category>
		<category><![CDATA[Feature Testing]]></category>

		<guid isPermaLink="false">http://ryanmorr.com/?p=105</guid>
		<description><![CDATA[For the most part, I tend to rely on window.onload; its simple, cross-browser, and the alternative onDOMReady functions were unreliable because of the use of browser sniffing. Aside from that, a low byte size in your pages is not only relatively easy to attain, but it should also be kept in mind during development &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>For the most part, I tend to rely on window.onload; its simple, cross-browser, and the alternative onDOMReady functions were unreliable because of the use of browser sniffing. Aside from that, a low byte size in your pages is not only relatively easy to attain, but it should also be kept in mind during development &#8211; bandwidth can be a valuable thing. Thus, images shouldn&#8217;t result in a long load time and therefore should eliminate the very requirement of an onDOMReady related function. <span id="more-105"></span></p>
<p>Despite the debate over the very need of such a function, I decided to pursue it anyways out of pure curiosity. Once I committed to it and began coding I was fully expecting something to go wrong, after all, if all the major libraries couldn&#8217;t figure it out then there must not be a solution that doesn&#8217;t resort to browser detection, right? Unless of course they never even tried. To my surprise it was fairly simple, within 20 minutes I had a working version that was every bit as reliable as those found in YUI, Prototype, Mootools, etc. except no browser sniffing. Surely these talented developers (and I don&#8217;t doubt their skill) could have found a similar solution had they looked. The only exception to this is jQuery who I am very pleased to see have made great steps in completely removing browser sniffing with <a href="http://www.jquery.com" target="_blank">jQuery 1.3</a>, including in their use of the ready function for onload handling, credit to John Resig and the jQuery team. Regardless, I present to you in just 42 lines of code, a simple function to handle all your onload requirements with no browser sniffing!</p>
<h3>Features</h3>
<p>The function itself works similar to the other solutions available around the web including those found in the various Javascript libraries. However, rather than separating each routine via browser detection, I simply use one function to parse the document&#8217;s current state and determine if the DOM is ready for manipulation. For Firefox and Opera a simple check of event type will determine if it is DOMContentLoaded. Safari and IE will check against the document&#8217;s ready state, and alternatively IE will routinely try to scroll the document left, if successful &#8211; the DOM is ready. Finally in case all else fails, the onload event will bring up the rear. Upon completion, the supplied function is called and all event listeners are removed and nulled out to prevent memory leaks. Test it out <a href="http://www.ryanmorr.com/tests/ondomready">here</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> onDOMReady<span style="color: #000000;">&#40;</span>fn<span style="color: #000000;">,</span> ctx<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> ready<span style="color: #000000;">,</span> timer<span style="color: #000000;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> onChange <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>e <span style="color: #000000;">&amp;&amp;</span> e.<span style="color: #006600;">type</span> <span style="color: #000000;">==</span> <span style="color: #3366CC;">&quot;DOMContentLoaded&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
            fireDOMReady<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span><span style="color: #003366; font-weight: bold;">else</span> <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>e <span style="color: #000000;">&amp;&amp;</span> e.<span style="color: #006600;">type</span> <span style="color: #000000;">==</span> <span style="color: #3366CC;">&quot;load&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
            fireDOMReady<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span><span style="color: #003366; font-weight: bold;">else</span> <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>document.<span style="color: #006600;">readyState</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #009900; font-style: italic;">/loaded|complete/</span><span style="color: #000000;">&#41;</span>.<span style="color: #006600;">test</span><span style="color: #000000;">&#40;</span>document.<span style="color: #006600;">readyState</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
                fireDOMReady<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
            <span style="color: #000000;">&#125;</span><span style="color: #003366; font-weight: bold;">else</span> <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">!!</span>document.<span style="color: #006600;">documentElement</span>.<span style="color: #006600;">doScroll</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
                <span style="color: #003366; font-weight: bold;">try</span><span style="color: #000000;">&#123;</span>
                    ready <span style="color: #000000;">||</span> document.<span style="color: #006600;">documentElement</span>.<span style="color: #006600;">doScroll</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'left'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
                <span style="color: #000000;">&#125;</span><span style="color: #003366; font-weight: bold;">catch</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
                    <span style="color: #003366; font-weight: bold;">return</span><span style="color: #000000;">;</span>
                <span style="color: #000000;">&#125;</span>
                fireDOMReady<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span><span style="color: #000000;">;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">var</span> fireDOMReady <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">!</span>ready<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
            ready <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #000000;">;</span>
            fn.<span style="color: #006600;">call</span><span style="color: #000000;">&#40;</span>ctx <span style="color: #000000;">||</span> window<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
            <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>document.<span style="color: #006600;">removeEventListener</span><span style="color: #000000;">&#41;</span>
                document.<span style="color: #006600;">removeEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">&quot;DOMContentLoaded&quot;</span><span style="color: #000000;">,</span> onChange<span style="color: #000000;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
            document.<span style="color: #006600;">onreadystatechange</span> <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #000000;">;</span>
            window.<span style="color: #003366;">onload</span> <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #000000;">;</span>
            clearInterval<span style="color: #000000;">&#40;</span>timer<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
            timer <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #000000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span><span style="color: #000000;">;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>document.<span style="color: #006600;">addEventListener</span><span style="color: #000000;">&#41;</span>
        document.<span style="color: #006600;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">&quot;DOMContentLoaded&quot;</span><span style="color: #000000;">,</span> onChange<span style="color: #000000;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
    document.<span style="color: #006600;">onreadystatechange</span> <span style="color: #000000;">=</span> onChange<span style="color: #000000;">;</span>
    timer <span style="color: #000000;">=</span> setInterval<span style="color: #000000;">&#40;</span>onChange<span style="color: #000000;">,</span> <span style="color: #CC0000;">5</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
    window.<span style="color: #003366;">onload</span> <span style="color: #000000;">=</span> onChange<span style="color: #000000;">;</span>
<span style="color: #000000;">&#125;</span><span style="color: #000000;">;</span></pre></div></div>

<p>I also made a similar version that utilized a defer script for IE and worked just as well, however due to a fundamental flaw in the implementation I recently became aware of, I decided the current version as it is now would suffice. Furthermore, the method could be modified to accept multiple functions and simply append them to an array which could then be iterated over and executed accordingly once the DOM is loaded, facilitating multiple onload handlers. Depending on the response I may add those or other features at some later point in time, or feel free to modify it as you see fit for your own personal use.</p>
<h3>Download</h3>
<ul>
<li><a href="http://ryanmorr.com/modules/ondomready-compressed.js">Compressed (1kb)</a></li>
<li><a href="http://ryanmorr.com/modules/ondomready.js">Fully Documented (2kb)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://ryanmorr.com/archives/ondomready-no-browser-sniffing/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Cross-Browser XML and XSLT Processing</title>
		<link>http://ryanmorr.com/archives/cross-browser-xml-and-xslt-processing</link>
		<comments>http://ryanmorr.com/archives/cross-browser-xml-and-xslt-processing#comments</comments>
		<pubDate>Wed, 07 Jan 2009 05:06:16 +0000</pubDate>
		<dc:creator>Ryan Morr</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://ryanmorr.com/?p=69</guid>
		<description><![CDATA[There is something inherently powerful about what direct data interaction and manipulation is capable of and the ease at which it performs. XSLT may be called a stylesheet but it is nothing like CSS, it deals directly in data management and presentation, utilizing advanced functionality typically reserved for a scripting language. Similar to what Smarty [...]]]></description>
			<content:encoded><![CDATA[<p>There is something inherently powerful about what direct data interaction and manipulation is capable of and the ease at which it performs. XSLT may be called a stylesheet but it is nothing like CSS, it deals directly in data management and presentation, utilizing advanced functionality typically reserved for a scripting language. Similar to what Smarty is to PHP templates, XSLT basically defines the rules for rendering an XML file, with one simportant distinction &#8211; the data is live. The data is subject to further XSL manipulation after the XML document has been initially tranformed, faciliating many dynamic features. This has great implications on the web applications of the future; loading a widget could be as simple as dynamically loading an XML document which contains all settings and instructions, performing an XSLT transformation and appending the result document within the DOM. With those kind of capabilities the potential for such an application is limitless. <span id="more-69"></span>The promise of both XML and XSLT or its greater implications may not be well known but it&#8217;s also not new. For a while now it has been speculated to be the next &#8220;big thing&#8221; in web development, some even went so far as to claim XML would replace HTML as the dominant markup language (I suppose XHTML is the first step, albeit a small one). However, aside from simple data transfer its popularity is far from it&#8217;s anticipated potential due in large part to a lack of browser support. I should however mention some of the great work I&#8217;ve seen out of <a href="http://www.backbase.com/" target="_blank">Backbase</a> with their various UI widgets, but nevertheless XML really hasn&#8217;t caught on the way I hoped it would.</p>
<p>In light of my new found appreciation of XML and XSLT I decided to put a little script together for your basic XML and XSLT importation and manipulation. Hopefully we can get those wheels turning again, maybe one day I can see that potential and not just read about it.</p>
<p>The script itself is fairly lightweight and handles the basic functions associated with XML and XSLT manipulation in Javascript as I&#8217;ve outlined below. If your looking for something a little more full featured, Sarissa is a great option as it can handle just about everything including the kitchen sink, you can find it here; <a href="http://dev.abiss.gr/sarissa/" target="_blank">http://dev.abiss.gr/sarissa/</a>.</p>
<h3>Features</h3>
<ul>
<li>Load local XML and XLST files</li>
<li>Encode and decode an XML string</li>
<li>Serialize an XML string into a document</li>
<li>Parse XML document into a string</li>
<li>Transform an XML file with an XSLT stylesheet and apply to element</li>
<li>Perform an XPath query on an XML document</li>
<li>Supports IE6+, FF, Opera, Safari, and Chrome</li>
<li>No browser detection</li>
</ul>
<h3>Example</h3>
<p>The following example illustrates a simple transformation involving an XML document, XSLT stylesheet, and an element (container) that will house the results of the transformation. See it in action <a href="http://ryanmorr.com/tests/xml/">here</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> processor <span style="color: #000000;">=</span> <span style="color: #003366; font-weight: bold;">new</span> XMLProcessor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> container <span style="color: #000000;">=</span> document.<span style="color: #006600;">getElementById</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'container'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
&nbsp;
processor.<span style="color: #006600;">loadXML</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'xml.xml'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
processor.<span style="color: #006600;">loadXSLT</span><span style="color: #000000;">&#40;</span><span style="color: #3366CC;">'xslt.xsl'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span>
processor.<span style="color: #006600;">transform</span><span style="color: #000000;">&#40;</span>container<span style="color: #000000;">&#41;</span><span style="color: #000000;">;</span></pre></div></div>

<p>The topic of XML and XSLT and their use within a web application deserves its own post (there is a lot more to it then when I&#8217;ve discussed here) and I think I&#8217;ll probably do that in the not to distant future.  For now you can find more information on XML, XSLT, and their various elements and functions at the  <a href="http://www.w3.org/TR/xslt" target="_blank">W3C</a> and <a href="http://www.w3schools.com/xsl/default.asp" target="_blank">W3 Schools</a>, two resources that I referred to in preparation of this article.</p>
<h3>Download</h3>
<ul>
<li><a href="http://ryanmorr.com/modules/xmlprocessor-compressed.js">Compressed (3kb)</a></li>
<li><a href="http://ryanmorr.com/modules/xmlprocessor.js">Fully Documented (6kb)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://ryanmorr.com/archives/cross-browser-xml-and-xslt-processing/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
