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

<channel>
	<title>Mere Mortals</title>
	<atom:link href="http://blog.stannard.net.au/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.stannard.net.au</link>
	<description></description>
	<lastBuildDate>Wed, 15 Feb 2012 20:28:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Preventing Hotlinking with Nginx and NodeJS</title>
		<link>http://blog.stannard.net.au/2012/02/15/preventing-hotlinking-with-nginx-and-nodejs/</link>
		<comments>http://blog.stannard.net.au/2012/02/15/preventing-hotlinking-with-nginx-and-nodejs/#comments</comments>
		<pubDate>Wed, 15 Feb 2012 06:01:11 +0000</pubDate>
		<dc:creator>Kevan Stannard</dc:creator>
				<category><![CDATA[NodeJS]]></category>
		<category><![CDATA[Nginx]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://blog.stannard.net.au/?p=382</guid>
		<description><![CDATA[If you are running a NodeJS site via Nginx then you may be using proxy_pass to route requests from Nginx to Node. If you&#8217;d like to also prevent hot linking then you might like to first have a read of Marcel Eichner&#8217;s post on preventing hot linking which this post is based on. Then you [...]]]></description>
			<content:encoded><![CDATA[<p>If you are running a NodeJS site via Nginx then you may be using <code>proxy_pass</code> to route requests from Nginx to Node.</p>
<p>If you&#8217;d like to also prevent hot linking then you might like to first have a read of <a href="http://www.marceleichner.de/blog/prevent-hotlinking-but-enable-facebook-and-google-image-sea/">Marcel Eichner&#8217;s post on preventing hot linking</a> which this post is based on.</p>
<p>Then you can use a slightly modified version of that code which includes the <code>proxy_pass</code> directive in both of the <code>location</code> sections.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">server {
    server_name yourdomain.com www.yourdomain.com;
    location ~* (\.jpg|\.png|\.gif)$ {
        valid_referers none blocked yourdomain.com www.yourdomain.com ~\.google\. ~\.yahoo\. ~\.bing\. ~\.facebook\. ~\.fbcdn\.;
        if ($invalid_referer) {
            return 403;
        }
        proxy_pass http://127.0.0.1:8123;
    }
    location / {
        proxy_pass http://127.0.0.1:8123;
    }
}</pre></div></div>

<p>Some notes about this code:</p>
<p>In the <code>valid_referers</code> line, &#8216;blocked&#8217; allows Referers that have been blocked by a firewall, &#8216;none&#8217; allows requests with no Referer.</p>
<p>This is then followed by a list of domains and domain patterns that are also allowed. Google, Bing, etc are allowed for their image bots to access your site.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stannard.net.au/2012/02/15/preventing-hotlinking-with-nginx-and-nodejs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Another 10 Interesting JavaScript Features</title>
		<link>http://blog.stannard.net.au/2011/06/25/another-10-interesting-javascript-features/</link>
		<comments>http://blog.stannard.net.au/2011/06/25/another-10-interesting-javascript-features/#comments</comments>
		<pubDate>Sat, 25 Jun 2011 06:49:49 +0000</pubDate>
		<dc:creator>Kevan Stannard</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://blog.stannard.net.au/?p=291</guid>
		<description><![CDATA[I previously posted about 10 Interesting JavaScript Features. Here&#8217;s ten more JavaScript features that I&#8217;ve recently found interesting. 1. Dynamically call object methods Javascript objects can contain functions as object members. Object members can be referenced using square bracket [] notation. Combining these ideas together we can dynamically call object methods: var foo = &#123; [...]]]></description>
			<content:encoded><![CDATA[<p>I previously posted about <a href="/2011/01/07/10-interesting-javascript-features/">10 Interesting JavaScript Features</a>. Here&#8217;s ten more JavaScript features that I&#8217;ve recently found interesting.</p>
<p><span id="more-291"></span></p>
<h2>1. Dynamically call object methods</h2>
<p>Javascript objects can contain functions as object members.  Object members can be referenced using square bracket [] notation. Combining these ideas together we can dynamically call object methods:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	one<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> console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;one&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	two<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> console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;two&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> ok <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> foo<span style="color: #009900;">&#91;</span>ok <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;one&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;two&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> result <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Prints &quot;one&quot;</span></pre></div></div>

<h2>2. Multi line strings</h2>
<p>You can create multi-line strings in JavaScript by terminating lines that should be continued with a backslash:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Goodbye <span style="color: #000099; font-weight: bold;">\</span>
cruel <span style="color: #000099; font-weight: bold;">\</span>
world&quot;</span><span style="color: #339933;">;</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> s <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;Goodbye cruel world&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span></pre></div></div>

<h2>3. Recursion in anonymous functions</h2>
<p>JavaScript allows functions to be created that have no names. These are called anonymous functions. A recursive function is one that calls itself, but how does a function call itself if it has no name?</p>
<p>First let&#8217;s consider a small program that applies a factorial function to numbers in an array.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Our recursive factorial function</span>
<span style="color: #003366; font-weight: bold;">var</span> factorial <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> n<span style="color: #339933;">&lt;=</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">?</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">:</span> n <span style="color: #339933;">*</span> factorial<span style="color: #009900;">&#40;</span>n<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Function that applies a function to each element of the array</span>
<span style="color: #003366; font-weight: bold;">var</span> applyTo <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>array<span style="color: #339933;">,</span>fn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> len<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> len<span style="color: #339933;">=</span>array.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>len<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		array<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> fn<span style="color: #009900;">&#40;</span> array<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">return</span> array<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Test our function</span>
<span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> applyTo<span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> factorial <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> result <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Prints [2, 24, 720]</span></pre></div></div>

<p>Rather that defining the factorial function separately, we can define it inline as a parameter to the applyTo() function call.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> applyTo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> 
                     <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                         <span style="color: #000066; font-weight: bold;">return</span> n<span style="color: #339933;">&lt;=</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">?</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">:</span> n <span style="color: #339933;">*</span> <span style="color: #339933;">???</span>WhatGoesHere<span style="color: #339933;">???</span><span style="color: #009900;">&#40;</span>n<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                     <span style="color: #009900;">&#125;</span>
                 <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Previously, JavaScript allowed us to replace the <em>???WhatGoesHere???</em> part with <strong>arguments.callee</strong>, which was a reference to the current function.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> applyTo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> 
                     <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                         <span style="color: #000066; font-weight: bold;">return</span> n<span style="color: #339933;">&lt;=</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">?</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">:</span> n <span style="color: #339933;">*</span> arguments.<span style="color: #660066;">callee</span><span style="color: #009900;">&#40;</span>n<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                     <span style="color: #009900;">&#125;</span>
                 <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This method still works in modern browsers but has been <a href="https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope/arguments/callee">deprecated</a>, so remember what it means but <strong>don&#8217;t use it</strong>.</p>
<p>The new method allows us to name our inline functions.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> applyTo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> 
                     <span style="color: #003366; font-weight: bold;">function</span> fact<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                         <span style="color: #000066; font-weight: bold;">return</span> n<span style="color: #339933;">&lt;=</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">?</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">:</span> n <span style="color: #339933;">*</span> fact<span style="color: #009900;">&#40;</span>n<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                     <span style="color: #009900;">&#125;</span>
                 <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>4. Short cuts with &#8216;or&#8217; and &#8216;and&#8217; operators</h2>
<p>When you <strong>or</strong> together several variables in a statement, JavaScript will return first &#8216;truthy&#8217; value it finds (see <a href="/2011/01/07/10-interesting-javascript-features/">previous post</a> about truthy values). This is useful when you want to use a default value if an existing variable is undefined.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Create an empty person object with no properties</span>
<span style="color: #003366; font-weight: bold;">var</span> person <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// person.firstName is undefined, so it returns 'unknown'</span>
<span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">name</span> <span style="color: #339933;">=</span> person.<span style="color: #660066;">firstName</span> <span style="color: #339933;">||</span> <span style="color: #3366CC;">'unknown'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Prints 'unknown'</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In contrast, the <strong>and</strong> operator returns the last element, but only if <strong>all</strong> of the expressions are truthy, else it returns undefined.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> o <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
o.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
o.<span style="color: #660066;">y</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
o.<span style="color: #660066;">z</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> n <span style="color: #339933;">=</span> o.<span style="color: #660066;">x</span> <span style="color: #339933;">&amp;&amp;</span> o.<span style="color: #660066;">y</span> <span style="color: #339933;">&amp;&amp;</span> o.<span style="color: #660066;">z</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Prints 3</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>5. A note about &#8216;undefined&#8217;</h2>
<p>It turns out that <strong>undefined</strong> is not a JavaScript keyword, instead undefined is a global variable, which in browsers is defined as window.undefined.</p>
<p>It is not uncommon to see code that tests if a variable is undefined like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>someObject.<span style="color: #660066;">x</span> <span style="color: #339933;">===</span> undefined<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// do something</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Which is the same as writing</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>someObject.<span style="color: #660066;">x</span> <span style="color: #339933;">===</span> window.<span style="color: #660066;">undefined</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// do something</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If by some chance your window.undefined variable is modified then checks for undefined like this will fail. No conscientious developer would ever do this intentionally, but it may happen by accident causing hard to find bugs, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> o <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
window<span style="color: #009900;">&#91;</span>o.<span style="color: #000066;">name</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'unknown'</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// BLAM!, window.undefined is now a string</span></pre></div></div>

<p>A better way to test for undefined is to use the <strong>typeof</strong> operator. Typeof an undefined variable will always return the string &#8216;undefined&#8217;.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Good way to test for undefined</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> someObject.<span style="color: #660066;">x</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// do something</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>6. Return statement</h2>
<p>The return statement needs it&#8217;s value to be on the same line as the return keyword. This may cause problem depending on your coding style. For example</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// This is ok</span>
<span style="color: #000066; font-weight: bold;">return</span> x<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// This returns undefined</span>
<span style="color: #000066; font-weight: bold;">return</span>
    x<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// This is ok</span>
<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>
    result<span style="color: #339933;">:</span><span style="color: #3366CC;">'success'</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// This returns undefined</span>
<span style="color: #000066; font-weight: bold;">return</span>
<span style="color: #009900;">&#123;</span>
    result<span style="color: #339933;">:</span><span style="color: #3366CC;">'success'</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This behaviour is due to <a href="http://www.google.com/search?q=JavaScript+automatic+semicolon+insertion">JavaScript&#8217;s automatic semicolon insertion</a>.</p>
<h2>7. Length of a function</h2>
<p>JavaScript functions have a length property that provides a count of the number of arguments the function expects.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> foo<span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span>b<span style="color: #339933;">,</span>c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> foo.<span style="color: #660066;">length</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Prints 3</span></pre></div></div>

<h2>8. The plus operator</h2>
<p>The plus operator can be used to easily convert anything into a number by simply prefixing the value with &#8220;+&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;0xFF&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// 255</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;010&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #006600; font-style: italic;">// 10 </span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">+</span><span style="color: #003366; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #006600; font-style: italic;">// 0</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">+</span><span style="color: #003366; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #006600; font-style: italic;">// 1</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">+</span><span style="color: #003366; font-weight: bold;">false</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #006600; font-style: italic;">// 0</span></pre></div></div>

<p>This is actually a just a nice shortcut for using the <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Number">Number()</a> function.</p>
<h2>9. Object property names can be any string</h2>
<p>JavaScript object members can have any string as their name.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> o <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;What the!&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'a'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;&lt;a href='#'&gt;Hash&lt;/a&gt;&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'b'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;*****&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'c'</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> key <span style="color: #000066; font-weight: bold;">in</span> o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> key <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Which prints:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">What the!
&lt;a href='#'&gt;Hash&lt;/a&gt;
*****</pre></div></div>

<h2>10. The &#8216;debugger&#8217; statement</h2>
<p>In the past we were limited to using <strong>alert()</strong> statements to debug our JavaScript. After that we were rescued with the much more sane <strong>console.log()</strong>. Things have come a long way and we now have solid line debugging available in modern browsers.</p>
<p>However, there is one addition to our debugging arsenal which I&#8217;ve found particularly invaluable when debugging JavaScript in Internet Explorer; the <strong>debugger</strong> statement.</p>
<p>When you add the <strong>debugger</strong> statement to your code the browser will stop execution and open the debugging environment ready for you to continue stepping through your code.</p>
<p>This is moderately useful on Chrome, Firefox and Safari but I have found essential for debugging IE.</p>
<p>Note that within IE, this statement only works in IE7 and above and you need to first start the debug mode. </p>
<p>OK, so that&#8217;s 10 features, but just one more thing to wrap up which is not really a JavaScript feature.</p>
<h2>JavaScript Coding Coventions</h2>
<p>If you&#8217;re not following a JavaScript coding convention then here are a couple to get you started:</p>
<ul>
<li><a href="http://javascript.crockford.com/code.html">Douglas Crockford&#8217;s JavaScript Coding Conventions</a></li>
<li><a href="http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml">Google&#8217;s JavaScript Style Guide</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.stannard.net.au/2011/06/25/another-10-interesting-javascript-features/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Should you use semicolons in JavaScript?</title>
		<link>http://blog.stannard.net.au/2011/02/10/should-you-use-semicolons-in-javascript/</link>
		<comments>http://blog.stannard.net.au/2011/02/10/should-you-use-semicolons-in-javascript/#comments</comments>
		<pubDate>Thu, 10 Feb 2011 03:40:11 +0000</pubDate>
		<dc:creator>Kevan Stannard</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[ASI]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Convention]]></category>
		<category><![CDATA[Semicolon]]></category>

		<guid isPermaLink="false">http://blog.stannard.net.au/?p=342</guid>
		<description><![CDATA[There has been a quite a bit of chatter recently on whether or not you should use semicolons in your JavaScript code. JavaScript provides a feature called Automatic Semicolon Insertion (ASI). For the most part there are rarely problems in omitting semicolons, but there are a few cases where semicolons are required to prevent syntax [...]]]></description>
			<content:encoded><![CDATA[<p>There has been a quite a bit of chatter recently on whether or not you should use semicolons in your JavaScript code.</p>
<p>JavaScript provides a feature called Automatic Semicolon Insertion (ASI). For the most part there are rarely problems in omitting semicolons, but there are a few cases where semicolons are required to prevent syntax errors or resolve code ambiguities.</p>
<p><span id="more-342"></span></p>
<p>A site that triggered a lot of chatter was <a href="http://aresemicolonsnecessaryinjavascript.com/">aresemicolonsnecessaryinjavascript.com</a> which boldly states &#8216;NO&#8217;. The author is really trying to <a href="http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding">explain</a> we should not just include semicolons everywhere due a fear that our code might break in some environments or parsers (such as JavaScript minifiers). Others are also continuing the same point that we should better understand why <a href="http://mislav.uniqpath.com/2010/05/semicolons/">semicolons in JavaScript are optional</a>. </p>
<p>Douglas Crockford, a leading expert in JavaScript <a href="http://javascript.crockford.com/code.html">recommends  using semicolons at the end of every simple statement</a> and his ubiqitious JavaScript code quality tool, <a href="http://www.jslint.com/">JSLint</a>, by default expects semicolons to be inserted. A poll on Stack Overflow strongly <a href="http://stackoverflow.com/questions/444080/do-you-recommend-using-semicolons-after-every-statement-in-javascript">recommends using semicolons everywhere</a>.</p>
<p>Lastly, I found Armin Ronacher&#8217;s post on <a href="http://lucumr.pocoo.org/2011/2/6/automatic-semicolon-insertion/">dealing with JavaScript&#8217;s automatic semicolon insertion</a> to be most useful. In particular his summary where he writes:</p>
<blockquote><p>
Are Semicolons Necessary in Javascript? Despite popular belief the answer is &quot;sometimes&quot; and not &quot;no&quot;. But to save yourself time and troubles, just place them all the time. Not only will it save yourself some headaches, your code will also look more consistent. Because there will be situations where a semicolon becomes necessary to resolve ambiguities.
</p></blockquote>
<p>So what will I be doing? My vote goes to following Douglas Crockford&#8217;s <a href="http://javascript.crockford.com/code.html">JavaScript coding guidelines</a> for now and I&#8217;ll be happily including semicolons in my code.</p>
<p>If you are interested, there are also a couple of discussions on Reddit regarding these couple of posts; <a href="http://www.reddit.com/r/programming/comments/fgjxc/dealing_with_javascripts_automatic_semicolon/">Dealing with JavaScript&#8217;s automatic semicolon insertion</a> and <a href="http://www.reddit.com/r/javascript/comments/ffy3c/are_semicolons_necessary_in_javascript/">Are semicolons necessary in JavaScript?</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stannard.net.au/2011/02/10/should-you-use-semicolons-in-javascript/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Creating namespaces in JavaScript</title>
		<link>http://blog.stannard.net.au/2011/01/14/creating-namespaces-in-javascript/</link>
		<comments>http://blog.stannard.net.au/2011/01/14/creating-namespaces-in-javascript/#comments</comments>
		<pubDate>Fri, 14 Jan 2011 20:32:33 +0000</pubDate>
		<dc:creator>Kevan Stannard</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Namespace]]></category>

		<guid isPermaLink="false">http://blog.stannard.net.au/?p=322</guid>
		<description><![CDATA[In the past it was very common to see global variables in snippets of JavaScript code across the web, such as: name = &#34;Spock&#34;; function greeting&#40;&#41; &#123; return &#34;Hello &#34; + name; &#125; A better approach is to place all of your code within a namespace; an object that contains all of your code and [...]]]></description>
			<content:encoded><![CDATA[<p>In the past it was very common to see global variables in snippets of JavaScript code across the web, such as:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Spock&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> greeting<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;Hello &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066;">name</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A better approach is to place all of your code within a <strong>namespace</strong>; an object that contains all of your code and helps to prevent name clashes with JavaScript code written by others.</p>
<p><span id="more-322"></span></p>
<p>The simplest method of creating a namespace is to use an object literal.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
foo.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Spock&quot;</span><span style="color: #339933;">;</span>
foo.<span style="color: #660066;">greeting</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;Hello &quot;</span> <span style="color: #339933;">+</span> foo.<span style="color: #000066;">name</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This can also be specified using a different syntax.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Spock&quot;</span><span style="color: #339933;">,</span>
&nbsp;
	greeting<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;Hello &quot;</span> <span style="color: #339933;">+</span> foo.<span style="color: #000066;">name</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This approach is better that having outright global variables, but it exposes everything within the namespace as global. In other words both foo.name and foo.greeting are available everywhere.</p>
<p>Another problem with this approach is that greeting needs to refer to &#8216;foo.name&#8217; rather than just &#8216;name&#8217;.</p>
<p>Another method of creating a namespace is through the use of a self executing function.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// Create a private variable</span>
	<span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Spock&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// Create a private function</span>
	<span style="color: #003366; font-weight: bold;">var</span> greeting <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;Hello &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066;">name</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// Return an object that exposes our greeting function publicly</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>
		greeting<span style="color: #339933;">:</span> greeting
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here, when the function is executed it creates a private variable and a private inner function. The inner function can refer to the private variable directly. The main function then returns an object literal containing a reference to the greeting private function &#8211; this then exposes the greeting function as a public function so we can call it via the foo namespace.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>foo.<span style="color: #660066;">greeting</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;Hello Spock&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span></pre></div></div>

<p>This post was inspired by a good post from David B. Calhoun about <a href="http://davidbcalhoun.com/2011/how-to-spot-outdated-javascript">spotting outdated JavaScript</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stannard.net.au/2011/01/14/creating-namespaces-in-javascript/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to use sessions on Google App Engine with Python and gae-sessions?</title>
		<link>http://blog.stannard.net.au/2011/01/09/how-to-use-sessions-on-google-app-engine-with-python-and-gae-sessions/</link>
		<comments>http://blog.stannard.net.au/2011/01/09/how-to-use-sessions-on-google-app-engine-with-python-and-gae-sessions/#comments</comments>
		<pubDate>Sun, 09 Jan 2011 11:08:49 +0000</pubDate>
		<dc:creator>Kevan Stannard</dc:creator>
				<category><![CDATA[Google App Engine]]></category>
		<category><![CDATA[GAE]]></category>
		<category><![CDATA[GAE-Session]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Session]]></category>

		<guid isPermaLink="false">http://blog.stannard.net.au/?p=304</guid>
		<description><![CDATA[Google App Engine with Python does not provide built in session capabilities. This step by step walkthrough sets up a Google App Engine app using the lightweight gae-sessions utility. 1. Download gae-sessions Download the gae-sessions code from https://github.com/dound/gae-sessions/ 2. Create your app directory Create a directory for your application. I&#8217;ll be using gaesessiontest/ 3. Copy [...]]]></description>
			<content:encoded><![CDATA[<p>Google App Engine with Python does not provide built in session capabilities. This step by step walkthrough sets up a Google App Engine app using the lightweight <a href="https://github.com/dound/gae-sessions/">gae-sessions</a> utility.</p>
<p><span id="more-304"></span></p>
<h2>1. Download gae-sessions</h2>
<p>Download the gae-sessions code from <a href="https://github.com/dound/gae-sessions/">https://github.com/dound/gae-sessions/</a></p>
<h2>2. Create your app directory</h2>
<p>Create a directory for your application. I&#8217;ll be using <strong>gaesessiontest/</strong></p>
<h2>3. Copy gaesessions to your app</h2>
<p>From the gae-sessions download file, copy the <strong>gaesessions/</strong> directory to your app directory.</p>
<h2>4. Create your app.yaml file</h2>
<p>Within your app directory create an <strong>app.yaml</strong> file with the contents:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">application: gaesessiontest
version: 1
runtime: python
api_version: 1
&nbsp;
handlers:
- url: /.*
  script: main.py</pre></div></div>

<h2>5. Create your appengine_config.py file</h2>
<p>Within your app directory create an <strong>appengine_config.py</strong> file with the contents:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> gaesessions <span style="color: #ff7700;font-weight:bold;">import</span> SessionMiddleware
<span style="color: #ff7700;font-weight:bold;">def</span> webapp_add_wsgi_middleware<span style="color: black;">&#40;</span>app<span style="color: black;">&#41;</span>:
    app = SessionMiddleware<span style="color: black;">&#40;</span>app, cookie_key=<span style="color: #483d8b;">&quot;You must change this&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> app</pre></div></div>

<h2>6. Change the cookie_key</h2>
<p>Change the <strong>cookie_key</strong> value to a secret combination of characters.</p>
<h2>7. Create your main.py file</h2>
<p>Within your app directory create a <strong>main.py</strong> file with the contents:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> google.<span style="color: black;">appengine</span>.<span style="color: black;">ext</span> <span style="color: #ff7700;font-weight:bold;">import</span> webapp
<span style="color: #ff7700;font-weight:bold;">from</span> google.<span style="color: black;">appengine</span>.<span style="color: black;">ext</span>.<span style="color: black;">webapp</span>.<span style="color: black;">util</span> <span style="color: #ff7700;font-weight:bold;">import</span> run_wsgi_app
<span style="color: #ff7700;font-weight:bold;">from</span> google.<span style="color: black;">appengine</span>.<span style="color: black;">ext</span>.<span style="color: black;">webapp</span> <span style="color: #ff7700;font-weight:bold;">import</span> template
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> gaesessions <span style="color: #ff7700;font-weight:bold;">import</span> get_current_session
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MainPage<span style="color: black;">&#40;</span>webapp.<span style="color: black;">RequestHandler</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> get<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
&nbsp;
        <span style="color: #808080; font-style: italic;"># Get the current session        </span>
        session = get_current_session<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># Get the value of the counter,</span>
        <span style="color: #808080; font-style: italic;"># defaulting to 0 if not present</span>
        counter = session.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'counter'</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># Increment the counter</span>
        session<span style="color: black;">&#91;</span><span style="color: #483d8b;">'counter'</span><span style="color: black;">&#93;</span> =  counter + <span style="color: #ff4500;">1</span> 
&nbsp;
        context = <span style="color: black;">&#123;</span>
             <span style="color: #483d8b;">&quot;counter&quot;</span>: counter 
        <span style="color: black;">&#125;</span>
&nbsp;
        path = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">dirname</span><span style="color: black;">&#40;</span>__file__<span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'index.html'</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">response</span>.<span style="color: black;">out</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span>template.<span style="color: black;">render</span><span style="color: black;">&#40;</span>path, context<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
application = webapp.<span style="color: black;">WSGIApplication</span><span style="color: black;">&#40;</span>
                                     <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/'</span>, MainPage<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>,
                                     debug=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    run_wsgi_app<span style="color: black;">&#40;</span>application<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<h2>8. Create your index.html file</h2>
<p>Within your app directory create an <strong>index.html</strong> file with the contents:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span> <span style="color: #000066;">lang</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;en&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>GAE Sessions<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
Counter = {{ counter}}
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<h2>9. Run your application</h2>
<p>My GAE installation is located in /opt/google_appengine/ so I start the application with the command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>google_appengine<span style="color: #000000; font-weight: bold;">/</span>dev_appserver.py gaesessiontest</pre></div></div>

<h2>10. View your app</h2>
<p>Open your browser and go to <a href="http://localhost:8080/">http://localhost:8080/</a>. You should see a counter that increments each time you refresh the page.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stannard.net.au/2011/01/09/how-to-use-sessions-on-google-app-engine-with-python-and-gae-sessions/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>10 Interesting JavaScript Features</title>
		<link>http://blog.stannard.net.au/2011/01/07/10-interesting-javascript-features/</link>
		<comments>http://blog.stannard.net.au/2011/01/07/10-interesting-javascript-features/#comments</comments>
		<pubDate>Fri, 07 Jan 2011 06:05:12 +0000</pubDate>
		<dc:creator>Kevan Stannard</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[Block]]></category>
		<category><![CDATA[Bool]]></category>
		<category><![CDATA[Equality]]></category>
		<category><![CDATA[Exception]]></category>
		<category><![CDATA[Falsy]]></category>
		<category><![CDATA[Finally]]></category>
		<category><![CDATA[Float]]></category>
		<category><![CDATA[Length]]></category>
		<category><![CDATA[New]]></category>
		<category><![CDATA[Number]]></category>
		<category><![CDATA[Object]]></category>
		<category><![CDATA[Scope]]></category>
		<category><![CDATA[Strict]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[Throw]]></category>
		<category><![CDATA[Truthy]]></category>

		<guid isPermaLink="false">http://blog.stannard.net.au/?p=226</guid>
		<description><![CDATA[I&#8217;ve been doing some more reading on JavaScript recently and decided to note down some of what were interesting features for me. If you&#8217;ve been doing JavaScript for a while then please let me know if anything looks a bit off, otherwise I hope you find the list an interesting read! 1. There is only [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been doing some more reading on JavaScript recently and decided to note down some of what were interesting features for me. If you&#8217;ve been doing JavaScript for a while then please let me know if anything looks a bit off, otherwise I hope you find the list an interesting read!</p>
<p><span id="more-226"></span></p>
<h2>1. There is only one number type in JavaScript</h2>
<p>All JavaScript numbers are internally represented as 64 bit floating point values. There is no separate integer type.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">a <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
b <span style="color: #339933;">=</span> <span style="color: #CC0000;">1.0</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>a <span style="color: #339933;">===</span> b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span></pre></div></div>

<p>We need to take care with floating point arithmetic as it may not always produce the exact answer you were expecting. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.1</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.2</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> c <span style="color: #339933;">=</span> a <span style="color: #339933;">+</span> b<span style="color: #339933;">;</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>c <span style="color: #339933;">===</span> <span style="color: #CC0000;">0.3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>c <span style="color: #339933;">===</span> <span style="color: #CC0000;">0.30000000000000004</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span></pre></div></div>

<h2>2. Blocks in JavaScript do not create a new scope</h2>
<p>In some languages (like Java) new variables created within blocks such as if, while and for only exist within that block. JavaScript does not have a block scope.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// This overwrites the value in s</span>
	<span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;bar&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>s <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;bar&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// true;</span></pre></div></div>

<h2>3. You can create anonymous self executing functions</h2>
<p>An anonymous self executing function is just a function that has no name and is executed immediately. It can be useful to induce a block scope and can be useful when dealing with closures within loops.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// This does not overwrite the value in s</span>
		<span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;bar&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>s <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// true;</span></pre></div></div>

<p>If you haven&#8217;t seen a function like this before, it&#8217;s built up like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// 1. Create the anonymous function</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;">// Do something</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// 2. Wrap it in parentheses</span>
<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Do something</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// 3. Put () on the end to execute it, just like any normal function call.</span>
<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Do something</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Not very pretty but is an important concept.</p>
<h2>4. You can throw objects for exceptions</h2>
<p>If you need to throw an exception, rather than just throwing a message string, throw an object that contains a name and a message.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>accountFound<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">throw</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'AccountFetchFailed'</span><span style="color: #339933;">,</span>
		message<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Sorry, there was a problem fetching the account details'</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>When catching the error you can then test which kind of exception occurred and react accordingly.</p>
<h2>5. JavaScript&#8217;s try catch has a finally clause</h2>
<p>A finally clause in a JavaScript exception will <strong>always</strong> execute, whether or not an exception was caught. The finally clause will <strong>still execute</strong> even if there is an error or a break in program flow within the catch clause (such as by a return or break statement).</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> testTryCatch<span style="color: #009900;">&#40;</span>hasError<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
		console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;In the try clause&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Has Error: &quot;</span> <span style="color: #339933;">+</span> hasError<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>hasError<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Throwing an exception&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">throw</span> <span style="color: #3366CC;">&quot;Oops!&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>			
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;In the catch clause: &quot;</span> <span style="color: #339933;">+</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #006600; font-style: italic;">// This return only occurs *after* the finally clause has executed</span>
		<span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
		console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;In the finally clause&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Testing with no error:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">testTryCatch<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Output:</span>
<span style="color: #006600; font-style: italic;">//   In the try clause</span>
<span style="color: #006600; font-style: italic;">//   Has Error: false</span>
<span style="color: #006600; font-style: italic;">//   In the finally clause</span></pre></div></div>

<p>Testing with an error:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">testTryCatch<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Output:</span>
<span style="color: #006600; font-style: italic;">//   In the try clause</span>
<span style="color: #006600; font-style: italic;">//   Has Error: true</span>
<span style="color: #006600; font-style: italic;">//   Throwing an exception</span>
<span style="color: #006600; font-style: italic;">//   In the catch clause: Oops!</span>
<span style="color: #006600; font-style: italic;">//   In the finally clause</span></pre></div></div>

<h2>6. You can explicitly set an array&#8217;s length property</h2>
<p>Increasing the length causes any missing elements to have an undefined value. Reducing the length deletes elements beyond that length.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;one&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;two&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;three&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;four&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;five&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> arr <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// [&quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;, &quot;five&quot;]</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> arr.<span style="color: #660066;">length</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 5</span>
&nbsp;
arr.<span style="color: #660066;">length</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">8</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> arr <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// [&quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;, &quot;five&quot;, undefined, undefined, undefined]</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> arr.<span style="color: #660066;">length</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 8</span>
&nbsp;
arr.<span style="color: #660066;">length</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> arr <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// [&quot;one&quot;, &quot;two&quot;, &quot;three&quot;]</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> arr.<span style="color: #660066;">length</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 3</span></pre></div></div>

<h2>7. There are two equality operators in JavaScript</h2>
<p>Javascript has two equality operators; the regular equality operator == and the strict equality operator ===. These have corresponding inequality operators != and !==.</p>
<p>The <strong>regular equality</strong> operators == and != attempt to convert the operand types when they are not the same type. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">null</span> <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">false</span>      <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span>
<span style="color: #009900;">&#40;</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">false</span>         <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span>
<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;&quot;</span> <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">false</span>        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span>
<span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">false</span> <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">false</span>     <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span>
<span style="color: #009900;">&#40;</span> NaN <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">false</span>       <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span>
<span style="color: #009900;">&#40;</span> undefined <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">false</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span></pre></div></div>

<p>The <strong>strict equality</strong> operators === and !== does <strong>not</strong> attempt to convert the operand types. If the types are different then they are not the same. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">null</span> <span style="color: #339933;">===</span> <span style="color: #003366; font-weight: bold;">false</span>      <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span>
<span style="color: #009900;">&#40;</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">===</span> <span style="color: #003366; font-weight: bold;">false</span>         <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span>
<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;&quot;</span> <span style="color: #339933;">===</span> <span style="color: #003366; font-weight: bold;">false</span>        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span>
<span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">false</span> <span style="color: #339933;">===</span> <span style="color: #003366; font-weight: bold;">false</span>     <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span>
<span style="color: #009900;">&#40;</span> NaN <span style="color: #339933;">===</span> <span style="color: #003366; font-weight: bold;">false</span>       <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span>
<span style="color: #009900;">&#40;</span> undefined <span style="color: #339933;">===</span> <span style="color: #003366; font-weight: bold;">false</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span></pre></div></div>

<p>You should generally use the <strong>strict equality</strong> operator.</p>
<p>For more info see the <a href="https://developer.mozilla.org/en/JavaScript/Reference/Operators/Comparison_Operators">Comparison operators docs</a>.</p>
<h2>8. Falsy, Truthy and boolean values</h2>
<p>As you saw in the previous feature, within JavaScript there are several non boolean values that evaluate to false or true. This seems to have led to the terms falsy and truthy.</p>
<p>Falsy values are</p>
<ul>
<li>0</li>
<li>NaN (not a number)</li>
<li>&#8221; (empty string)</li>
<li>false</li>
<li>null</li>
<li>undefined</li>
</ul>
<p> Everything else, including numbers, non empty strings, boolean true, arrays, objects and functions are truthy.</p>
<p>If you have a situation where you must have a real boolean true or false value then you can convert the value using a double negative operator !!.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> makeBool<span style="color: #009900;">&#40;</span>thing<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #339933;">!!</span>thing<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// These are all true</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> makeBool<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">99</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> makeBool<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;hello&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> makeBool<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> makeBool<span style="color: #009900;">&#40;</span>Number.<span style="color: #660066;">POSITIVE_INFINITY</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> makeBool<span style="color: #009900;">&#40;</span>Number.<span style="color: #660066;">NEGATIVE_INFINITY</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> makeBool<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> makeBool<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> makeBool<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// These are all false</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> makeBool<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> makeBool<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> makeBool<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> makeBool<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> makeBool<span style="color: #009900;">&#40;</span>NaN<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> makeBool<span style="color: #009900;">&#40;</span>undefined<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>More details about <a href="http://blogs.sitepoint.com/2009/07/01/javascript-truthy-falsy/">Falsy and Truthy</a></p>
<h2>9. Parentheses are optional when creating new objects</h2>
<p>The parenthesis are optional when creating new objects that require no initial parameters.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> User<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> u <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> User<span style="color: #339933;">;</span></pre></div></div>

<h2>10. Hidden JavaScript features</h2>
<p>I started compiling this list as a few interesting things I found as I was reading through <a href="http://www.amazon.com/exec/obidos/ASIN/0596517742/wrrrldwideweb">JavaScript: The Good Parts</a> and doing a bit of research. </p>
<p>Along the way I found a great set of <a href="http://stackoverflow.com/questions/61088/hidden-features-of-javascript">hidden JavaScript features</a> on Stack Overflow. So check out the list for more interesting JavaScript features.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stannard.net.au/2011/01/07/10-interesting-javascript-features/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Creating a form with labels inside text fields using jQuery</title>
		<link>http://blog.stannard.net.au/2011/01/07/creating-a-form-with-labels-inside-text-fields-using-jquery/</link>
		<comments>http://blog.stannard.net.au/2011/01/07/creating-a-form-with-labels-inside-text-fields-using-jquery/#comments</comments>
		<pubDate>Fri, 07 Jan 2011 03:40:27 +0000</pubDate>
		<dc:creator>Kevan Stannard</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Form]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Label]]></category>
		<category><![CDATA[TextField]]></category>

		<guid isPermaLink="false">http://blog.stannard.net.au/?p=247</guid>
		<description><![CDATA[It&#8217;s common to see forms that have a label displayed inside text fields which then disappear when you click into the field. Let&#8217;s go through an example for a login form that demonstrates a method for doing this. First, start with a simple HTML form: &#60;form action=&#34;&#34; method=&#34;post&#34; class=&#34;login&#34;&#62; &#60;ul&#62; &#60;li&#62; &#60;div class=&#34;title&#34;&#62;Login&#60;/div&#62; &#60;/li&#62; &#60;li&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s common to see forms that have a label displayed inside text fields which then disappear when you click into the field. Let&#8217;s go through an example for a login form that demonstrates a method for doing this.</p>
<p><span id="more-247"></span><br />
First, start with a simple HTML form:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;login&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;title&quot;</span>&gt;</span>Login<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;username&quot;</span>&gt;</span>Username<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;username&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password&quot;</span>&gt;</span>Password<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">button</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span>&gt;</span>Login<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">button</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></div></div>

<p>Next let&#8217;s start styling up the form.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* Create a bordered box for the form */</span>
form<span style="color: #6666ff;">.login</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#eee</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">300px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">10px</span> <span style="color: #933;">20px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">5px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#ddd</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* Set the font for all of the elements */</span>
form.login<span style="color: #00AA00;">,</span>
form<span style="color: #6666ff;">.login</span> input<span style="color: #00AA00;">,</span>
form<span style="color: #6666ff;">.login</span> button <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> Helvetica<span style="color: #00AA00;">,</span> Arial<span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span><span style="color: #933;">18px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* Make our title a bit bigger */</span>
form<span style="color: #6666ff;">.login</span> div<span style="color: #6666ff;">.title</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span><span style="color: #933;">24px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span><span style="color: #993333;">bold</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin-bottom</span><span style="color: #00AA00;">:</span><span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* Remove the bullets and padding from our list */</span>
form<span style="color: #6666ff;">.login</span> ul <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">list-style-type</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* Give our input fields a fixed width and a bit of padding */</span>
form<span style="color: #6666ff;">.login</span> input <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">280px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin-bottom</span><span style="color: #00AA00;">:</span><span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>For the next step we want to have our labels displayed over the top of our input fields. So a bit of extra CSS to do that.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* Make each field container relative.
This lets us position the label absolutely inside it. */</span>
form<span style="color: #6666ff;">.login</span> ul li <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* Position the labels inside our input fields. */</span>
form<span style="color: #6666ff;">.login</span> label <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span><span style="color: #933;">8px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span><span style="color: #933;">9px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#aaa</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Lastly we need to have the labels disappear when we start editing a text field and reappear again if we leave the text field without entering a value.</p>
<p>We will use the jQuery <strong>focus()</strong> function to handle entering a text field and the <strong>blur()</strong> function to handle when leaving a field. We&#8217;ll also hide the labels for fields that have a pre-populated value.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// Find each of our input fields</span>
	<span style="color: #003366; font-weight: bold;">var</span> fields <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;form.login input&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// If a field gets focus then hide the label</span>
	<span style="color: #006600; font-style: italic;">// (which is the previous element in the DOM).</span>
	fields.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">prev</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</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>
&nbsp;
	<span style="color: #006600; font-style: italic;">// If a field loses focus and nothing has</span>
	<span style="color: #006600; font-style: italic;">// been entered in the field then show the label.</span>
	fields.<span style="color: #000066;">blur</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</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;">prev</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// If the form is pre-populated with some values</span>
	<span style="color: #006600; font-style: italic;">// then immediately hide the corresponding labels. </span>
	fields.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</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;">prev</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>However jQuery supports function chaining, so perhaps a nicer way to write this may be:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;form.login input&quot;</span><span style="color: #009900;">&#41;</span>
		.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</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;">prev</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
		.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">prev</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</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: #000066;">blur</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</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;">prev</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</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></div></div>

<p><strong>EDIT:</strong></p>
<p><a href="http://blog.pengoworks.com/">Dan G. Switzer, II</a> provided a slightly more succinct version of the jQuery code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;form.login input&quot;</span><span style="color: #009900;">&#41;</span>
		.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;focus.labelFx&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">prev</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</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: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;blur.labelFx&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">prev</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">!</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;show&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;hide&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</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: #660066;">trigger</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;blur.labelFx&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.stannard.net.au/2011/01/07/creating-a-form-with-labels-inside-text-fields-using-jquery/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Installing Django with Apache and mod_wsgi on Ubuntu 10.04</title>
		<link>http://blog.stannard.net.au/2010/12/11/installing-django-with-apache-and-mod_wsgi-on-ubuntu-10-04/</link>
		<comments>http://blog.stannard.net.au/2010/12/11/installing-django-with-apache-and-mod_wsgi-on-ubuntu-10-04/#comments</comments>
		<pubDate>Sat, 11 Dec 2010 00:33:43 +0000</pubDate>
		<dc:creator>Kevan Stannard</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[WSGI]]></category>

		<guid isPermaLink="false">http://blog.stannard.net.au/?p=200</guid>
		<description><![CDATA[Step by step instructions for installing Django with Apache and mod_wsgi on Ubuntu 10.04. PART 1 &#8211; Prepare the server Update the server &#62; sudo apt-get update &#62; sudo apt-get upgrade Install Apache and mod_wsgi &#62; sudo apt-get install apache2 libapache2-mod-wsgi Install setup tools and pip &#62; sudo apt-get install python-setuptools &#62; sudo apt-get install [...]]]></description>
			<content:encoded><![CDATA[<p>Step by step instructions for installing Django with Apache and mod_wsgi on Ubuntu 10.04.</p>
<p><span id="more-200"></span></p>
<h2>PART 1 &#8211; Prepare the server</h2>
<p><strong>Update the server</strong></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; sudo apt-get update
&gt; sudo apt-get upgrade</pre></div></div>

<p><strong>Install Apache and mod_wsgi</strong></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; sudo apt-get install apache2 libapache2-mod-wsgi</pre></div></div>

<p><strong>Install setup tools and pip</strong></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; sudo apt-get install python-setuptools
&gt; sudo apt-get install python-pip</pre></div></div>

<p><strong>Install Django</strong></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; sudo pip install django</pre></div></div>

<p><strong>Create a folder for storing our sites</strong></p>
<p>I&#8217;ll be placing our sites in the /srv/www directory. The /srv directory should already exist so we just need to create the /www directory</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; sudo mkdir /srv/www</pre></div></div>

<h2>PART 2 &#8211; Add host entries for testing</h2>
<p>We will set up two domains for testing the configuration<br />
- one for testing that WSGI is working, and<br />
- one for testing that Django is working.</p>
<p>My test virtual machine&#8217;s IP address is 172.16.52.130 so I&#8217;ll set up the following in my <strong>local</strong> hosts file (not on the server)</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; sudo nano /etc/hosts</pre></div></div>

<p>And add the following</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">172.16.52.130    djangoserver
172.16.52.130    wsgi.djangoserver
172.16.52.130    hello.djangoserver</pre></div></div>

<h2>PART 3 &#8211; Test WSGI is working</h2>
<p>Create our wsgi test site content</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; sudo mkdir /srv/www/wsgi
&gt; sudo nano /srv/www/wsgi/app.wsgi</pre></div></div>

<p>And add the content</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> application<span style="color: black;">&#40;</span>environ, start_response<span style="color: black;">&#41;</span>:
    status = <span style="color: #483d8b;">'200 OK'</span>
    output = <span style="color: #483d8b;">'Hello World!'</span>
&nbsp;
    response_headers = <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Content-type'</span>, <span style="color: #483d8b;">'text/plain'</span><span style="color: black;">&#41;</span>,
                        <span style="color: black;">&#40;</span><span style="color: #483d8b;">'Content-Length'</span>, <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>output<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
    start_response<span style="color: black;">&#40;</span>status, response_headers<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span>output<span style="color: black;">&#93;</span></pre></div></div>

<p>Create a new apache site</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; sudo nano /etc/apache2/sites-available/wsgi</pre></div></div>

<p>And add the content</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&lt;VirtualHost *:80&gt;
&nbsp;
    ServerName wsgi.djangoserver
    DocumentRoot /srv/www/wsgi
&nbsp;
    &lt;Directory /srv/www/wsgi&gt;
        Order allow,deny
        Allow from all
    &lt;/Directory&gt;
&nbsp;
    WSGIScriptAlias / /srv/www/wsgi/app.wsgi
&nbsp;
&lt;/VirtualHost&gt;</pre></div></div>

<p>And activate the site</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; sudo a2ensite wsgi
&gt; sudo /etc/init.d/apache2 reload</pre></div></div>

<p>Then open your web browser and browse to</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">http://wsgi.djangoserver</pre></div></div>

<p>You should see a &#8216;Hello World!&#8217; message</p>
<h2>PART 4 &#8211; Test Django is working</h2>
<p><strong>Create a new Django site</strong></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; cd /srv/www
&gt; sudo django-admin.py startproject hello</pre></div></div>

<p>Create a wsgi file for the site</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; sudo mkdir /srv/www/hello/apache
&gt; sudo nano /srv/www/hello/apache/django.wsgi</pre></div></div>

<p>And add the content</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
&nbsp;
path = <span style="color: #483d8b;">'/srv/www'</span>
<span style="color: #ff7700;font-weight:bold;">if</span> path <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span>:
    <span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span>.<span style="color: black;">insert</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #483d8b;">'/srv/www'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #dc143c;">os</span>.<span style="color: black;">environ</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'DJANGO_SETTINGS_MODULE'</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">'hello.settings'</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span>
application = django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span>.<span style="color: black;">WSGIHandler</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p><strong>Create a new apache site</strong></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; sudo nano /etc/apache2/sites-available/hello</pre></div></div>

<p>And add the content</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&lt;VirtualHost *:80&gt;
&nbsp;
    ServerName hello.djangoserver
    DocumentRoot /srv/www/hello
&nbsp;
    &lt;Directory /srv/www/hello&gt;
        Order allow,deny
        Allow from all
    &lt;/Directory&gt;
&nbsp;
    WSGIDaemonProcess hello.djangoserver processes=2 threads=15 display-name=%{GROUP}
    WSGIProcessGroup hello.djangoserver
&nbsp;
    WSGIScriptAlias / /srv/www/hello/apache/django.wsgi
&nbsp;
&lt;/VirtualHost&gt;</pre></div></div>

<p>And activate the site</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; sudo a2ensite hello
&gt; sudo /etc/init.d/apache2 reload</pre></div></div>

<p>Then open your web browser and browse to</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">http://hello.djangoserver</pre></div></div>

<p>You should see the Django default installation message.</p>
<h2>Notes</h2>
<p><strong>NOTE 1 &#8211; Running in daemon mode</strong></p>
<p>Our test Django site is configured to run in daemon mode &#8211; because of these two lines:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">WSGIDaemonProcess hello.djangoserver processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup hello.djangoserver</pre></div></div>

<p>So if we modify the code then rather than restarting apache we can just <strong>touch</strong> the wsgi file and the changes will be picked up:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; sudo touch /srv/www/hello/apache/django.wsgi</pre></div></div>

<p><strong>NOTE 2 &#8211; Specify the application module name</strong></p>
<p>It appears to be a good idea to specify the application module when specifying the DJANGO_SETTINGS_MODULE. So rather than writing this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'</pre></div></div>

<p>We should write this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">os.environ['DJANGO_SETTINGS_MODULE'] = 'hello.settings'</pre></div></div>

<h2>Useful Commands</h2>
<p><strong>Error log file</strong></p>
<p>If you get errors, check the apache log file</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; tail /var/log/apache2/error.log</pre></div></div>

<p><strong>Test using development mode</strong></p>
<p>If your app does not seem to be working using wsgi, then check if it is working via the development server.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; cd /srv/www/hello
&gt; python manage.py runserver 0:8080</pre></div></div>

<p>The in your web browser go to</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">http://hello.djangoserver:8080</pre></div></div>

<h2>References</h2>
<p>An improved WSGI script for use with Django.<br />
<a href="http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html">http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html</a></p>
<p>Modwsgi &#8211; Integration With Django<br />
<a href="http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango">http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango</a></p>
<p>How do I stop getting ImportError: Could not import settings &#8216;mofin.settings&#8217; when using django with wsgi?<br />
<a href="http://stackoverflow.com/questions/1411417/how-do-i-stop-getting-importerror-could-not-import-settings-mofin-settings-whe">http://stackoverflow.com/questions/1411417/how-do-i-stop-getting-importerror-could-not-import-settings-mofin-settings-whe</a></p>
<p>Configuration problems with django and mod_wsgi<br />
<a href="http://stackoverflow.com/questions/2587251/configuration-problems-with-django-and-mod-wsgi">http://stackoverflow.com/questions/2587251/configuration-problems-with-django-and-mod-wsgi</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stannard.net.au/2010/12/11/installing-django-with-apache-and-mod_wsgi-on-ubuntu-10-04/feed/</wfw:commentRss>
		<slash:comments>41</slash:comments>
		</item>
		<item>
		<title>Setting up Open Source Flex SDK with debugging on Ubuntu</title>
		<link>http://blog.stannard.net.au/2010/09/22/setting-up-open-source-flex-sdk-with-debugging-on-ubuntu/</link>
		<comments>http://blog.stannard.net.au/2010/09/22/setting-up-open-source-flex-sdk-with-debugging-on-ubuntu/#comments</comments>
		<pubDate>Wed, 22 Sep 2010 03:58:20 +0000</pubDate>
		<dc:creator>Kevan Stannard</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[Debug]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[SDK]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://blog.stannard.net.au/?p=182</guid>
		<description><![CDATA[Step by step instructions for setting up Open Source Flex SDK with debugging on Ubuntu. Download the Flex SDK http://opensource.adobe.com/wiki/display/flexsdk/Flex+SDK I downloaded the file flex_sdk_4.1.0.16076.zip Unzip and install Flex Unzip, move to /opt/ and create a link named flex to it unzip flex_sdk_4.1.0.16076.zip -d flex_sdk_4.1.0.16076 sudo mv flex_sdk_4.1.0.16076 /opt/ ln -s /opt/flex_sdk_4.1.0.16076 /opt/flex Optionally you [...]]]></description>
			<content:encoded><![CDATA[<p>Step by step instructions for setting up Open Source Flex SDK with debugging on Ubuntu.</p>
<p><span id="more-182"></span><br />
<strong>Download the Flex SDK</strong></p>
<p><a href="http://opensource.adobe.com/wiki/display/flexsdk/Flex+SDK">http://opensource.adobe.com/wiki/display/flexsdk/Flex+SDK</a></p>
<p>I downloaded the file flex_sdk_4.1.0.16076.zip</p>
<p><strong>Unzip and install Flex</strong> </p>
<p>Unzip, move to /opt/ and create a link named flex to it</p>
<p><code><br />
unzip flex_sdk_4.1.0.16076.zip -d flex_sdk_4.1.0.16076<br />
sudo mv flex_sdk_4.1.0.16076 /opt/<br />
ln -s /opt/flex_sdk_4.1.0.16076 /opt/flex<br />
</code></p>
<p>Optionally you can add the Flex bin directory to your path. There are a few ways to do this but I like to add the path to my environment file</p>
<p><code><br />
sudo nano /etc/environment<br />
</code></p>
<p>Add :/opt/flex/bin to the end of the path. The reload the environment.</p>
<p><code><br />
source /etc/environment<br />
</code></p>
<p><strong>Download the Flash player with debugger</strong></p>
<p><a href="http://www.adobe.com/support/flashplayer/downloads.html">http://www.adobe.com/support/flashplayer/downloads.html</a></p>
<p>I downloaded the player content debugger flashplayer_10_plugin_debug.tar.gz</p>
<p>Unzip this file to get the libflashplayer.so file.</p>
<p>Rename the libflashplayer.so file in your /usr/lib/flashplugin-installer/ directory, the copy the new libflashplayer.so to this location.</p>
<p><strong>Create and compile a Flex app</strong></p>
<p>Create any simple Flex app that uses the trace() function. I used this one from the online Flex 4 docs &#8211; file name hello.mxml:</p>
<p><code><br />
&lt;?xml version="1.0"?&gt;<br />
&lt;s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"<br />
    xmlns:mx="library://ns.adobe.com/flex/mx"<br />
    xmlns:s="library://ns.adobe.com/flex/spark"&gt;</p>
<p>    &lt;fx:Script&gt;<br />
        &lt;![CDATA[<br />
            private function writeToLog():void {<br />
                trace(myText.text);<br />
            }<br />
        ]]&gt;<br />
    &lt;/fx:Script&gt;</p>
<p>    &lt;s:VGroup id="myVGroup"&gt;<br />
        &lt;s:TextInput id="myText"<br />
            text="Hello World!" /&gt;<br />
        &lt;s:Button id="mybutton"<br />
            label="Say Hello"<br />
            click="writeToLog();"/&gt;<br />
    &lt;/s:VGroup&gt;<br />
&lt;/s:Application&gt;<br />
</code> </p>
<p>Compile this with the &#8211;debug option</p>
<p><code><br />
mxmlc --show-actionscript-warnings=true --strict=true --debug=true hello.mxml<br />
</code></p>
<p>This creates a hello.swf file</p>
<p><strong>Running and debugging</strong></p>
<p>Open a terminal window and run the Flex debugger</p>
<p><code><br />
fdb<br />
</code></p>
<p>Within fdb start a debug session with the run command</p>
<p><code><br />
(fdb) run<br />
</code></p>
<p>Open hello.swf in your browser.</p>
<p>You should see a dialog window &#8216;Where is the debugger or host application running?&#8217;</p>
<p>* Choose localhost<br />
* Tick don&#8217;t show this dialog at launch<br />
* Click Connect</p>
<p>Go back to the terminal window and type &#8216;continue&#8217;</p>
<p><code><br />
(fdb) continue<br />
</code></p>
<p>Your should be able to use your flex app and see your debug trace statements in the terminal window.</p>
<p>If you take too long between running the debug process and connecting via the browser then the session will automatically end, so you might need to try again.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.stannard.net.au/2010/09/22/setting-up-open-source-flex-sdk-with-debugging-on-ubuntu/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>HTML Checked, Disabled and Selected attributes</title>
		<link>http://blog.stannard.net.au/2010/07/20/html-checked-disabled-and-selected-attributes/</link>
		<comments>http://blog.stannard.net.au/2010/07/20/html-checked-disabled-and-selected-attributes/#comments</comments>
		<pubDate>Tue, 20 Jul 2010 01:50:20 +0000</pubDate>
		<dc:creator>Kevan Stannard</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Attribute]]></category>
		<category><![CDATA[Boolean]]></category>
		<category><![CDATA[Checked]]></category>
		<category><![CDATA[Disabled]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Selected]]></category>

		<guid isPermaLink="false">http://blog.stannard.net.au/?p=179</guid>
		<description><![CDATA[Checked, Disabled and Selected are common HTML Boolean attributes. So what&#8217;s the correct way to specify these (and any other similar Boolean attributes) in your HTML markup? From the HTML 5 specs we get the following. DO THIS To add the attributes, you can do this: &#60;input type=&#34;checkbox&#34; checked=&#34;checked&#34;&#62; &#60;input disabled=&#34;disabled&#34;&#62; &#60;option selected=&#34;selected&#34;&#62; Or use [...]]]></description>
			<content:encoded><![CDATA[<p>Checked, Disabled and Selected are common HTML Boolean attributes. So what&#8217;s the correct way to specify these (and any other similar Boolean attributes) in your HTML markup?</p>
<p><span id="more-179"></span><br />
From the <a href="http://www.w3.org/TR/html5/common-microsyntaxes.html#boolean-attributes">HTML 5 specs</a> we get the following.</p>
<p><strong>DO THIS</strong></p>
<p>To add the attributes, you can do this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;input type=&quot;checkbox&quot; checked=&quot;checked&quot;&gt;
&lt;input disabled=&quot;disabled&quot;&gt;
&lt;option selected=&quot;selected&quot;&gt;</pre></div></div>

<p>Or use an empt string:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;input type=&quot;checkbox&quot; checked=&quot;&quot;&gt;
&lt;input disabled=&quot;&quot;&gt;
&lt;option selected=&quot;&quot;&gt;</pre></div></div>

<p>/Edit 19 July 2011/ Attributes without a value are also valid:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;input type=&quot;checkbox&quot; checked&gt;
&lt;input disabled&gt;
&lt;option selected&gt;</pre></div></div>

<p>/Edit 19 July 2011/ And the double quotes are optional:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;input type=checkbox checked=checked&gt;
&lt;input disabled=disabled&gt;
&lt;option selected=selected&gt;</pre></div></div>

<p>To not apply the attribute, just leave them off:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;input type=&quot;checkbox&quot;&gt;
&lt;input&gt;
&lt;option&gt;</pre></div></div>

<p><strong>DON&#8217;T DO THIS</strong></p>
<p>/Edit 19 July 2011/</p>
<p><del datetime="2011-07-18T22:16:25+00:00">Leaving the attributes without a value is invalid:</del></p>
<p><strike datetime="2011-07-18T22:16:25+00:00"></p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;input type=&quot;checkbox&quot; checked&gt;
&lt;input disabled&gt;
&lt;option selected&gt;</pre></div></div>

<p></strike></p>
<p>Using true/false values is invalid:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;input type=&quot;checkbox&quot; checked=&quot;true&quot;&gt;
&lt;input disabled=&quot;false&quot;&gt;
&lt;option selected=&quot;true&quot;&gt;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.stannard.net.au/2010/07/20/html-checked-disabled-and-selected-attributes/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
