<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Tech+Life+Music</title>
	
	<link>http://richardneililagan.com</link>
	<description>The not-so-secret website lair of Richard Neil Ilagan</description>
	<lastBuildDate>Tue, 05 Mar 2013 15:46:35 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/richardneililagan" /><feedburner:info uri="richardneililagan" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Enabling CORS in ASP.NET Web API</title>
		<link>http://feedproxy.google.com/~r/richardneililagan/~3/PkxNnsdTo-Q/</link>
		<comments>http://richardneililagan.com/2013/03/enabling-cors-aspnet-web-api/#comments</comments>
		<pubDate>Tue, 05 Mar 2013 15:46:35 +0000</pubDate>
		<dc:creator>Richard Neil Ilagan</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[cors]]></category>
		<category><![CDATA[cross domain]]></category>
		<category><![CDATA[web api]]></category>

		<guid isPermaLink="false">http://richardneililagan.com/?p=1473</guid>
		<description><![CDATA[<p><a href="http://richardneililagan.com/2013/03/enabling-cors-aspnet-web-api/">Enabling CORS in ASP.NET Web API</a> was originally published in <a href="http://richardneililagan.com">Tech+Life+Music</a>.</p><p>I wrote some code that will allow you to use CORS / cross-origin resource sharing on your Web API controllers / actions. Feel free to use it as well!</p>]]></description>
				<content:encoded><![CDATA[<p><a href="http://richardneililagan.com/2013/03/enabling-cors-aspnet-web-api/">Enabling CORS in ASP.NET Web API</a> was originally published in <a href="http://richardneililagan.com">Tech+Life+Music</a>.</p><p>I found myself having to create a completely REST-enabled API for an in-house solution, so what better way to do this in .NET than leveraging ASP.NET Web API?</p>
<p>While Web API makes mostly everything about the process simple and easy, considering that this is going to be a full-fledged REST API, I had to consider allowing cross-domain requests to the API, which normally meant that I had to enable cross-origin resource sharing (CORS) on it. </p>
<p>I found a nice handful of good resources for ASP.NET Web API CORS support, but I found <a href="http://blogs.msdn.com/b/carlosfigueira/archive/2012/07/02/cors-support-in-asp-net-web-api-rc-version.aspx" target="_blank">Carlos Figueira&#8217;s take on it</a> the most helpful, so I went ahead and adapted his code into my own small AOP library for use with future projects.</p>
<p>Using the library should be as easy as just adding <code>[CorsEnabled]</code> on your API actions and/or controllers. It provides a bit of additional functionality if you need it (like specifying allowed domains if you want).</p>
<p><a href="https://gist.github.com/richardneililagan/5091035" target="_blank">Feel free to get the code here</a>. I wholeheartedly welcome comments on it, and if you find that you want to fork it, be my guest.</p>
<p><script src="https://gist.github.com/richardneililagan/5091035.js"></script></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/richardneililagan?a=PkxNnsdTo-Q:a3npYfB1IoI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/richardneililagan?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/richardneililagan?a=PkxNnsdTo-Q:a3npYfB1IoI:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/richardneililagan?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/richardneililagan?a=PkxNnsdTo-Q:a3npYfB1IoI:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/richardneililagan?i=PkxNnsdTo-Q:a3npYfB1IoI:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/richardneililagan/~4/PkxNnsdTo-Q" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://richardneililagan.com/2013/03/enabling-cors-aspnet-web-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://richardneililagan.com/2013/03/enabling-cors-aspnet-web-api/</feedburner:origLink></item>
		<item>
		<title>Using Razor in Javascript and CSS files</title>
		<link>http://feedproxy.google.com/~r/richardneililagan/~3/p68oBH_Fkq8/</link>
		<comments>http://richardneililagan.com/2013/01/razor-javascript-css-files/#comments</comments>
		<pubDate>Mon, 28 Jan 2013 16:11:11 +0000</pubDate>
		<dc:creator>Richard Neil Ilagan</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[razor]]></category>

		<guid isPermaLink="false">http://richardneililagan.com/?p=1460</guid>
		<description><![CDATA[<p><a href="http://richardneililagan.com/2013/01/razor-javascript-css-files/">Using Razor in Javascript and CSS files</a> was originally published in <a href="http://richardneililagan.com">Tech+Life+Music</a>.</p><p>A sample project for this discussion is available over on my Github. There are countless use cases where I found myself needing to use Razor syntax (in ASP.NET MVC 3+) inside Javascript, CSS and other asset files. While there are many different ways to get Razor output into external JS files (for example, getting a [...]</p>]]></description>
				<content:encoded><![CDATA[<p><a href="http://richardneililagan.com/2013/01/razor-javascript-css-files/">Using Razor in Javascript and CSS files</a> was originally published in <a href="http://richardneililagan.com">Tech+Life+Music</a>.</p><p><em><a href="https://github.com/richardneililagan/razor-assets" target="_blank">A sample project for this discussion is available over on my Github.</a></em></p>
<p>There are countless use cases where I found myself needing to use Razor syntax (in ASP.NET MVC 3+) inside Javascript, CSS and other asset files. While there are many different ways to get Razor output into external JS files (for example, getting a <code>Url.Action()</code> call into a JS file&#8217;s <code>jQuery.ajax()</code> call), I find that most feel like beating around the bush. After all, views in MVC don&#8217;t always have to be HTML files, at least in the traditional spirit of MVC.</p>
<p>So why not create a Javascript view?</p>
<p>If you think about it, there&#8217;s nothing really stopping you from creating a controller, adding in a view, then writing up the view in Javascript syntax (save for the less-than-tasty syntax highlighting and Intellisense in Visual Studio, since it kind of expects that views are HTML files).</p>
<p>The key thing is to modify the <strong>Content-Type</strong> of the response to the appropriate value, based on what you&#8217;re returning. So Javascript files will be <code>text/javascript</code>, CSS files will be <code>text/css</code>. It&#8217;s the same basic idea with, for example, returning image data through the wire.</p>
<p>For example, this controller action:</p>

<div class="wp_codebox"><table><tr id="p14606"><td class="code" id="p1460code6"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> AssetController <span style="color: #008000;">:</span> Controller <span style="color: #008000;">&#123;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> ActionResult Styles <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
        Response<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;text/css&quot;</span><span style="color: #008000;">;</span>
&nbsp;
        var model <span style="color: #008000;">=</span> Context<span style="color: #008000;">.</span><span style="color: #0000FF;">GetEntity</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> View<span style="color: #008000;">&#40;</span>model<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>&#8230; can have the following view:</p>

<div class="wp_codebox"><table><tr id="p14607"><td class="code" id="p1460code7"><pre class="css" style="font-family:monospace;"><span style="color: #a1a100;">@model Entity;</span>
&nbsp;
<span style="color: #cc00cc;">#entity</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">color</span> <span style="color: #00AA00;">:</span> <span style="color: #a1a100;">@Model.Color;</span>
    <span style="color: #000000; font-weight: bold;">background</span> <span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #a1a100;">@Model.BackgroundImage.Url);</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>&#8230; and can be wired into your markup just like any other stylesheet:</p>

<div class="wp_codebox"><table><tr id="p14608"><td class="code" id="p1460code8"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;@Url.Action(&quot;</span>Styles<span style="color: #ff0000;">&quot;,&quot;</span>Asset<span style="color: #ff0000;">&quot;)&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<p>Taking all that a bit further, we can write up an <code>Attribute</code> to easily maintain the corresponding <code>Content-Type</code> to each action:</p>

<div class="wp_codebox"><table><tr id="p14609"><td class="code" id="p1460code9"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ContentType <span style="color: #008000;">:</span> FilterAttribute, IActionFilter 
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> contentType<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> ContentType <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> ct<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">contentType</span> <span style="color: #008000;">=</span> ct<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnActionExecuted<span style="color: #008000;">&#40;</span>ActionExecutedContext context<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008080; font-style: italic;">/* nada */</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnActionExecuting<span style="color: #008000;">&#40;</span>ActionExecutingContext context<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        context<span style="color: #008000;">.</span><span style="color: #0000FF;">HttpContext</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Response</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">contextType</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>&#8230; and just mark our actions with it:</p>

<div class="wp_codebox"><table><tr id="p146010"><td class="code" id="p1460code10"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>ContentType<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;text/css&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> ActionResult Styles<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// ...</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> View<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>All that notwithstanding though, you&#8217;ll still have to deal with two problems when you do this:</p>
<ol>
<li><strong>Visual Studio assumes that all views are HTML.</strong> You don&#8217;t get accurate color highlighting and Intellisense in your asset views. I don&#8217;t know about you, but the countless ways that VS pushes back when I do this kind of irks me off.</li>
<li><strong>You don&#8217;t get bundling (in ASP.NET MVC 4), and unless you wire it up somehow with WebGrease, you don&#8217;t get minification either.</strong> Caching is possible, but the whole point of dynamic assets are just that: they&#8217;re dynamic. You still can cache via normal ASP.NET MVC methods if, for example, you can manage to scope your use cases into unique URLs (via query strings probably), or you could opt to write your own caching mechanism for this.</li>
</ol>
<p>If you want to try this out, I&#8217;ve prepared <a href="https://github.com/richardneililagan/razor-assets" target="_blank">a very simple VS 2012 MVC 4 project ready to go over on my Github</a>. Feel free to clone, fork, and/or contribute!</p>
<p>With all that said and done though, I believe that this is the way to correctly use views in an MVC framework, and that Visual Studio should perhaps allows for better leniency for use cases such as this. I&#8217;d love to hear your ideas on the topic though.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/richardneililagan?a=p68oBH_Fkq8:dYULonTPsRY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/richardneililagan?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/richardneililagan?a=p68oBH_Fkq8:dYULonTPsRY:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/richardneililagan?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/richardneililagan?a=p68oBH_Fkq8:dYULonTPsRY:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/richardneililagan?i=p68oBH_Fkq8:dYULonTPsRY:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/richardneililagan/~4/p68oBH_Fkq8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://richardneililagan.com/2013/01/razor-javascript-css-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://richardneililagan.com/2013/01/razor-javascript-css-files/</feedburner:origLink></item>
		<item>
		<title>Setting up dotless with automatic LESS CSS compilation on build</title>
		<link>http://feedproxy.google.com/~r/richardneililagan/~3/z9W7UDgnkcA/</link>
		<comments>http://richardneililagan.com/2012/08/automatic-dotless-compile-less-css/#comments</comments>
		<pubDate>Fri, 31 Aug 2012 14:20:44 +0000</pubDate>
		<dc:creator>Richard Neil Ilagan</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[automatic]]></category>
		<category><![CDATA[compile]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[dot less]]></category>
		<category><![CDATA[dotless]]></category>
		<category><![CDATA[less]]></category>
		<category><![CDATA[less css]]></category>
		<category><![CDATA[msbuild]]></category>

		<guid isPermaLink="false">http://richardneililagan.com/?p=1431</guid>
		<description><![CDATA[<p><a href="http://richardneililagan.com/2012/08/automatic-dotless-compile-less-css/">Setting up dotless with automatic LESS CSS compilation on build</a> was originally published in <a href="http://richardneililagan.com">Tech+Life+Music</a>.</p><p>How do you set up the dotless package to automatically compile your LESS CSS files on build? This guide will show you how.</p>]]></description>
				<content:encoded><![CDATA[<p><a href="http://richardneililagan.com/2012/08/automatic-dotless-compile-less-css/">Setting up dotless with automatic LESS CSS compilation on build</a> was originally published in <a href="http://richardneililagan.com">Tech+Life+Music</a>.</p><p>If you want to use the <a href="http://lesscss.org/" title="LESS CSS" target="_blank">LESS dynamic CSS language</a> in your .NET web applications, using the <a href="https://github.com/dotless/dotless" target="_blank">dotless</a> (formally .less) library is the popular way to go, so that your <code>.less</code> files are compiled server-side.</p>
<p>While dotless provides a no-brainer way to serve compiled <code>.less</code> files through its custom <code>HttpHandler</code>, this still forces the app to compile the <code>.less</code> files on every request (barring caching, etc). An arguably better way would be to pre-compile our <code>.less</code> files beforehand, so that we can work with LESS in development, while mindlessly serving minified CSS files in production.</p>
<p>This post will show how we can integrate the dotless package into your .NET web application, do all that crap I just described above, and making sure we do them in an automatic manner. A true set-and-forget system.</p>
<p>Major props go to my colleague @camemay, whose work I&#8217;ve built upon.</p>
<p>Let&#8217;s get started!</p>
<h2>Installing the dotless package</h2>
<p>For this post, I&#8217;ll be using an ASP.NET MVC 3 web application as a scapegoat. Having a ready MVC 3 project in hand (a new one off Visual Studio would be fine), installing dotless is a breeze over NuGet.</p>

<div class="wp_codebox"><table><tr id="p143111"><td class="code" id="p1431code11"><pre class="csharp" style="font-family:monospace;">install<span style="color: #008000;">-</span>package dotless</pre></td></tr></table></div>

<p>This does a few things off the bat, but aside from the usual auto-adding of installed assemblies, the changes of more concern are those that silently happened in your <code>web.config</code>:</p>
<ul>
<li>The custom <code>LessCssHttpHandler</code> was registered in your <code>httpHandlers</code> declaration to handle <code>*.less</code> files.</li>
<li>The same handler was also registered against the <code>system.webServer</code> declaration.</li>
<li>A custom <code>dotless</code> configuration was defined, and a preliminary <code>dotless</code> config section was appended at the end of the file.</li>
</ul>
<p>Let&#8217;s start by modifying the <code>path="*.less"</code> declarations (in two places) to <code>path=".less.css"</code>. This allows us to open LESS files in Visual Studio automatically as CSS files, and therefore with CSS Intellisense. Take note that this modifies the handling declarations such that <code>.less</code> files <strong>won&#8217;t</strong> be handled by the dotless handler (they won&#8217;t be compiled!) &#8212; you have to write your LESS code in <code>.less.css</code> files.</p>
<p>If you&#8217;re particularly paranoid about it, you may opt to change <code>verb="GET"</code> to <code>verb="GET,HEAD"</code> as well.</p>

<div class="wp_codebox"><table><tr id="p143112"><td class="code" id="p1431code12"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;system.web<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- ... --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;httpHandlers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;*.less.css&quot;</span> <span style="color: #000066;">verb</span>=<span style="color: #ff0000;">&quot;GET&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;dotless.Core.LessCssHttpHandler, dotless.Core&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/httpHandlers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/system.web<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>


<div class="wp_codebox"><table><tr id="p143113"><td class="code" id="p1431code13"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;system.webServer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- ... --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;handlers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;dotless&quot;</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;*.less.css&quot;</span> <span style="color: #000066;">verb</span>=<span style="color: #ff0000;">&quot;GET&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;dotless.Core.LessCssHttpHandler,dotless.Core&quot;</span> <span style="color: #000066;">resourceType</span>=<span style="color: #ff0000;">&quot;File&quot;</span> <span style="color: #000066;">preCondition</span>=<span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/handlers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/system.webServer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<h2>Setting up the automated LESS compilation script</h2>
<p>Now that we&#8217;ve basically decided that LESS CSS code will be in <code>*.less.css</code> files, we can target that for automatic compilation. </p>
<p>The way we&#8217;re going to approach this is by writing a custom MSBuild script that executes the dotless compiler against <code>*.less.css</code> files, converting the LESS syntax to minified legal CSS, and renaming the files to <code>*.min.css</code>. These will be our files for production.</p>
<p>For most of this process, I&#8217;ve been referencing <a href="http://tedgustaf.com/en/blog/2010/261/setup-dotless-in-aspnet-project/" title="Set up dotLess in a new ASP.NET project" target="_blank">Ted Nyberg&#8217;s own post on the topic</a>, with a few changes here and there.</p>
<h3>Include the dotless compiler in your project</h3>
<p>First thing we have to do is to include the <code>dotless.compiler.exe</code> in the project so we can refer to it later in the build script. If you followed this guide and installed dotless via NuGet, then this file will be in <code>$(ProjectDir)/packages/dotless.x.x.x/tool</code>. Just add this executable into your project. For purposes of this guide, I&#8217;ll be mirroring Ted&#8217;s folder naming convention as well, so I&#8217;ll be adding this into an <code>[MSBuild]</code> folder.</p>
<h3>Write the build script to compile LESS</h3>
<p>Next we&#8217;ll start writing our build script. I added an XML file into my <code>[MSBuild]</code> folder, and named it <code>dotless.CompileLessToCss.targets</code>. You can name it whatever you want, but by nature of what we&#8217;re going to be working with, a good practice is to name it as a <code>*.targets</code> file.</p>
<p>Let&#8217;s start with Ted&#8217;s script, and let&#8217;s work from there:</p>

<div class="wp_codebox"><table><tr id="p143114"><td class="code" id="p1431code14"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Project</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/developer/MsBuild/2003&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Target</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;CompileDotlessCss&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemGroup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Binaries</span> <span style="color: #000066;">Include</span>=<span style="color: #ff0000;">&quot;*.dll;*.exe&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemGroup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- Compile dotLess CSS into minified full CSS --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Exec</span> <span style="color: #000066;">Command</span>=<span style="color: #ff0000;">&quot;$(ProjectDir)..\[MSBuild]\dotless.Compiler.exe $(ProjectDir)..\css\main.css $(ProjectDir)..\css\main.min.css -m&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>This script will compile only one file in our project: <code>main.css</code> into <code>main.min.css</code>. Let&#8217;s modify this so that it&#8217;ll convert all <code>*.less.css</code> in our <code>$(ProjectDir)..\css\</code> folder into their <code>*.min.css</code> counterparts.</p>
<p>We&#8217;ll have to make the build script identify all those <code>*.less.css</code> files. We can set up an <code>ItemGroup</code> declaration to do just that. Put this right before the <code>&lt;Target&gt;</code> declaration:</p>

<div class="wp_codebox"><table><tr id="p143115"><td class="code" id="p1431code15"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemGroup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;LessFiles</span> <span style="color: #000066;">Include</span>=<span style="color: #ff0000;">&quot;css\*.less.css&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemGroup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>That aside, we can modify the <code>Exec</code> command to this:</p>

<div class="wp_codebox"><table><tr id="p143116"><td class="code" id="p1431code16"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Exec</span> <span style="color: #000066;">Command</span>=<span style="color: #ff0000;">&quot;[MSBuild]\dotless.compiler.exe -m %(LessFiles.FullPath)&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<p>This will take care of running the dotless compiler against all our <code>*.less.css</code> files. Only thing left to do is to specify what filename to use for the compiled + minified CSS code (and not rely on the compiler&#8217;s defaults).</p>
<p>Unfortunately, there&#8217;s no easy way to call a string.replace on <code>LessFiles.FullPath</code>. I&#8217;m personally using the method described in <a href="http://stackoverflow.com/a/8904902/304588" target="_blank">this StackOverflow answer</a>, but there are others suggested on the same page. Use the method most comfortable for you. This modifies my command to this:</p>

<div class="wp_codebox"><table><tr id="p143117"><td class="code" id="p1431code17"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Exec</span> <span style="color: #000066;">Command</span>=<span style="color: #ff0000;">&quot;[MSBuild]\dotless.compiler.exe -m %(LessFiles.FullPath) $([System.String]::Copy('%(LessFiles.FullPath)').Replace('.less.','.min.'))&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<p>Lastly, add the <code>AfterTargets="AfterBuild"</code> property to the <code>Target</code> tag. This will tell MSBuild to run this script after the <code>AfterBuild</code> &#8220;event&#8221; in the project build process.</p>
<p>Our final build script will be as follows:</p>

<div class="wp_codebox"><table><tr id="p143118"><td class="code" id="p1431code18"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Project</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/developer/MsBuild/2003&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #808080; font-style: italic;">&lt;!--    </span>
<span style="color: #808080; font-style: italic;">    This MSBuild Script will compile all [*.less.css] files in the /CSS folder to their [*.min.css] counterparts.</span>
<span style="color: #808080; font-style: italic;">    --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemGroup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;LessFiles</span> <span style="color: #000066;">Include</span>=<span style="color: #ff0000;">&quot;css\*.less.css&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemGroup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Target</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;CompileDotlessCss&quot;</span> <span style="color: #000066;">AfterTargets</span>=<span style="color: #ff0000;">&quot;AfterBuild&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemGroup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Binaries</span> <span style="color: #000066;">Include</span>=<span style="color: #ff0000;">&quot;*.dll;*.exe&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemGroup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>                
&nbsp;
        <span style="color: #808080; font-style: italic;">&lt;!-- Compile dotLess CSS into minified full CSS --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Exec</span> <span style="color: #000066;">Command</span>=<span style="color: #ff0000;">&quot;[MSBuild]\dotless.compiler.exe -m %(LessFiles.FullPath) $([System.String]::Copy('%(LessFiles.FullPath)').Replace('.less.','.min.'))&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<h3>Invoking our script when the project builds</h3>
<p>The last thing we have to do is to make sure our script gets run when the project actually builds. I&#8217;m not at all adept with MSBuild (crash courses! ugh!) so you might prefer doing this another way, but this is the method I found closest to heart. Feel free to adjust.</p>
<p>The <code>.csproj</code> file that defines your web application also has MSBuild instructions on how to build it on compile. We&#8217;ll just manually plug in directions to include our script.</p>
<p>To be able to edit the <code>.csproj</code>, we&#8217;ll first need to unload the project. Right-click on the project in your <strong>Solution Explorer</strong>, and choose to <strong>Unload Project</strong>.</p>
<p>With your project unloaded, right-click on the project again, and choose to edit it. You&#8217;ll be presented with delicious XML.</p>
<p>Somewhere in all that XML, just plug in an <code>Import</code> command:</p>

<div class="wp_codebox"><table><tr id="p143119"><td class="code" id="p1431code19"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Import</span> <span style="color: #000066;">Project</span>=<span style="color: #ff0000;">&quot;[MSBuild]\dotless.CompileLessToCss.targets&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<p>I&#8217;m particularly paranoid, so I put this somewhere towards the end of the file.</p>
<h3>And that&#8217;s it!</h3>
<p>If we did everything correctly, our project should automatically compile our <code>*.less.css</code> into <code>*.min.css</code> whenever we build the project.</p>
<p>Try it out! Add a <code>style.less.css</code> into your project, and slap this inside:</p>

<div class="wp_codebox"><table><tr id="p143120"><td class="code" id="p1431code20"><pre class="css" style="font-family:monospace;"><span style="color: #a1a100;">@foo : #beefff;</span>
&nbsp;
body <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background</span> <span style="color: #00AA00;">:</span> <span style="color: #a1a100;">@foo;</span>
    div<span style="color: #cc00cc;">#main</span> <span style="color: #00AA00;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">padding</span> <span style="color: #00AA00;">:</span> <span style="color: #933;">2px</span><span style="color: #00AA00;">;</span>
    <span style="color: #00AA00;">&#125;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Compile your project, then navigate to <code>style.less.css</code> in your browser, and you should get the following:</p>

<div class="wp_codebox"><table><tr id="p143121"><td class="code" id="p1431code21"><pre class="css" style="font-family:monospace;">body <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#beefff</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
body div<span style="color: #cc00cc;">#main</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">2px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Navigating to <code>style.min.css</code> should net you:</p>

<div class="wp_codebox"><table><tr id="p143122"><td class="code" id="p1431code22"><pre class="css" style="font-family:monospace;">body<span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#beefff</span><span style="color: #00AA00;">&#125;</span>body div<span style="color: #cc00cc;">#main</span><span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">2px</span><span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Use the <code>*.less.css</code> files for development and debugging, and the <code>*.min.css</code> files for production.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/richardneililagan?a=z9W7UDgnkcA:m3rD_hydUiE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/richardneililagan?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/richardneililagan?a=z9W7UDgnkcA:m3rD_hydUiE:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/richardneililagan?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/richardneililagan?a=z9W7UDgnkcA:m3rD_hydUiE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/richardneililagan?i=z9W7UDgnkcA:m3rD_hydUiE:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/richardneililagan/~4/z9W7UDgnkcA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://richardneililagan.com/2012/08/automatic-dotless-compile-less-css/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://richardneililagan.com/2012/08/automatic-dotless-compile-less-css/</feedburner:origLink></item>
		<item>
		<title>BPI, BDO, are you storing my password?</title>
		<link>http://feedproxy.google.com/~r/richardneililagan/~3/2XQQYRGQCA0/</link>
		<comments>http://richardneililagan.com/2012/04/password-storing/#comments</comments>
		<pubDate>Mon, 02 Apr 2012 14:53:08 +0000</pubDate>
		<dc:creator>Richard Neil Ilagan</dc:creator>
				<category><![CDATA[Other Computers and Technology]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[bank]]></category>
		<category><![CDATA[exploit]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[password hash]]></category>
		<category><![CDATA[plain text]]></category>
		<category><![CDATA[plaintext]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[security exploit]]></category>
		<category><![CDATA[web application]]></category>
		<category><![CDATA[website]]></category>

		<guid isPermaLink="false">http://richardneililagan.com/?p=1404</guid>
		<description><![CDATA[<p><a href="http://richardneililagan.com/2012/04/password-storing/">BPI, BDO, are you storing my password?</a> was originally published in <a href="http://richardneililagan.com">Tech+Life+Music</a>.</p><p>Under no circumstance should any website store anyone's password, whatever their service may be. From the looks of it, BPI and BDO may be doing just that however.</p>]]></description>
				<content:encoded><![CDATA[<p><a href="http://richardneililagan.com/2012/04/password-storing/">BPI, BDO, are you storing my password?</a> was originally published in <a href="http://richardneililagan.com">Tech+Life+Music</a>.</p><p>It&#8217;s really careless <em>programmer</em>-slash-<em>security</em> things like this that piss me off. Have a look at the Bank of the Philippine Islands&#8217; (BPI) personal internet banking login screen:</p>
<aside>For some reason, the login screen to BPI&#8217;s internet banking feature limits passwords to only <strong>12 characters</strong></aside>
<p><img src="http://richardneililagan.com/wp-content/uploads/2012/04/bpi.png" alt="BPI, BDO, are you storing my password?" title="The BPI personal login screen" width="545" height="406" class="aligncenter size-full wp-image-1405" /></p>
<p>Although it&#8217;s not as restrictive as the one on BPI&#8217;s website, here&#8217;s the equivalent login service provided on Banco de Oro&#8217;s (BDO) website:</p>
<aside>Similarly, BDO limits characters to only <strong>20 characters</strong>. That doesn&#8217;t sound as bad as BPI&#8217;s but let me make my point.</aside>
<p><img src="http://richardneililagan.com/wp-content/uploads/2012/04/bdo1.png" alt="BPI, BDO, are you storing my password?" title="The BDO internet banking login screen" width="545" height="426" class="aligncenter size-full wp-image-1409" /></p>
<p>I can think of absolutely no use case where, as a software architect, programmer, what-have-you, <strong>I would want to limit the number or kind of characters a user can assign for a password</strong>. It&#8217;s an absolute no-no. It&#8217;s a Bad Thing™. It&#8217;s what your mother had been warning you not to do when you were a kid, right after those sermons against playing with fire and sharp objects.</p>
<h2>But, limiting characters saves database space!</h2>
<p>Point well taken, and honestly, that would make a lot of sense <strong>provided that we weren&#8217;t talking about passwords.</strong></p>
<p>If this was any other kind of field (like a username, or an address input), limiting the characters a user can input may not just be helpful: it may well be essential. By controlling that amount of data anyone can save into any data field, we can control how fast the size of the underlying database bloats (because databases take up <em>expensive</em> hard drive space too, you know).</p>
<p>Passwords are an exception though. To start off, websites (or anyone for that matter) <a href="http://plaintextoffenders.com/about/" title="About | PlainTextOffenders">should not be storing your password in plain text</a>.</p>
<aside>Why?<br/><br/>If someone else knew your password, they could use it to log in as you, and do things on your behalf, such as transfer money to their own bank account, or post demeaning Facebook status messages.<br/><br/>Same thing here. If a website service stores your password in plain text, someone can come in and read that from a database, and use it to log into websites with malicious intent. Especially if you use the same password across different sites.<br/><br/>Who can do that? I don&#8217;t know. Maybe a playful programmer who has access to the database. Maybe a hacker who gains entry on your bank&#8217;s database. Maybe a disgruntled employee who wants to <em>stick it to the man</em>. Take your pick. <a href="http://arstechnica.com/business/news/2012/02/microsofts-store-site-in-india-defaced-hackers-find-plain-text-passwords.ars" target="_blank">It&#8217;s happened before</a>.</aside>
<p>It&#8217;s simple. <strong>No one but you should know your password</strong>. <a href="http://www.codinghorror.com/blog/2007/09/youre-probably-storing-passwords-incorrectly.html">Not even the websites you use your passwords on</a>. Ever wonder why (most) websites allow you to reset your password, but they don&#8217;t (can&#8217;t) give you your password when you forget it? Well, this is why.</p>
<p>But then, you&#8217;d ask: if websites don&#8217;t know my password, how do they know that I&#8217;m using the right password for my account then? Again, simple. They use what&#8217;s called a <strong>hash</strong>.</p>
<p>The thing is, websites <a href="http://simple.wikipedia.org/wiki/Cryptographic_hash_function">can &#8220;hash&#8221; any piece of text</a>, and it will generate an (ideally) constant string of text that looks randomized, and is hard to remember. Here are a few examples using SHA256 hashes:</p>

<div class="wp_codebox"><table><tr id="p140423"><td class="code" id="p1404code23"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// &quot;password&quot;</span>
5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8
&nbsp;
<span style="color: #008080; font-style: italic;">// &quot;passworD&quot;</span>
9e78de733c6a51c0cc954c1d956d8929ad1310513e1042d81edc375219c6a2ef
&nbsp;
<span style="color: #008080; font-style: italic;">// &quot;1234&quot;</span>
03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4
&nbsp;
<span style="color: #008080; font-style: italic;">// &quot;a&quot;</span>
ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb</pre></td></tr></table></div>

<p>This is what websites (should) use in checking if your password is correct. It stores the hash of your password in its database, so whenever you try to login (and provide a password), it hashes that, and compares the resulting hash to the hash it has in its database.</p>
<aside>The thing with hashes is that no matter how many times you hash the same string, you always get the same result. Hashing text is easy, but doing it the other way around is near impossible (that is, restoring a hash to its original plain text). It&#8217;s a form of <em>one-way encryption</em>, in a sense.</aside>
<p>Why does that matter then? Well, one of the unique things about a hash is that no matter how long the original piece of text is, it always always always results in a hash of constant length (for example, the SHA256 hashes above are always 64 characters long).</p>
<p>So if you&#8217;re storing that in a database, you know that <strong>you&#8217;re always storing text that is 64 characters long</strong> no matter what the password is. What&#8217;s the sense of limiting my password to 12 characters then?</p>
<p>More importantly, what does that tell me?</p>
<p>Either BPI and BDO are <em>unnecessarily</em> making their users use passwords that are significantly less secure than possible, or <strong>they&#8217;re storing your passwords in plain text</strong>. I just hope it&#8217;s not the latter.</p>
<h2>But 12 characters in a password is more than enough.</h2>
<p>Again, point well taken. If you use numbers, lowercase and uppercase letters, and symbols in your password, a 12-character password will take a hacker years to break (only hours or even minutes if you only use letters!).</p>
<p>But then again, passwords like that look like <code>*HU9J67+0G3%</code> and are generally hard to remember. This is exactly the reason why there has been debate about <a href="http://richardneililagan.com/2011/08/a-compelling-idea-for-creating-strong-passwords-that-are-easy-to-remember/" title="A compelling idea for creating strong passwords that are easy to remember">the usability of the passwords</a> we use everyday.</p>
<aside>The point being made here is that, while using different kinds of characters in your password makes it more secure, <strong>so does just making it longer</strong>. Longer passwords with less kinds of characters can just be as secure as short, cryptic ones, with the added benefit of being easier to remember.</aside>
<p>Passwords, to make a long discussion short, are hard to remember. Passphrases (like <code>pleasedontmilkmycow</code>, on the other hand, <a href="https://xkcd.com/936/">are more efficient on both security and human usability aspects</a>. But to make use of them, I need more than just 12 characters. It&#8217;s a somewhat touchy topic, but hey, you&#8217;ve got to at least agree on the usability part.</p>
<h2>Please don&#8217;t store my password.</h2>
<p>Let me just take this opportunity to say it again: <strong>there&#8217;s absolutely no justification as to why you&#8217;d want to limit your users&#8217; password length</strong>. As far as I&#8217;m concerned, if you limit the length of my password, <strong>I know you&#8217;re storing my password</strong>. And that, my friend, is a big no-no.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/richardneililagan?a=2XQQYRGQCA0:kmsN_wVRH6c:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/richardneililagan?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/richardneililagan?a=2XQQYRGQCA0:kmsN_wVRH6c:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/richardneililagan?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/richardneililagan?a=2XQQYRGQCA0:kmsN_wVRH6c:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/richardneililagan?i=2XQQYRGQCA0:kmsN_wVRH6c:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/richardneililagan/~4/2XQQYRGQCA0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://richardneililagan.com/2012/04/password-storing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://richardneililagan.com/2012/04/password-storing/</feedburner:origLink></item>
		<item>
		<title>jQuery event delegation, and one more reason why you should be using it more</title>
		<link>http://feedproxy.google.com/~r/richardneililagan/~3/PjN0CHZx4a8/</link>
		<comments>http://richardneililagan.com/2012/03/jquery-delegate-another-use-case/#comments</comments>
		<pubDate>Mon, 12 Mar 2012 06:05:17 +0000</pubDate>
		<dc:creator>Richard Neil Ilagan</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[delegate]]></category>
		<category><![CDATA[event handling]]></category>

		<guid isPermaLink="false">http://richardneililagan.com/?p=1385</guid>
		<description><![CDATA[<p><a href="http://richardneililagan.com/2012/03/jquery-delegate-another-use-case/">jQuery event delegation, and one more reason why you should be using it more</a> was originally published in <a href="http://richardneililagan.com">Tech+Life+Music</a>.</p><p>One thing I've noticed all this time in teaching is just how difficult it is to make developers do one simple thing in jQuery: convincing jQuery developers to use delegate more in their code has got to be one of the more resilient challenges for someone teaching the jQuery craft.</p>]]></description>
				<content:encoded><![CDATA[<p><a href="http://richardneililagan.com/2012/03/jquery-delegate-another-use-case/">jQuery event delegation, and one more reason why you should be using it more</a> was originally published in <a href="http://richardneililagan.com">Tech+Life+Music</a>.</p><p>I&#8217;ve had a somewhat extensive experience with jQuery so far. I&#8217;ve had the pleasure of reviewing peer jQuery code, mentoring potential web devs, teaching in classroom settings, and <a href="http://stackoverflow.com/users/304588/richard-neil-ilagan">bashing heads over on StackOverflow</a>, but one thing I&#8217;ve noticed all this time is just how hard it is to make developers do one simple thing in jQuery.</p>
<p>And when I say hard, I mean <strong>hard</strong>. Convincing jQuery developers to use <code>delegate</code> more in their code has got to be one of the more resilient challenges for someone teaching the jQuery craft.</p>
<p>The problem with the learning part of this (what I think at least) is that for devs coming into jQuery, you see <code>bind()</code> and then you see <code>live()</code>, and immediately think that those&#8217;re all you&#8217;ll need for working with events. Think about it: how long did it take you to know about <code>delegate()</code> from the time you learned about <code>live()</code>?</p>
<p>There&#8217;s a lot of argument for <code>delegate</code> <a href="http://www.alfajango.com/blog/the-difference-between-jquerys-bind-live-and-delegate/">out</a> <a href="http://www.ultimatewebtips.com/why-jquery-live-is-a-bad-option-to-use/">in</a> <a href="http://stackoverflow.com/questions/4579117/jquery-live-vs-delegate">the</a> <a href="http://www.timacheson.com/Blog/2011/oct/jquery_live_vs_delegate">interwebs</a>, and <a href="http://richardneililagan.com/2011/12/the-understated-beauty-of-jquerys-new-on-and-off-functions/" title="The understated beauty of jQuery’s new .on() and .off() functions">I&#8217;ve chucked my share in as well somewhat</a>, but let me offer one more compelling reason to use <code>delegate</code>.</p>
<h2>Buff your events with a bit more shine</h2>
<p>It&#8217;s easy to get lost when working with events in jQuery, even for just the somewhat harmless reason that <strong>everything is just so easy to do</strong>. For example, consider this:</p>

<div class="wp_codebox"><table><tr id="p138524"><td class="code" id="p1385code24"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>This<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>is<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>a<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>simple<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>list<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>


<div class="wp_codebox"><table><tr id="p138525"><td class="code" id="p1385code25"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// we're assigning an event handler to each li element</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toggleClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'selected'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>One would write this code and probably won&#8217;t give it a second look. After all, we&#8217;re writing our jQuery in pretty much a good form. </p>
<p>What most don&#8217;t realize, however, is that to some degree, this comes with it&#8217;s inefficiencies, particularly in the fact that binding an event listener in this way <em>automagically</em> attaches a distinct instance of the handler function to each distinct element in the jQuery wrapped set. That means that, for this particular example, we&#8217;re creating five references to a function, because each of the <code>li</code> elements gets its own piece of the pie.</p>
<p>If you&#8217;re interested in optimizing your code further, you could do this instead:</p>

<div class="wp_codebox"><table><tr id="p138526"><td class="code" id="p1385code26"><pre class="javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #006600; font-style: italic;">// we're delegating the event handler here</span>
     $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'li'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toggleClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'selected'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>What we&#8217;re doing here instead is that we&#8217;re assigning one (just one!) reference of the function to the <code>ul</code> element, and we&#8217;re telling it to fire it whenever a click occurs in any <code>li</code> inside it (hence, <em>delegating</em>).</p>
<p>Now, at first glance, that may not be much, but you&#8217;ll be surprised at <a href="http://jsperf.com/bind-vs-click/32">how fast the performance hits can add up</a>, to the point that there are arguments that <code>delegate</code> may actually be even more efficient to use in a majority of applications than <code>bind</code>.</p>
<p>It&#8217;s a pretty simple thing to do in your jQuery code (now even more so <a href="http://richardneililagan.com/2011/12/the-understated-beauty-of-jquerys-new-on-and-off-functions/" title="The understated beauty of jQuery’s new .on() and .off() functions">because of the unified <code>on()</code> function</a>), so do keep this in mind whenever you&#8217;re writing to optimize your jQuery performance.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/richardneililagan?a=PjN0CHZx4a8:Qgioa_BNcH8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/richardneililagan?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/richardneililagan?a=PjN0CHZx4a8:Qgioa_BNcH8:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/richardneililagan?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/richardneililagan?a=PjN0CHZx4a8:Qgioa_BNcH8:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/richardneililagan?i=PjN0CHZx4a8:Qgioa_BNcH8:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/richardneililagan/~4/PjN0CHZx4a8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://richardneililagan.com/2012/03/jquery-delegate-another-use-case/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://richardneililagan.com/2012/03/jquery-delegate-another-use-case/</feedburner:origLink></item>
		<item>
		<title>Learning from the HTML5 Boilerplate, Part 3</title>
		<link>http://feedproxy.google.com/~r/richardneililagan/~3/xCAjbc0_xeQ/</link>
		<comments>http://richardneililagan.com/2012/01/learning-html5-boilerplate-part-3/#comments</comments>
		<pubDate>Fri, 20 Jan 2012 13:00:07 +0000</pubDate>
		<dc:creator>Richard Neil Ilagan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[boilerplate]]></category>
		<category><![CDATA[chrome]]></category>
		<category><![CDATA[chrome frame]]></category>
		<category><![CDATA[chromium]]></category>
		<category><![CDATA[google chrome]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[html5 boilerplate]]></category>

		<guid isPermaLink="false">http://richardneililagan.com/?p=1377</guid>
		<description><![CDATA[<p><a href="http://richardneililagan.com/2012/01/learning-html5-boilerplate-part-3/">Learning from the HTML5 Boilerplate, Part 3</a> was originally published in <a href="http://richardneililagan.com">Tech+Life+Music</a>.</p><p>One of the easiest things you can do to make sure the dreaded Internet Explorer 6 is up to reasonable snuff is to include a single Javascript snippet on your page.</p>]]></description>
				<content:encoded><![CDATA[<p><a href="http://richardneililagan.com/2012/01/learning-html5-boilerplate-part-3/">Learning from the HTML5 Boilerplate, Part 3</a> was originally published in <a href="http://richardneililagan.com">Tech+Life+Music</a>.</p><p>On the topic of <a href="http://richardneililagan.com/2012/01/learning-html5-boilerplate-part-2/" title="Learning from the HTML5 Boilerplate, Part 2">making Internet Explorer behave</a>, one of the easiest things you can do to make sure the dreaded Internet Explorer 6 is up to reasonable snuff is to include a single Javascript snippet on your page.</p>
<h2>Chrome Frame</h2>
<p>The Google Chrome Frame <a href="http://blog.chromium.org/2009/09/introducing-google-chrome-frame.html" title="Introducing Google Chrome Frame" target="_blank">is not new</a>. It&#8217;s an IE plugin that acts as an additional rendering engine that delivers more or less the same features, offerings, and HTML5 + CSS3 goodness as the standard Google Chrome. Long story short, it&#8217;s a Google Chrome running <em>inside</em> your Internet Explorer. The ultimate problem was that it needed to be installed by <a href="http://kb.iu.edu/data/aorq.html" title="What are administrative rights?" target="_blank">a user with administrative rights</a>.</p>
<p>And then <a href="http://blog.chromium.org/2011/06/introducing-non-admin-chrome-frame.html" title="Introducing Non-Admin Chrome Frame" target="_blank">the &#8220;non-admin&#8221; Chrome Frame came around</a>. It&#8217;s a Chrome Frame that can be installed by any user on a client computer. Now, there&#8217;s virtually no reason anymore <strong>not</strong> to install it if you&#8217;re stuck on a legacy Internet Explorer for some reason.</p>
<h2>Getting users to install Chrome Frame</h2>
<p>Now, in order to be able to deliver all the cutting-edge HTML 5 rainbow goodness for your clients, you just have to make sure that install the Google Chrome Frame. How to do that?</p>
<p>Just include the following inside your markup, preferably right before the closing <code></body></code> tag:</p>

<div class="wp_codebox"><table><tr id="p137727"><td class="code" id="p1377code27"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!--[if lt IE 7 ]&gt;</span>
<span style="color: #808080; font-style: italic;">  &lt;script src=&quot;//ajax.googleapis.com/ajax/libs/chrome-frame/1.0.3/CFInstall.min.js&quot;&gt;&lt;/script&gt;</span>
<span style="color: #808080; font-style: italic;">  &lt;script&gt;window.attachEvent(&quot;onload&quot;,function(){CFInstall.check({mode:&quot;overlay&quot;})})&lt;/script&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;![endif]--&gt;</span></pre></td></tr></table></div>

<p>That&#8217;s pretty much it. If your webpage is loaded inside an Internet Explorer 6 or lower, a <strong>very</strong> prominent overlay attempts to convince your user to install Chrome Frame.</p>
<p><a href="http://richardneililagan.com/wp-content/uploads/2012/01/chromeframe.png"><img src="http://richardneililagan.com/wp-content/uploads/2012/01/chromeframe.png" alt="Learning from the HTML5 Boilerplate, Part 3" title="Google Chrome Frame" width="545" height="414" class="aligncenter size-full wp-image-1378" /></a></p>
<p>Of course, to make the most use of Chrome Frame, you&#8217;ll have to include the appropriate <code>X-UA-Compatible</code> value as detailed in <a href="http://richardneililagan.com/2012/01/learning-html5-boilerplate-part-2/" title="Learning from the HTML5 Boilerplate, Part 2" target="_blank">this HTML5 Boilerplate article</a>.</p>
<p>If you want to know more about the Google Chrome Frame, <a href="http://www.chromium.org/developers/how-tos/chrome-frame-getting-started/chrome-frame-faq" title="Google Chrome Frame FAQ" target="_blank">hop on over to the FAQ</a>.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/richardneililagan?a=xCAjbc0_xeQ:7OmQYLAA3LQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/richardneililagan?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/richardneililagan?a=xCAjbc0_xeQ:7OmQYLAA3LQ:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/richardneililagan?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/richardneililagan?a=xCAjbc0_xeQ:7OmQYLAA3LQ:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/richardneililagan?i=xCAjbc0_xeQ:7OmQYLAA3LQ:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/richardneililagan/~4/xCAjbc0_xeQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://richardneililagan.com/2012/01/learning-html5-boilerplate-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://richardneililagan.com/2012/01/learning-html5-boilerplate-part-3/</feedburner:origLink></item>
		<item>
		<title>About SOPA and PIPA, aka Why the Blackout aka On Being Filipino</title>
		<link>http://feedproxy.google.com/~r/richardneililagan/~3/rxQpO2V-u6Q/</link>
		<comments>http://richardneililagan.com/2012/01/sopa-pipa-aka-blackout-aka-filipino/#comments</comments>
		<pubDate>Wed, 18 Jan 2012 13:44:33 +0000</pubDate>
		<dc:creator>Richard Neil Ilagan</dc:creator>
				<category><![CDATA[Other Computers and Technology]]></category>
		<category><![CDATA[blackout]]></category>
		<category><![CDATA[pipa]]></category>
		<category><![CDATA[sopa]]></category>
		<category><![CDATA[stop pipa]]></category>
		<category><![CDATA[stop sopa]]></category>

		<guid isPermaLink="false">http://richardneililagan.com/?p=1368</guid>
		<description><![CDATA[<p><a href="http://richardneililagan.com/2012/01/sopa-pipa-aka-blackout-aka-filipino/">About SOPA and PIPA, aka Why the Blackout aka On Being Filipino</a> was originally published in <a href="http://richardneililagan.com">Tech+Life+Music</a>.</p><p>Why the blackout? Why the care for stopping SOPA and PIPA?</p>]]></description>
				<content:encoded><![CDATA[<p><a href="http://richardneililagan.com/2012/01/sopa-pipa-aka-blackout-aka-filipino/">About SOPA and PIPA, aka Why the Blackout aka On Being Filipino</a> was originally published in <a href="http://richardneililagan.com">Tech+Life+Music</a>.</p><p><img src="http://richardneililagan.com/wp-content/uploads/2012/01/sopa.png" alt="About SOPA and PIPA, aka Why the Blackout aka On Being Filipino" title="This is what the web could like under SOPA / PIPA." width="545" height="250" class="aligncenter size-full wp-image-1370" /></p>
<p>This blog is written by a Filipino, maintained by a Filipino, and proudly Filipino. Most of the content were typed by my own hand, code crafted by my own mind, and none of it is infringing copyright in whatsoever way (hooray for open source).</p>
<p>So why the blackout? Why the care for stopping SOPA and PIPA?</p>
<p><span id="more-1368"></span></p>
<aside>Want to know more about SOPA and PIPA? Hop on over to the <a href="http://www.sopablackout.org/learnmore/" title="SOPA Blackout" target="_blank">SOPA Blackout landing page</a>.</aside>
<p>Honestly, this video gets all the explaining done quickly and effectively, so I&#8217;ll just coax you to watch it in its entirety.</p>
<p><iframe src="http://player.vimeo.com/video/31100268?byline=0&amp;portrait=0" width="545" height="307" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe></p>
<p>January 18 is Stop SOPA Day. I encourage you to join sites worldwide in protest. If you run a WordPress, Tumblr, blog or whatever, post about it / embed the video. </p>
<p>If you&#8217;re on Twitter, do tweet about this. Here, I&#8217;ll make it even easier for you:</p>
<p><a href="https://twitter.com/share" class="twitter-share-button" data-text="Help stop SOPA / PIPA." data-via="techlifemusic" data-related="techlifemusic" data-hashtags="sopa">Tweet</a><br />
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script></p>
<p>If you&#8217;re on Facebook, likewise. Post about it, tell your friends about it, and hopefully we can help generate a bit of additional buzz.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/richardneililagan?a=rxQpO2V-u6Q:zZrKTWKmhdk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/richardneililagan?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/richardneililagan?a=rxQpO2V-u6Q:zZrKTWKmhdk:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/richardneililagan?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/richardneililagan?a=rxQpO2V-u6Q:zZrKTWKmhdk:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/richardneililagan?i=rxQpO2V-u6Q:zZrKTWKmhdk:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/richardneililagan/~4/rxQpO2V-u6Q" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://richardneililagan.com/2012/01/sopa-pipa-aka-blackout-aka-filipino/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://richardneililagan.com/2012/01/sopa-pipa-aka-blackout-aka-filipino/</feedburner:origLink></item>
		<item>
		<title>Learning from the HTML5 Boilerplate, Part 2</title>
		<link>http://feedproxy.google.com/~r/richardneililagan/~3/UWNX6bg3snk/</link>
		<comments>http://richardneililagan.com/2012/01/learning-html5-boilerplate-part-2/#comments</comments>
		<pubDate>Thu, 12 Jan 2012 12:22:46 +0000</pubDate>
		<dc:creator>Richard Neil Ilagan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[boilerplate]]></category>
		<category><![CDATA[html 5]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[html5 boilerplate]]></category>
		<category><![CDATA[iis]]></category>
		<category><![CDATA[web programming]]></category>
		<category><![CDATA[x-ua-compatible]]></category>

		<guid isPermaLink="false">http://richardneililagan.com/?p=1352</guid>
		<description><![CDATA[<p><a href="http://richardneililagan.com/2012/01/learning-html5-boilerplate-part-2/">Learning from the HTML5 Boilerplate, Part 2</a> was originally published in <a href="http://richardneililagan.com">Tech+Life+Music</a>.</p><p>Internet Explorer definitely bites when you're creating web pages. Learn how to make it IE behave easier, as shown by code snippets from the HTML5 Boilerplate.</p>]]></description>
				<content:encoded><![CDATA[<p><a href="http://richardneililagan.com/2012/01/learning-html5-boilerplate-part-2/">Learning from the HTML5 Boilerplate, Part 2</a> was originally published in <a href="http://richardneililagan.com">Tech+Life+Music</a>.</p><p>The HTML5 Boilerplate is well-known across web programmers as a powerful starting template for (as the project homepage puts it) building <strong>fast, robust, future-proof sites</strong>. Out of the box, it provides HTML, CSS and Javascript code arranged in such a way to drive as much <em>oomph</em> into your pages as possible. Every nook, cranny, and design decision borrows from widely-accepted standards, and is supported by countless hours of research and testing.</p>
<p>While the HTML5 Boilerplate is awesome as a starting foundation for building awesome sites, it is also packed with code snippets and patterns you should be using for your projects. Just in case you&#8217;re not using the HTML5 Boilerplate for some reason.</p>
<p><span id="more-1352"></span></p>
<h2>Making Internet Explorer behave</h2>
<p>As if it wasn&#8217;t enough that web developers had to keep track of different Internet Explorer versions, it must also be said that computers that run IE also have multiple rendering engines. That is, if a user had IE 8, for example, you can be sure that it has the IE 8 rendering engine. However, it also has a readily available mock of the IE 7 engine as well, so a version 8 Internet Explorer may render a page as if it was version 7. If that wasn&#8217;t enough, it must also be said that these rendering engines aren&#8217;t exact copies: a version 8 IE using the version 7 rendering engine will not behave in the exact same way as a true blue version 7 IE. And that sucks.</p>
<p>Circumstantially, sites viewed <em>from an intranet</em> also trigger <strong>Compatibility View</strong>, something like a glorified <em>quirks mode</em>, by default. That sucks even more.</p>
<p>As it turns out, you can explicitly tell Internet Explorer browsers to use its latest available rendering engine using a meta tag:</p>

<div class="wp_codebox"><table><tr id="p135228"><td class="code" id="p1352code28"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;meta</span> <span style="color: #000066;">http-equiv</span>=<span style="color: #ff0000;">&quot;X-UA-Compatible&quot;</span> <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;IE=edge,chrome=1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></pre></td></tr></table></div>

<p>The <code>IE=edge</code> half of the meta value tells Internet Explorer to use its latest rendering engine for your page. That, in itself, is already a boon and a half. </p>
<p>The <code>chrome=1</code> part tells it to use the Chrome Frame rendering engine if it is installed on the client computer, which basically brings Chrome rendering power to Internet Explorer (yes, even version 6).</p>
<h2>Taking it one notch higher</h2>
<p>Now, this doesn&#8217;t come without any imperfections. First of all, this won&#8217;t work completely as expected inside a conditional comment, so you might as well scratch that idea.</p>
<p>Secondly (and arguably more important to some), is that this line breaks validation. Now, to some, that can be quite a deal breaker.</p>
<p>If you&#8217;re absolutely picky about your HTML validation (&#8217;cause I&#8217;m most often not), a way to work around this is to send the meta value above as an <strong>HTTP header</strong> instead. That should work just as well.</p>
<aside>If you&#8217;re unfamiliar, right before a server sends a web page to your browser, it sends a bunch of text called <strong>HTTP headers</strong>. This chunk of text contains information that your browser needs to know to be able to recognize, render, and react to the data it will be receiving (which, for most cases, is an HTML page). The idea presented here will send the <strong>X-UA-Compatible</strong> meta value as part of the HTTP headers sent with your page, and therefore will not break validation as it is not found right smack in the middle of your HTML markup.</aside>
<p>If you are hosting from an Apache web server, you can send the <code>X-UA-Compatible</code> headers by editing your <code>httpd.conf</code> file and adding in the following:</p>

<div class="wp_codebox"><table><tr id="p135229"><td class="code" id="p1352code29"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Location</span> /mypath<span style="color: #000000; font-weight: bold;">&gt;</span></span>
    Header set X-UA-Compatible &quot;IE=edge,chrome=1&quot;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Location<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>If you are serving off an IIS server, the easiest way to send a custom header is to set a custom configuration setting in your <code>web.config</code> file:</p>

<div class="wp_codebox"><table><tr id="p135230"><td class="code" id="p1352code30"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;system.webServer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- ... --&gt;</span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;httpProtocol<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;customHeaders<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;X-UA-Compatible&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;IE=edge,chrome=1&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/customHeaders<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/httpProtocol<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- ... --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/system.webServer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Doing any of the above let&#8217;s you completely remove the <code><meta></code> tag from your markup.</p>
<p><em>You might want to read the other articles in this series as well:</em></p>
<ul>
<li><a href="http://richardneililagan.com/2012/01/learning-html5-boilerplate-part-1/" title="Learning from the HTML5 Boilerplate, Part 1">Part 1 : Conditional HTML tags</a>
<li>
</ul>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/richardneililagan?a=UWNX6bg3snk:TdxYhgzhwss:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/richardneililagan?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/richardneililagan?a=UWNX6bg3snk:TdxYhgzhwss:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/richardneililagan?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/richardneililagan?a=UWNX6bg3snk:TdxYhgzhwss:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/richardneililagan?i=UWNX6bg3snk:TdxYhgzhwss:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/richardneililagan/~4/UWNX6bg3snk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://richardneililagan.com/2012/01/learning-html5-boilerplate-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://richardneililagan.com/2012/01/learning-html5-boilerplate-part-2/</feedburner:origLink></item>
		<item>
		<title>Learning from the HTML5 Boilerplate, Part 1</title>
		<link>http://feedproxy.google.com/~r/richardneililagan/~3/GmphWu6wenM/</link>
		<comments>http://richardneililagan.com/2012/01/learning-html5-boilerplate-part-1/#comments</comments>
		<pubDate>Tue, 10 Jan 2012 14:57:07 +0000</pubDate>
		<dc:creator>Richard Neil Ilagan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[boilerplate]]></category>
		<category><![CDATA[html 5]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[html5 boilerplate]]></category>
		<category><![CDATA[web programming]]></category>

		<guid isPermaLink="false">http://richardneililagan.com/?p=1325</guid>
		<description><![CDATA[<p><a href="http://richardneililagan.com/2012/01/learning-html5-boilerplate-part-1/">Learning from the HTML5 Boilerplate, Part 1</a> was originally published in <a href="http://richardneililagan.com">Tech+Life+Music</a>.</p><p>Read about the HTML5 Boilerplate's take on conditional comments and CSS hacks, along with what alternative it proposes and why.</p>]]></description>
				<content:encoded><![CDATA[<p><a href="http://richardneililagan.com/2012/01/learning-html5-boilerplate-part-1/">Learning from the HTML5 Boilerplate, Part 1</a> was originally published in <a href="http://richardneililagan.com">Tech+Life+Music</a>.</p><p>The HTML5 Boilerplate is well-known across web programmers as a powerful starting template for (as the project homepage puts it) building <strong>fast, robust, future-proof sites</strong>. Out of the box, it provides HTML, CSS and Javascript code arranged in such a way to drive as much <em>oomph</em> into your pages as possible. Every nook, cranny, and design decision borrows from widely-accepted standards, and is supported by countless hours of research and testing.</p>
<p>While the HTML5 Boilerplate is awesome as a starting foundation for building awesome sites, it is also packed with code snippets and patterns you should be using for your projects. Just in case you&#8217;re not using the HTML5 Boilerplate for some reason.<br />
<span id="more-1325"></span></p>
<h2>Making browsers behave the same way</h2>
<p>Browsers are not made equal. And this makes our work that much harder. </p>
<p>Different browsers (and browser versions) all behave differently, and so it is not uncommon to see code like this:</p>

<div class="wp_codebox"><table><tr id="p132531"><td class="code" id="p1325code31"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;style.css&quot;</span> <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #808080; font-style: italic;">&lt;!--[if lte IE7]&gt;&lt;link href=&quot;style.ie7.css&quot; rel=&quot;stylesheet&quot; /&gt;&lt;![endif]--&gt;</span></pre></td></tr></table></div>

<p><a href="http://www.quirksmode.org/css/condcom.html" title="Conditional comments : Quirksmode" target="_blank">Conditional comments</a> such as these are meant to load different asset files into the HTML document, depending on whether or not a specific version of Internet Explorer (or even just Internet Explorer) is used. (Yes, IE is <strong>that</strong> notorious.) It&#8217;s meant to introduce additional assets that enforce additional rules that make sure that misbehaving browser quirks render as consistently as possible.</p>
<aside>While conditional comments are primarily intended for IE, CSS hacks rely on browser bugs and quirks, and can therefore be used for taming other browser quirks.</aside>
<p>Alternatively, one can also make use of <a href="http://paulirish.com/2009/browser-specific-css-hacks/" title="Browser-specific CSS hacks" target="_blank">CSS hacks</a>.</p>

<div class="wp_codebox"><table><tr id="p132532"><td class="code" id="p1325code32"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#content</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span> _margin<span style="color: #00AA00;">:</span><span style="color: #933;">20px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>CSS hacks don&#8217;t validate, and rely on browser bugs. Good luck to you when those bugs get patched.</p>
<p>Conditional comments, on the other hand, also present a few disadvantages:</p>
<ol>
<li>They introduce additional (arguably unnecessary) HTTP requests.</li>
<li>They have to be located in the <code>&lt;head&gt;</code>, which means that they can block loading and rendering.</li>
<li>Just imagine the added complexity of working with multiple stylesheets.</li>
<li>Conditional comments can significantly slow your page down due to <a href="http://webforscher.wordpress.com/2010/05/20/ie-6-slowing-down-ie-8/" title="IE6 slowing down IE8" target="_blank">a browser bug in Internet Explorer 8</a>.</li>
</ol>
<h2>Conditional HTML tags</h2>
<p>To address these, the HTML5 Boilerplate uses the following markup instead:</p>

<div class="wp_codebox"><table><tr id="p132533"><td class="code" id="p1325code33"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!--[if lt IE 7 ]&gt; &lt;html class=&quot;ie6 no-js oldie&quot;&gt; &lt;![endif]--&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;!--[if IE 7 ]&gt;    &lt;html class=&quot;ie7 no-js oldie&quot;&gt; &lt;![endif]--&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;!--[if IE 8 ]&gt;    &lt;html class=&quot;ie8 no-js oldie&quot;&gt; &lt;![endif]--&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;!--[if IE 9 ]&gt;    &lt;html class=&quot;ie9 no-js&quot;&gt; &lt;![endif]--&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;!--[if (gt IE 9)|!(IE)]&gt;&lt;!--&gt;</span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;no-js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> <span style="color: #808080; font-style: italic;">&lt;!--&lt;![endif]--&gt;</span></pre></td></tr></table></div>

<p>The HTML5 Boilerplate uses conditional comments to dynamically assign classes to the <code>&lt;html&gt;</code> tag. However, since none of these comments are used to load asset files into the page, the previously mentioned browser bug does not occur.</p>
<aside>If you really must use conditional comments to load external files into your page, make sure to include <code>&lt;!--[if IE]&gt;&lt;![endif]--&gt;</code> in your page right after the <code>&lt;head&gt;</code> tag (or as close to the top of your page as possible). This also prevents said browser bug.</aside>
<p>With this markup, your <code>&lt;html&gt;</code> tag dynamically gets assigned a corresponding <em>iex</em> class that you can target in your CSS in a valid manner:</p>

<div class="wp_codebox"><table><tr id="p132534"><td class="code" id="p1325code34"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#content</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #933;">10px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.ie7</span> <span style="color: #cc00cc;">#content</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #933;">20px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>This promotes the use of a single stylesheet for your page, and (hopefully) keeps related style rules together for easier maintainability. This markup <em>still</em> does not validate, but what&#8217;s arguably more important is that your page renders correctly in any browser nonetheless.</p>
<p>If you&#8217;re wondering what the <code>no-js</code> class is, it is used by <a href="http://www.modernizr.com/" title="Modernizr" target="_blank">Modernizr</a>, a Javascript library that helps you build future-proof pages by allowing you to detect a browser&#8217;s capabilities, and react accordingly. When it loads, it changes the <code>no-js</code> class to <code>js</code>, letting you know if Javascript is enabled in a client&#8217;s browser.</p>
<p>Modernizr is included with HTML5 Boilerplate out of the box. We&#8217;ll discuss using it effectively in a future post.</p>
<p><em>You might want to read the other articles in this series as well:</em></p>
<ul>
<li><a href="http://richardneililagan.com/2012/01/learning-html5-boilerplate-part-2/" title="Learning from the HTML5 Boilerplate, Part 2">Part 2 : X-UA-Compatible, and making Internet Explorer behave</a>
<li>
</ul>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/richardneililagan?a=GmphWu6wenM:fxhWJZvX0zw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/richardneililagan?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/richardneililagan?a=GmphWu6wenM:fxhWJZvX0zw:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/richardneililagan?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/richardneililagan?a=GmphWu6wenM:fxhWJZvX0zw:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/richardneililagan?i=GmphWu6wenM:fxhWJZvX0zw:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/richardneililagan/~4/GmphWu6wenM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://richardneililagan.com/2012/01/learning-html5-boilerplate-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://richardneililagan.com/2012/01/learning-html5-boilerplate-part-1/</feedburner:origLink></item>
		<item>
		<title>The understated beauty of jQuery’s new .on() and .off() functions</title>
		<link>http://feedproxy.google.com/~r/richardneililagan/~3/JZcrD2IAtlc/</link>
		<comments>http://richardneililagan.com/2011/12/the-understated-beauty-of-jquerys-new-on-and-off-functions/#comments</comments>
		<pubDate>Wed, 14 Dec 2011 09:18:34 +0000</pubDate>
		<dc:creator>Richard Neil Ilagan</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://richardneililagan.com/?p=1307</guid>
		<description><![CDATA[<p><a href="http://richardneililagan.com/2011/12/the-understated-beauty-of-jquerys-new-on-and-off-functions/">The understated beauty of jQuery&#8217;s new .on() and .off() functions</a> was originally published in <a href="http://richardneililagan.com">Tech+Life+Music</a>.</p><p>The new .on() and .off() functions both make you a better jQuery programmer just by using them. It's magic. No, seriously.</p>]]></description>
				<content:encoded><![CDATA[<p><a href="http://richardneililagan.com/2011/12/the-understated-beauty-of-jquerys-new-on-and-off-functions/">The understated beauty of jQuery&#8217;s new .on() and .off() functions</a> was originally published in <a href="http://richardneililagan.com">Tech+Life+Music</a>.</p><p>The latest version of jQuery (that is, 1.7.1, or 1.7.x) has been around for a good while already, and the new <code>.on()</code> and <code>.off()</code> functions <strong>still</strong> come to me as the shiniest new toys that came along with it. Not only are they shorter, more intuitive and better at what they do, but they also <strong>promote (generally) accepted jQuery good practice</strong> that has been around for quite a while.</p>
<h2>What am I talking about?</h2>
<p>If you&#8217;re any jQuery programmer worth your salt, then you know of, <strong>at the very least</strong>, <code>.bind()</code> and <code>.live()</code> when it comes to DOM event handling. The main difference being that the former is preferred for present elements, and the latter is &#8220;mandatory&#8221; for future element handling.</p>
<p>One or two rungs up the jQuery programmer ladder, and you&#8217;d know of <code>.delegate()</code> as well. If you actually use it with convicted priority over <code>.live()</code>, then you&#8217;re golden. An explanation of why would require <a href="http://www.alfajango.com/blog/the-difference-between-jquerys-bind-live-and-delegate/" title="The difference between jQuery's bind, live and delegate" target="_blank">a good grasp of how jQuery handles events behind the scenes</a>, followed by a primer on <a href="http://www.ultimatewebtips.com/why-jquery-live-is-a-bad-option-to-use/" title="Why jQuery live is a bad option to use" target="_blank">why using .live() is bad</a>.</p>
<p>It&#8217;s admittedly a pet peeve of mine. And I&#8217;d like to think that the reason why <code>.live()</code> persists so sturdily when <code>.delegate()</code> is most clearly a better choice (aside from the fact that the latter was introduced one major version step later in the jQuery lifetime), is that <code>.live()</code> <strong>is so much easier to type</strong>.</p>
<p>Isn&#8217;t it? I mean look at it.</p>

<div class="wp_codebox"><table><tr id="p130735"><td class="code" id="p1307code35"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// live</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> do_backflips<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// delegate</span>
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">delegate</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> do_backflips<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Sheer convenience.</p>
<h2>.on() and .off()</h2>
<p>Which is a big reason why I -heart heart heart- the new <code>.on()</code> and <code>.off()</code> functions in jQuery 1.7.x. It streamlines <strong>all</strong> your event handling into just two functions, whether or not you&#8217;re taking into account present or future elements.</p>
<p>If you&#8217;re not familiar, the usage for <code>.on()</code> is like this:</p>

<div class="wp_codebox"><table><tr id="p130736"><td class="code" id="p1307code36"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// for present elements</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> do_backflips<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// for future elements</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'a'</span><span style="color: #339933;">,</span> do_backflips<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><em>I&#8217;d suggest you also take a look at the official documentation for the <a href="http://api.jquery.com/on" target="_blank">on</a> and <a href="http://api.jquery.com/off" target="_blank">off</a> functions as well. Pretty spiffy.</em></p>
<p>What&#8217;s really nice is that both versions of <code>.on()</code> map to <code>.bind()</code> and <code>.delegate()</code> respectively, which has the added benefit of simplifying the question of what function to use on any given case, and cleaning up syntax. And, hey, it&#8217;s also easy and intuitive to use.</p>
<p>Additionally, <code>.live()</code> has also been deprecated as of jQuery 1.7.x. I quote:</p>
<blockquote><p>
As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().
</p></blockquote>
<p>Frankly, one could argue that good practice is being forced down the programmers&#8217; throats. But as it plays on clear improvements on jQuery&#8217;s event model, I don&#8217;t mind that one bit.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/richardneililagan?a=JZcrD2IAtlc:aJE4vUKl18M:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/richardneililagan?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/richardneililagan?a=JZcrD2IAtlc:aJE4vUKl18M:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/richardneililagan?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/richardneililagan?a=JZcrD2IAtlc:aJE4vUKl18M:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/richardneililagan?i=JZcrD2IAtlc:aJE4vUKl18M:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/richardneililagan/~4/JZcrD2IAtlc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://richardneililagan.com/2011/12/the-understated-beauty-of-jquerys-new-on-and-off-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://richardneililagan.com/2011/12/the-understated-beauty-of-jquerys-new-on-and-off-functions/</feedburner:origLink></item>
	</channel>
</rss>
