<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Sebastiano Armeli]]></title>
  <link href="http://www.sebarmeli.com/atom.xml" rel="self"/>
  <link href="http://www.sebarmeli.com/blog/"/>
  <updated>2022-08-15T14:13:26+02:00</updated>
  <id>http://www.sebarmeli.com/blog/</id>
  <author>
    <name><![CDATA[Sebastiano Armeli]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[ePub - MVC applies to JavaScript - released]]></title>
    <link href="http://www.sebarmeli.com/blog/2013/02/21/epub-mvc-applies-to-javascript-released/"/>
    <updated>2013-02-21T17:43:00+01:00</updated>
    <id>http://www.sebarmeli.com/blog/blog/2013/02/21/epub-mvc-applies-to-javascript-released</id>
    <content type="html"><![CDATA[<p><img src="http://www.sebarmeli.com/blog/images/btn-book.png" title="MVC applies to JavaScript" alt="MVC applies to JavaScript" /></p>

<p>Glad to announce that <a href="http://www.developer-press.com/">Developer.press</a> released my first ePub: &#8216;MVC applies to JavaScript&#8217;. The ebook is about integrating the MVC pattern into JavaScript development. It presents a pragmatic approach on how you can develop a client-side application using this pattern. Real code is possibly the best way to explain the advantages of using a design pattern and this book shows a comparison of the code needed to build a sample application adopting and not adopting MVC.</p>

<p>Backbone.js, one of the most popular MVC/MV* frameworks, it is also brought as term of comparison. The sample application mentioned previously is built using Backbone.js to show how the use of a framework can help and speed up your development process.
Client-side applications are becoming larger and larger and having a pattern to use as a guideline can definitely be very advantageous for producing maintainable and scalable applications.</p>

<p>The book is available on <a href="http://www.amazon.com/MVC-Applies-to-JavaScript-ebook/dp/B00B5THNI2/ref=sr_1_1?ie=UTF8&amp;amp;qid=1360060185&amp;amp;sr=8-1&amp;amp;keywords=mvc%20applies%20to%20javascript">Amazon</a> and on <a href="https://itunes.apple.com/us/book/mvc-applies-to-javascript/id596345509?mt=11">Itunes</a> for less than $2.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Get started with Selenium WebDriver]]></title>
    <link href="http://www.sebarmeli.com/blog/2012/04/15/get-started-with-selenium-webdriver/"/>
    <updated>2012-04-15T17:41:00+02:00</updated>
    <id>http://www.sebarmeli.com/blog/blog/2012/04/15/get-started-with-selenium-webdriver</id>
    <content type="html"><![CDATA[<p>Selenium is a well known framework for automating functional testing of web applications. The second version of this tool, named <strong>Selenium WebDriver</strong>, has been released last year merging the best part of Selenium (1) and Web Driver. If you are interested in automated functional testing and you want to know more around Selenium Web Driver, feel free to read  <a href="http://www.ibm.com/developerworks/web/library/wa-selenium2/index.html">this article</a> I wrote for the IBMDeveloperWork on how to get started with it.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[The State of JavaScript in 2012]]></title>
    <link href="http://www.sebarmeli.com/blog/2012/03/22/the-state-of-javascript-in-2012/"/>
    <updated>2012-03-22T17:36:00+01:00</updated>
    <id>http://www.sebarmeli.com/blog/blog/2012/03/22/the-state-of-javascript-in-2012</id>
    <content type="html"><![CDATA[<p>2012 is looking a year where JavaScript development will be more and more critical in web development. I recently wrote an article for the Safari Books online on <a href="http://blog.safaribooksonline.com/2012/01/31/the-state-of-javascript-in-2012/">The State of JavaScript in 2012</a> where I quickly introduce interesting things happening in the JavaScript world. You can also find short tips on <a href="http://blog.safaribooksonline.com/?s=coffeescript">CoffeeScript</a> and on <a href="http://blog.safaribooksonline.com/2012/02/02/node-js-server-side-javascript/">Node.js</a>. Leave a comment there or here if you find them helpful.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[What is there behind a class in CoffeeScript?]]></title>
    <link href="http://www.sebarmeli.com/blog/2012/03/17/what-is-there-behind-a-class-in-coffeescript/"/>
    <updated>2012-03-17T17:29:00+01:00</updated>
    <id>http://www.sebarmeli.com/blog/blog/2012/03/17/what-is-there-behind-a-class-in-coffeescript</id>
    <content type="html"><![CDATA[<p><strong>CoffeeScript</strong> is a little language that compiles into JavaScript. CoffeeScript is gaining lots of popularity due to its simplicity and it is really popular in the Ruby community having lots of similarity with Ruby. CoffeeScript comes with the notion of &#8221;<strong>class</strong>&#8221;. Class?? JavaScript doesn&#8217;t have class, right? True, but JavaScript has the concept of inheritance built-in the language. JavaScript doesn&#8217;t use classes for inheritance, but it uses the &#8221;<em>prototype</em>&#8221; object as you may know. <strong>Prototypal inheritance</strong> can be implemented in JS as the &#8221;<strong>Object.create</strong>&#8221; method (available in ES5) does or as in the <em>protoInherit()</em> in the following snippet:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span><span class="c1">// parent object</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">parentObj</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">prop1</span> <span class="o">:</span> <span class="s2">&quot;value1&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="nx">method1</span> <span class="o">:</span> <span class="kd">function</span><span class="p">(){</span> <span class="k">return</span> <span class="mf">1</span><span class="p">;</span> <span class="p">}</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// prototypal inheritance through the protoInherit function</span>
</span><span class='line'><span class="kd">function</span> <span class="nx">protoInherit</span><span class="p">(</span><span class="nx">parentObj</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="kd">function</span> <span class="nx">Temp</span><span class="p">(){};</span>
</span><span class='line'>  <span class="nx">Temp</span><span class="p">.</span><span class="nx">prototype</span> <span class="o">=</span> <span class="nx">parentObj</span><span class="p">;</span>
</span><span class='line'>  <span class="k">return</span> <span class="ow">new</span> <span class="nx">Temp</span><span class="p">();</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// child object inherits from parent</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">childObj</span> <span class="o">=</span> <span class="nx">protoInherit</span><span class="p">(</span><span class="nx">parentObj</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">childObj</span><span class="p">.</span><span class="nx">prop1</span><span class="p">);</span> <span class="c1">// returns &quot;value1&quot;</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">childObj</span><span class="p">.</span><span class="nx">method1</span><span class="p">());</span> <span class="c1">// return 1</span>
</span></code></pre></td></tr></table></div></figure>


<p>Quite straightforward: the prototype linked to the new object points to the object that we want to inherit from. In this way the new object gets all the properties and methods from the parent  object name &#8221;<em>parentObj</em>&#8221; in the previous snippet.</p>

<p>Now, let&#8217;s go back to CoffeeScript. If you visit <a href="http://coffeescript.org/">http://coffeescript.org/</a> and you scroll down to the section &#8221;<em>Classes, Inheritance, and Super</em>&#8221;, you can see in the right hand how the inheritance is implemented and how a CoffeeScript class is translated into JavaScript. The method that is in charge of expressing inheritance is &#8221;<em>__extends</em>&#8221; and below is the implementation used:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span><span class="nx">__hasProp</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">hasOwnProperty</span><span class="p">,</span>
</span><span class='line'><span class="nx">__extends</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">child</span><span class="p">,</span> <span class="nx">parent</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">key</span> <span class="ow">in</span> <span class="nx">parent</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>      <span class="k">if</span> <span class="p">(</span><span class="nx">__hasProp</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">parent</span><span class="p">,</span> <span class="nx">key</span><span class="p">))</span>
</span><span class='line'>          <span class="nx">child</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="nx">parent</span><span class="p">[</span><span class="nx">key</span><span class="p">];</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="kd">function</span> <span class="nx">ctor</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>      <span class="k">this</span><span class="p">.</span><span class="kr">constructor</span> <span class="o">=</span> <span class="nx">child</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="nx">ctor</span><span class="p">.</span><span class="nx">prototype</span> <span class="o">=</span> <span class="nx">parent</span><span class="p">.</span><span class="nx">prototype</span><span class="p">;</span>
</span><span class='line'>  <span class="nx">child</span><span class="p">.</span><span class="nx">prototype</span> <span class="o">=</span> <span class="ow">new</span> <span class="nx">ctor</span><span class="p">;</span>
</span><span class='line'>  <span class="nx">child</span><span class="p">.</span><span class="nx">__super__</span> <span class="o">=</span> <span class="nx">parent</span><span class="p">.</span><span class="nx">prototype</span><span class="p">;</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">child</span><span class="p">;</span>
</span><span class='line'><span class="p">};</span>
</span></code></pre></td></tr></table></div></figure>


<p>Not exactly the same code as in the previous snippet. Look confusing? Let&#8217;s have a better look. The function implementing the inheritance takes the &#8221;<em>child</em>&#8221; and the &#8221;<em>parent</em>&#8221; function as arguments. First of all, all the properties from the &#8220;parent&#8221; are copied in the &#8220;child&#8221; function. Then, the prototype object of the &#8220;child&#8221; function points to a temporary constructor, which prototype points to the &#8220;parent&#8220;&#8216;s prototype. The &#8221;<em><strong>super</strong></em>&#8221; property is attached to the &#8220;child&#8221; function and it points to the parent&#8217;s prototype, in case you need to refer to the parent function. Finally the &#8220;child&#8221; function is returned.</p>

<p>Basically the <strong>classical inheritance</strong> pattern has been implemented here and it is an alternative to express inheritance in JavaScript. It makes sense in this context as CoffeeScript is trying here to emulate the behaviour of classes, familiar for Ruby, Java or C# developers, and to hide the prototype&#8217;s behaviour, which can be confusing for non-JavaScript developers.</p>

<p>I personally still like using the &#8220;prototype&#8221; object for inheritance, as it seems more a native thing from the language. Anyway, hope to have clarified the meaning of the code behind a &#8220;class&#8221; in CoffeeScript.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Getting started with Backbone.js]]></title>
    <link href="http://www.sebarmeli.com/blog/2012/03/14/getting-started-with-backbone-dot-js/"/>
    <updated>2012-03-14T17:19:00+01:00</updated>
    <id>http://www.sebarmeli.com/blog/blog/2012/03/14/getting-started-with-backbone-dot-js</id>
    <content type="html"><![CDATA[<p><strong>Backbone.js</strong> is a great library for building single page applications and JavaScript MVC-like apps. The author of Backbone is Jeremy Ashkenas, the same guy who wrote CoffeeScript. If you are interested in knowing more around this library I just redirect to an article I wrote recently for <a href="http://www.ibm.com/developerworks/web/library/wa-backbonejs/index.html">IBM DeveloperWorks</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Parsing querystring in a URL through JavaScript]]></title>
    <link href="http://www.sebarmeli.com/blog/2011/05/30/parsing-querystring-in-a-url-through-javascript/"/>
    <updated>2011-05-30T17:14:00+02:00</updated>
    <id>http://www.sebarmeli.com/blog/blog/2011/05/30/parsing-querystring-in-a-url-through-javascript</id>
    <content type="html"><![CDATA[<p>To parse a query string in a URL through JS, you can use a function like the following one:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span><span class="kd">function</span> <span class="nx">getQueryValue</span><span class="p">(</span><span class="nx">param</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">qstring</span> <span class="o">=</span> <span class="nb">window</span><span class="p">.</span><span class="nx">location</span><span class="p">.</span><span class="nx">search</span><span class="p">,</span>
</span><span class='line'>      <span class="nx">qstringArray</span> <span class="o">=</span> <span class="nx">qstring</span> <span class="o">&amp;&amp;</span> <span class="nx">qstring</span><span class="p">.</span><span class="nx">substring</span><span class="p">(</span><span class="mf">1</span><span class="p">).</span><span class="nx">split</span><span class="p">(</span><span class="s2">&quot;&amp;&quot;</span><span class="p">),</span>
</span><span class='line'>      <span class="nx">i</span> <span class="o">=</span> <span class="mf">0</span><span class="p">,</span>
</span><span class='line'>      <span class="nx">len</span> <span class="o">=</span> <span class="nx">qstringArray</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span>
</span><span class='line'>  <span class="k">for</span> <span class="p">(;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">len</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>      <span class="kd">var</span> <span class="nx">token</span> <span class="o">=</span> <span class="nx">qstringArray</span><span class="p">[</span><span class="nx">i</span><span class="p">],</span>
</span><span class='line'>          <span class="nx">eqIndex</span> <span class="o">=</span> <span class="nx">token</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="s2">&quot;=&quot;</span><span class="p">),</span>
</span><span class='line'>          <span class="nx">firstPart</span> <span class="o">=</span> <span class="nx">token</span> <span class="o">&amp;&amp;</span> <span class="nx">token</span><span class="p">.</span><span class="nx">substring</span><span class="p">(</span><span class="mf">0</span><span class="p">,</span> <span class="nx">eqIndex</span><span class="p">);</span>
</span><span class='line'>      <span class="k">if</span> <span class="p">(</span><span class="nx">firstPart</span> <span class="o">===</span> <span class="nx">param</span> <span class="p">)</span> <span class="p">{</span>
</span><span class='line'>          <span class="k">return</span> <span class="nx">token</span><span class="p">.</span><span class="nx">substring</span><span class="p">(</span><span class="nx">eqIndex</span> <span class="o">+</span> <span class="mf">1</span><span class="p">,</span> <span class="nx">token</span><span class="p">.</span><span class="nx">length</span><span class="p">);</span>
</span><span class='line'>      <span class="p">}</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>E.g. Given a URL &#8220;http://domain.com.au?aaa=bbb&#8221; , you can call this function as getQueryValue(&#8220;aaa&#8221;) and youll get &#8220;bbb&#8221;</p>

<p>I uploaded this code in a <a href="https://gist.github.com/713549">Gist</a> on Github (bit modified to be compliant with the module pattern).</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Singleton pattern in Java and JavaScript]]></title>
    <link href="http://www.sebarmeli.com/blog/2011/02/01/singleton-pattern-in-java-and-javascript/"/>
    <updated>2011-02-01T16:18:00+01:00</updated>
    <id>http://www.sebarmeli.com/blog/blog/2011/02/01/singleton-pattern-in-java-and-javascript</id>
    <content type="html"><![CDATA[<p>Singleton pattern is a <a href="http://en.wikipedia.org/wiki/Design_pattern_(computer_science">Design Pattern</a> that uses a single instance of the same class. It&#8217;s used where we need just an object, an instance which we can call through the whole application. In Java, a typical example where this pattern is commonly used is when we use the logging functionality (Log4j, Commons Logging, SLF4J..), where we need a single instance of the Logger object.</p>

<p>In <strong>Java</strong>, we can implement this pattern typically in two ways:</p>

<p>a) The Singleton Class has a public, static and final field that get the instance and  a private constructor</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span></span><span class="kd">public</span> <span class="kd">class</span> <span class="nc">SingletonClass</span> <span class="p">{</span>
</span><span class='line'>  <span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">SingletonClass</span> <span class="n">INSTANCE</span> <span class="o">=</span> <span class="k">new</span> <span class="n">SingletonClass</span><span class="p">();</span>
</span><span class='line'>  <span class="kd">private</span> <span class="nf">SingletonClass</span><span class="p">(){};</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>You can call the object as &#8221;<strong>SingletonClass.INSTANCE</strong>&#8221;.</p>

<p>This first approach is not completely safe though (see AccessibleObject.setAccessible method to call the private constructor).
To avoid that, we can look into the second type of approach:</p>

<p>b) The Singleton Class has a public static method that return the instance (<strong>factory method</strong>), a private constructor and a private field that instantiates the single instance.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span></span><span class="kd">public</span> <span class="kd">class</span> <span class="nc">SingletonClass</span> <span class="p">{</span>
</span><span class='line'>  <span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">SingletonClass</span> <span class="n">INSTANCE</span> <span class="o">=</span> <span class="k">new</span> <span class="n">SingletonClass</span><span class="p">();</span>
</span><span class='line'>  <span class="kd">private</span> <span class="nf">SingletonClass</span><span class="p">(){};</span>
</span><span class='line'>  <span class="kd">public</span> <span class="kd">static</span> <span class="n">SingletonClass</span> <span class="nf">getInstance</span><span class="p">(){</span><span class="k">return</span> <span class="n">INSTANCE</span><span class="p">;}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>You can call the object as &#8221;<strong>SingletonClass.getInstance()</strong>&#8221; and it gives you more flexibility around the API.</p>

<p>From Java 1.5, the best approach according to <em>Joshua Bloch</em>, is using the <em>enum</em> type.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span></span><span class="kd">public</span> <span class="kd">enum</span> <span class="n">SingletonClass</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">INSTANCE</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>You can call the object as &#8221;<strong>SingletonClass.INSTANCE</strong>&#8221;, very similar to the first solution shown. This approach appears to be the best especially when you need to deal with Serialization and Synchronization.</p>

<p>In <strong>JavaScript</strong>, it&#8217;s of course possible to implement this pattern as in Java. That is how the code can look:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span><span class="kd">var</span> <span class="nx">SingletonClass</span> <span class="o">=</span> <span class="p">(</span><span class="kd">function</span><span class="p">(){</span>
</span><span class='line'>  <span class="kd">function</span> <span class="nx">SingletonClass</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>      <span class="c1">//do stuff</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">instance</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">return</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>      <span class="nx">getInstance</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
</span><span class='line'>          <span class="k">if</span> <span class="p">(</span><span class="nx">instance</span> <span class="o">===</span> <span class="kc">undefined</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>              <span class="nx">instance</span> <span class="o">=</span> <span class="ow">new</span> <span class="nx">SingletonClass</span><span class="p">();</span>
</span><span class='line'>          <span class="p">}</span>
</span><span class='line'>          <span class="k">return</span> <span class="nx">instance</span><span class="p">;</span>
</span><span class='line'>      <span class="p">};</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">})();</span>
</span></code></pre></td></tr></table></div></figure>


<p>You can call the object as <strong>SingletonClass.getInstance()</strong>.
The code looks really similar to the way the Singleton used to be implemented in Java, where the main problem is dealing with Threads &amp; Synchronized blocks (having a similar code in Java, it will cause an issue in an environment with multiple threads).</p>

<p> JavaScript fortunately is <strong>single-threaded</strong>, so we don&#8217;t need to worry about this kind of problems.</p>

<p>For more info, you can read the article from the <a href="http://en.wikipedia.org/wiki/Singleton_pattern">wiki</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Reasons why I wrote the plugin Jquery Asynchronous Image Loader (JAIL)]]></title>
    <link href="http://www.sebarmeli.com/blog/2011/01/04/reasons-why-i-wrote-the-plugin-jquery-asynchronous-image-loader-jail/"/>
    <updated>2011-01-04T16:16:00+01:00</updated>
    <id>http://www.sebarmeli.com/blog/blog/2011/01/04/reasons-why-i-wrote-the-plugin-jquery-asynchronous-image-loader-jail</id>
    <content type="html"><![CDATA[<p>The need for a plugin that loads images asynchronously comes from the will of improving the page load time and making the user experience enjoyable/acceptable when there are heavy images in a page. When there are images on a page, the browser makes HTTP requests to download them and in case there are many of them and they have large size, it can take a while to load the rest of the page. The user will see the content below the images with some delay and the user experience won&#8217;t be that pleasant.</p>

<p>In mobile sites, I find this problem quite critical, as the user will especially feel the effects of this delay.</p>

<p>Deferring the loading of the images can be the solution and that&#8217;s what my plugin is about.</p>

<p>Before to have started writing it though, I researched on the net if there was something already that could solve my issue. Well, of course someone else has already thought about this problem and has already implemented something good. That&#8217;s what I found:</p>

<ul>
<li><p><a href="http://developer.yahoo.com/yui/3/imageloader/">YUI ImageLoader Utility</a>: utility from YUI3, that delays the loading of the images and lets you determine triggers and time limits to initiate image loading. You can find more info on their web page and I believe it&#8217;s a great utility (like all YUI3 library great code as well).
This utility requires some HTML change, such as leaving out the &#8220;src&#8221; attribute from the IMG tag.
There are few things about this solution that weren&#8217;t good for my purpose though (in order of importance):
a) Disabling JavaScript, the images are not loaded. That was surprising! They didn&#8217;t care about <a href="http://www.alistapart.com/articles/understandingprogressiveenhancement/">progressive enhancement</a>.
b) I can customize a trigger to load the images, but I can&#8217;t customize a callback function after the images are loaded.
c) I&#8217;ve already got jQuery in my page, I&#8217;d like not use another JS library for page performance reasons. (other JS code to download)</p></li>
<li><p><a href="http://www.appelsiini.net/projects/lazyload">Lazy Loading Plugin for jQuery</a>: plugin for jQuery written by Mika Tuupola.
This solution was respecting the progressive enhancement and was solving above a) and c)! It seems the way to go.</p></li>
</ul>


<p>Just a couple of things go bad:
a) In the homepage it&#8217;s clearly written that the plugin is currently not usable. :(
b) Refreshing the <a href="http://www.appelsiini.net/projects/lazyload/enabled.html">demo page</a> with the Net tab of Firebug opened, the images &#8220;below&#8221; the fold are loaded.
So something must go wrong at least in my browser FF 3.6 on Mac.</p>

<p>I decided to take notes from this previous solutions and in order to achieve what I needed for the site I was working on, I wrote my own plugin for jQuery, called
 JAIL aka Jquery Asynchronous Image Loader. The whole solution respects the concept of progressive enhancement, you can customize a callback function and it requires jQuery of course.</p>

<p> You can find the source on <a href="https://github.com/sebarmeli/JAIL">Github</a>.</p>

<p> It&#8217;s lightweight (compressed version is 4Kb), it&#8217;s licensed under MIT and GPL v3 and it&#8217;s been tested with jQuery 1.3.2+ on FF2+, IE6+, Chrome5+, Opera 10+, Safari3+.</p>

<p> Just some quick notes, this plugin is not for all the cases and it&#8217;s not supposed to be applied to all the images. It required some HTML changes so you need to be able
 to modify the HTML, and I&#8217;d suggest to use it for &#8220;heavy&#8221; images and for mobile sites.</p>

<p>UPDATE (19/12/2011) : Version 0.7 released. Added support for &#8216;offset&#8217; configuration</p>

<p>UPDATE (03/04/2011) : Version 0.8 released - jail() function, fixed critical issue on v0.7, resizing function, scrolling fixes</p>

<p>UPDATE (03/08/2011) : Version 0.9.5 released - Issue 14 fixed regarding applying event on container different from &#8216;window&#8217;, filenames changed.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Enumeration in JavaScript]]></title>
    <link href="http://www.sebarmeli.com/blog/2010/12/07/enumeration-in-javascript/"/>
    <updated>2010-12-07T16:09:00+01:00</updated>
    <id>http://www.sebarmeli.com/blog/blog/2010/12/07/enumeration-in-javascript</id>
    <content type="html"><![CDATA[<p>In the previous post, I mentioned that I would have talked about the &#8221;<strong>FOR-IN loop</strong>&#8221; in this post, and that&#8217;s what I&#8217;m about to do. So reading the title, you can gather how &#8220;for-in&#8221; loop is used for Enumeration in JavaScript.</p>

<p>What does it mean?</p>

<p>It means that if you want to enumerate the properties inside a JavaScript object, you need to use this type of loop. This is an example:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span><span class="c1">// Object literal</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">myObject</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">field</span> <span class="o">:</span> <span class="s2">&quot;myfield&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="nx">mymethod</span> <span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
</span><span class='line'>      <span class="k">return</span> <span class="s2">&quot;hello&quot;</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>




<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span><span class="c1">// for-in loop</span>
</span><span class='line'><span class="k">for</span> <span class="p">(</span><span class="nx">i</span> <span class="ow">in</span> <span class="nx">myObject</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">i</span><span class="p">)</span> <span class="c1">//&quot;field&quot;, &quot;mymethod&quot;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The console will print the properties of the object, so the field and the method.</p>

<p>Coming back to my previous post, one of the issue with this loop is that is taking under consideration even all the objects involved in the prototypal chain, so that if we augment the Object with a new property the for-in loop will consider even this last one.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span><span class="nb">Object</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">newMethod</span> <span class="o">=</span> <span class="s2">&quot;string&quot;</span><span class="p">;</span>
</span><span class='line'><span class="c1">// Object literal</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">myObject</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">field</span> <span class="o">:</span> <span class="s2">&quot;myfield&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="nx">mymethod</span> <span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
</span><span class='line'>      <span class="k">return</span> <span class="s2">&quot;hello&quot;</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// for-in loop</span>
</span><span class='line'><span class="k">for</span> <span class="p">(</span><span class="nx">i</span> <span class="ow">in</span> <span class="nx">myObject</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">i</span><span class="p">)</span> <span class="c1">//&quot;field&quot;, &quot;mymethod&quot;, &quot;newMethod&quot;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>To avoid this inconvenient, we can use the method <strong>hasOwnProperty</strong>()  which returns true if the property belongs to the object itself (not considering the prototype), so that:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span><span class="nb">Object</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">newMethod</span> <span class="o">=</span> <span class="s2">&quot;string&quot;</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Object literal</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">myObject</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">field</span> <span class="o">:</span> <span class="s2">&quot;myfield&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="nx">mymethod</span> <span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
</span><span class='line'>      <span class="k">return</span> <span class="s2">&quot;hello&quot;</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// for-in loop</span>
</span><span class='line'><span class="k">for</span> <span class="p">(</span><span class="nx">i</span> <span class="ow">in</span> <span class="nx">myObject</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">if</span><span class="p">(</span><span class="nx">myObject</span><span class="p">.</span><span class="nx">hasOwnProperty</span><span class="p">(</span><span class="nx">i</span><span class="p">)){</span>
</span><span class='line'>      <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">i</span><span class="p">)</span> <span class="c1">//&quot;field&quot;, &quot;mymethod&quot;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>In this case we get what we possibly were expecting!</p>

<p>In <em>ECMAScript 5</em>, few enhancements have been made and now you can define a property (<strong>Object.defineProperty</strong> method) and you can use the method <strong>Object.keys(obj)</strong> to return an array of the properties belonging to that object without the need of using the &#8220;dirty&#8221; hasOwnProperty(). That&#8217;s how you could rewrite the previous code in ECMAScript 5:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span><span class="s2">&quot;use strict&quot;</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">myObject</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">field</span> <span class="o">:</span> <span class="s2">&quot;myfield&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="nx">mymethod</span> <span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
</span><span class='line'>      <span class="k">return</span> <span class="s2">&quot;hello&quot;</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'><span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">myObject</span><span class="p">);</span> <span class="c1">//[&quot;field&quot;, &quot;mymethod&quot;]</span>
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Best way to loop through an Array in JavaScript]]></title>
    <link href="http://www.sebarmeli.com/blog/2010/12/06/best-way-to-loop-through-an-array-in-javascript/"/>
    <updated>2010-12-06T15:56:00+01:00</updated>
    <id>http://www.sebarmeli.com/blog/blog/2010/12/06/best-way-to-loop-through-an-array-in-javascript</id>
    <content type="html"><![CDATA[<p>Last week, a colleague of mine asked me &#8220;Why are you using a &#8216;standard for loop&#8217; rather than the <em>faster</em> &#8216;for-in loop&#8217; to loop through an Array in  JavaScript?&#8221; and for this reason I&#8217;m going to write a short explanation about that.</p>

<p>First of all, when I talk about the standard &#8221;<strong>FOR loop</strong>&#8221;, I mean something like</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span><span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span><span class="o">=</span><span class="mf">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">myArray</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">){}</span>
</span></code></pre></td></tr></table></div></figure>


<p>instead the &#8221;<strong>FOR-IN loop</strong>&#8221; is something like:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span><span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="ow">in</span> <span class="nx">myArray</span><span class="p">){}</span>
</span></code></pre></td></tr></table></div></figure>


<p><strong>for-in loop</strong> is faster&#8230;just to type, for a lazy developer :) and it&#8217;s a common mistake especially for developers coming from Java programming (I&#8217;m a Java developer as well, that&#8217;s why I know that :P), trying to port Java into JavaScript.</p>

<p>The two main problems with for-in loop are :
1) It enumerates through all the properties even the ones associated to the Object (that can be augmented previously)
2) The order of the elements is not guaranteed.</p>

<p>You may not interested in the order of the elements(even if often you are), but you need to deal with the first issue and that&#8217;s what can happen:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span><span class="kd">var</span> <span class="nx">myArray</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;aa&quot;</span><span class="p">,</span> <span class="s2">&quot;bb&quot;</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Object() augmented</span>
</span><span class='line'><span class="nb">Object</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">newMethod</span> <span class="o">=</span> <span class="s2">&quot;cc&quot;</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// for-in loop</span>
</span><span class='line'><span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="ow">in</span> <span class="nx">myArray</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">myArray</span><span class="p">[</span><span class="nx">i</span><span class="p">]);</span> <span class="c1">//&quot;aa&quot;, bb&quot;, &quot;cc&quot;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The console will print not only the two elements from the array, but also the value of the new property in the Object. Remember that array are objects in JavaScript, at the root of the prototypal chain there is always Object and you never know if Object has augmented previously by a library or some script.</p>

<p>The correct way to execute that loop is using the <strong>for loop:</strong></p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span><span class="kd">var</span> <span class="nx">myArray</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;aa&quot;</span><span class="p">,</span> <span class="s2">&quot;bb&quot;</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Object() augmented</span>
</span><span class='line'>
</span><span class='line'><span class="nb">Object</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">newMethod</span> <span class="o">=</span> <span class="s2">&quot;cc&quot;</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="c1">//for loop</span>
</span><span class='line'><span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span><span class="o">=</span><span class="mf">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">myArray</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">myArray</span><span class="p">[</span><span class="nx">i</span><span class="p">]);</span> <span class="c1">//&quot;aa&quot;, &quot;bb&quot;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Actually, we can make a <em>micro-optimization</em> to this, &#8220;caching&#8221; the length of the array and avoiding every time to calculate the length of the array.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span><span class="kd">var</span> <span class="nx">myArray</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;aa&quot;</span><span class="p">,</span> <span class="s2">&quot;bb&quot;</span><span class="p">];</span>
</span><span class='line'><span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span><span class="o">=</span><span class="mf">0</span><span class="p">,</span>  <span class="nx">tot</span><span class="o">=</span><span class="nx">myArray</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">tot</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">myArray</span><span class="p">[</span><span class="nx">i</span><span class="p">]);</span> <span class="c1">//&quot;aa&quot;, &quot;bb&quot;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Notice that there is a comma between &#8220;var i=0&#8221; and &#8220;tot=myArray.length&#8221; as the &#8220;for loop&#8221; accepts three expressions.</p>

<p>If you find this last way a bit weird, you could do something like:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span><span class="kd">var</span> <span class="nx">myArray</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;aa&quot;</span><span class="p">,</span> <span class="s2">&quot;bb&quot;</span><span class="p">],</span>
</span><span class='line'>  <span class="nx">i</span><span class="o">=</span><span class="mf">0</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">for</span> <span class="p">(</span><span class="nx">tot</span><span class="o">=</span><span class="nx">myArray</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&amp;</span><span class="nx">lt</span><span class="p">;</span> <span class="nx">tot</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">myArray</span><span class="p">[</span><span class="nx">i</span><span class="p">]);</span> <span class="c1">//&quot;aa&quot;, &quot;bb&quot;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>So it&#8217;s strongly recommended to use the <strong>FOR LOOP</strong> to loop through an Array and remember that Java and JavaScript are different.</p>

<p>In the next post, I may go through where the &#8220;for-in&#8221; loop is used.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Understanding WebStorage]]></title>
    <link href="http://www.sebarmeli.com/blog/2010/11/22/understanding-webstorage/"/>
    <updated>2010-11-22T16:21:00+01:00</updated>
    <id>http://www.sebarmeli.com/blog/blog/2010/11/22/understanding-webstorage</id>
    <content type="html"><![CDATA[<p><strong>WebStorage</strong> is one the <strong>HTML5</strong> features and it makes the browser capable to store information. Well, we have always been using <strong>Cookies</strong>, but they have few limitations and let&#8217;s see how WebStorage feature can fix these limitations.</p>

<p>Case 1: A user has two different windows opened and he wants to make two transactions on the same site. Using cookies, you could have &#8220;leaks&#8221; between windows and there is no clear distinction between the windows. WebStorage includes <strong>sessionStorage</strong> object and each window will have a different copy of this object, making each window being &#8220;independent&#8221;. As you can tell from the name this object is limited to a session, so as soon as you close your browser window, it will be erased.</p>

<p>Case 2: The site needs to keep information in common across multiple windows and even after the session is closed. Cookies are relative to each request so they can&#8217;t handle this case properly. WebStorage includes <strong>localStorage</strong> object and it&#8217;s relative to a specific domain and it lasts even after a session is finished.</p>

<p>Other thing, a cookie can have maximum size of 4Kb, Storage object can have size of 5Mb (quite standardized value in all browsers).</p>

<p>Reading the <a href="http://dev.w3.org/html5/webstorage/">W3C specs</a>, those are the methods for the Storage objects:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span><span class="kr">interface</span> <span class="nx">Storage</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">readonly</span> <span class="nx">attribute</span> <span class="nx">unsigned</span> <span class="kr">long</span> <span class="nx">length</span><span class="p">;</span>
</span><span class='line'>  <span class="nx">getter</span> <span class="nx">DOMString</span> <span class="nx">key</span><span class="p">(</span><span class="ow">in</span> <span class="nx">unsigned</span> <span class="kr">long</span> <span class="nx">index</span><span class="p">);</span>
</span><span class='line'>  <span class="nx">getter</span> <span class="nx">any</span> <span class="nx">getItem</span><span class="p">(</span><span class="ow">in</span> <span class="nx">DOMString</span> <span class="nx">key</span><span class="p">);</span>
</span><span class='line'>  <span class="nx">setter</span> <span class="nx">creator</span> <span class="ow">void</span> <span class="nx">setItem</span><span class="p">(</span><span class="ow">in</span> <span class="nx">DOMString</span> <span class="nx">key</span><span class="p">,</span> <span class="ow">in</span> <span class="nx">any</span> <span class="nx">value</span><span class="p">);</span>
</span><span class='line'>  <span class="nx">deleter</span> <span class="ow">void</span> <span class="nx">removeItem</span><span class="p">(</span><span class="ow">in</span> <span class="nx">DOMString</span> <span class="nx">key</span><span class="p">);</span>
</span><span class='line'>  <span class="ow">void</span> <span class="nx">clear</span><span class="p">();</span>
</span><span class='line'><span class="p">};</span>
</span></code></pre></td></tr></table></div></figure>


<p>Let&#8217;s make some examples to understand better this methods:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span><span class="nx">localStorage</span><span class="p">.</span><span class="nx">setItem</span><span class="p">(</span><span class="s2">&quot;firstKey&quot;</span><span class="p">,</span> <span class="s2">&quot;firstValue&quot;</span><span class="p">);</span>
</span><span class='line'><span class="nb">window</span><span class="p">.</span><span class="nx">localStorage</span><span class="p">.</span><span class="nx">getItem</span><span class="p">(</span><span class="s2">&quot;firstKey&quot;</span><span class="p">);</span>     <span class="c1">//firstValue</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is really a basic use and show us how to use &#8221;<strong>setItem</strong>&#8221; and &#8221;<strong>getItem</strong>&#8221; methods. You can notice how you can use either &#8220;localStorage&#8221; or &#8220;window.localStorage&#8221;, this is because localStorage is attached to the <em>window</em> object. Also, the key/value are two strings, or better you can even set an object as value but you&#8217;d better &#8221;<strong>stringify</strong>&#8221; it (transforming it in a string using the stringify JSON method). Let&#8217;s see an example first trying to push an object in a Storage object without &#8220;stringify&#8221;:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span>   <span class="nx">localStorage</span><span class="p">.</span><span class="nx">setItem</span><span class="p">(</span><span class="s2">&quot;secondKey&quot;</span><span class="p">,</span> <span class="p">{</span><span class="nx">id</span> <span class="o">:</span> <span class="mf">1</span><span class="p">});</span>
</span><span class='line'>   <span class="nx">localStorage</span><span class="p">.</span><span class="nx">getItem</span><span class="p">(</span><span class="s2">&quot;secondKey&quot;</span><span class="p">);</span> <span class="c1">// &quot;[object Object]&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>&#8220;[object Object]&#8221; is the String we get but we can&#8217;t get the real object. That&#8217;s how we can fix it:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span>   <span class="nx">localStorage</span><span class="p">.</span><span class="nx">setItem</span><span class="p">(</span><span class="s2">&quot;secondKey&quot;</span><span class="p">,</span> <span class="nb">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">({</span><span class="nx">id</span> <span class="o">:</span> <span class="mf">1</span><span class="p">}));</span>
</span><span class='line'>   <span class="kd">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="nb">JSON</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="nx">localStorage</span><span class="p">.</span><span class="nx">getItem</span><span class="p">(</span><span class="s2">&quot;secondKey&quot;</span><span class="p">));</span> <span class="c1">// Object {id=1}</span>
</span><span class='line'>   <span class="nx">a</span><span class="p">.</span><span class="nx">id</span><span class="p">;</span> <span class="c1">// 1</span>
</span></code></pre></td></tr></table></div></figure>


<p>We used JSON <em>stringify</em> method and JSON <em>parse</em> method, that are useful to transform a JSON object in a string and viceversa. JSON object is now available in HTML5, but it&#8217;s not fully supported in all browsers, so you may want to download <a href="http://github.com/douglascrockford/JSON-js">json.js</a> written by Douglas Crockford, Yahoo JS Architect and in my opinion the maximum expert on JavaScript.</p>

<p>Let&#8217;s explore other methods available:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span><span class="nx">localStorage</span><span class="p">.</span><span class="nx">removeItem</span><span class="p">(</span><span class="s2">&quot;secondKey&quot;</span><span class="p">)</span>
</span><span class='line'><span class="nx">localStorage</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="c1">// 1</span>
</span><span class='line'><span class="nx">localStorage</span><span class="p">.</span><span class="nx">key</span><span class="p">(</span><span class="mf">0</span><span class="p">);</span> <span class="c1">//&quot;firstKey</span>
</span><span class='line'><span class="nx">localStorage</span><span class="p">.</span><span class="nx">clear</span><span class="p">();</span>
</span><span class='line'><span class="nx">localStorage</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="c1">//0</span>
</span></code></pre></td></tr></table></div></figure>


<p>they are quite straightforward methods: &#8221;<strong>removeItem</strong>(index)&#8221; will remove the key/value, &#8221;<strong>length</strong>&#8221; will return the number of key/value, &#8221;<strong>key</strong>(index)&#8221; will return the key for that index (0-based), &#8221;<strong>clear</strong>()&#8221; will remove all key/value.</p>

<p>Notice that I&#8217;ve been using localStorage in those example but I could use sessionStorage as well, as they share the same methods, coming from the same Interface.</p>

<p>You may meet another object called &#8220;globalStorage&#8221;, but remember that it&#8217;s been removed from the specs from Security reasons. It was basically holding different &#8220;localStorage&#8221; objects depending on the domain. So now &#8220;localStorage&#8221; for www.domain.com is different from &#8220;localStorage&#8221; for &#8220;a.domain.com&#8221; or &#8220;domain.com&#8221;.</p>

<p>Other feature is the &#8221;<strong>storage</strong>&#8221; event, which is triggered when the storage has change (using &#8220;setItem&#8221;, &#8220;clear&#8221;..). It has few attributes:</p>

<ul>
<li>key : key being changed</li>
<li>oldValue : old value of the key before being changed</li>
<li>newValue : new value of the key after being changed</li>
<li>url : url of the document where the storage has been changed</li>
<li>storageArea : storage object being changed (local or session)</li>
</ul>


<p>Last thing, it&#8217;s supported in all modern browsers..and IE8! Suprising!</p>

<p>You wanna know more?
Have a read <a href="http://diveintohtml5.org/storage.html">here</a> (great writing from Mark Pilgrim) or have a look at the <a href="http://dev.w3.org/html5/webstorage/">W3C Specs</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Understanding Array.prototype.slice.apply(arguments) in JavaScript]]></title>
    <link href="http://www.sebarmeli.com/blog/2010/11/12/understanding-array-dot-prototype-dot-slice-dot-apply-arguments-in-javascript/"/>
    <updated>2010-11-12T15:30:00+01:00</updated>
    <id>http://www.sebarmeli.com/blog/blog/2010/11/12/understanding-array-dot-prototype-dot-slice-dot-apply-arguments-in-javascript</id>
    <content type="html"><![CDATA[<p>If you are a JavaScript developer soon or later you&#8217;ll bump into this guy: <em>Array.prototype.slice.apply(arguments)</em> and you&#8217;ll ask yourself..what the hell is that??</p>

<p>Well, it&#8217;s not that hard to understand actually, it&#8217;s just ugly. Anyway, &#8221;<em>Array</em>&#8221; is the JS class Array, with &#8221;<em>Array.prototype</em>&#8221; you get its prototype. I assume you know about the prototype, the key concept in JavaScript.</p>

<p>&#8220;<em>slice</em>&#8221; is a method in JavaScript that &#8220;selects a part of an array, and returns the new array.&#8221; (<a href="http://www.w3schools.com/jsref/jsref_slice_array.asp">W3CSchool</a>). It can have two arguments : start_index(required), end_index.
So given:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span><span class="kd">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;a&quot;</span><span class="p">,</span> <span class="s2">&quot;b&quot;</span><span class="p">,</span> <span class="s2">&quot;c&quot;</span><span class="p">];</span>
</span><span class='line'><span class="nx">a</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mf">1</span><span class="p">,</span><span class="mf">2</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>It return [&#8220;b&#8221;], so an array containing the element between index &#8216;1&#8217; and index &#8216;2&#8217; in the Array &#8220;a&#8221;.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span><span class="kd">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;a&quot;</span><span class="p">,</span> <span class="s2">&quot;b&quot;</span><span class="p">,</span> <span class="s2">&quot;c&quot;</span><span class="p">];</span>
</span><span class='line'><span class="nx">a</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mf">1</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>It return [&#8220;b&#8221;, &#8220;c&#8221;], so an array containing the elements between index &#8216;1&#8217; and last index in the a, which is an Array.</p>

<p>So &#8220;a&#8221; is an Array of course, what about &#8220;arguments&#8221; variable?</p>

<p>Ok, arguments, you know, it&#8217;s the implicit JS variable created when you invoke a function, containing the arguments of a function. You&#8217;re expecting this variable to be an Array, right?
Well, it&#8217;s not, it&#8217;s similar, but it&#8217;s still an object, so:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span><span class="kd">function</span> <span class="nx">f</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">arguments</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>given this function f, launching</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span><span class="nx">f</span><span class="p">(</span><span class="s2">&quot;1&quot;</span><span class="p">,</span> <span class="s2">&quot;2&quot;</span><span class="p">)</span> <span class="ow">instanceof</span> <span class="nb">Array</span>
</span></code></pre></td></tr></table></div></figure>


<p>you&#8217;ll get FALSE! That means we can&#8217;t apply a bunch of stuff we normally do with an Array, such as push, pop, slice..but I need those methods, so what can I do?</p>

<p>There you go <strong>Array.prototype.slice.apply(arguments)</strong> converts arguments into an ARRAY.</p>

<p>Here we use one of the methods to call a function in JavaScript, the <strong>APPLY</strong> method, you can learn more about how to call a function in JS <a href="http://devlicious.com/blogs/sergio_pereira/archive/2009/02/09/javascript-5-ways-to-call-a-function.aspx">here</a>.
So we apply the slice function to the first argument of the apply function(in this case &#8220;arguments&#8221;) and we know that the slice() method returns always an Array. We got our Array!</p>

<p>So now</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span></span><span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">slice</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">f</span><span class="p">(</span><span class="s2">&quot;1&quot;</span><span class="p">,</span> <span class="s2">&quot;2&quot;</span><span class="p">))</span> <span class="ow">instanceof</span> <span class="nb">Array</span>
</span></code></pre></td></tr></table></div></figure>


<p>it&#8217;ll return TRUE!</p>

<p>PS: In <strong>ECMAScript5</strong>, we won&#8217;t need to use &#8220;Array.prototype.slice.apply(arguments)&#8221; anymore, but we can easily use <strong>arguments.slice()</strong>!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[How to redirect your site to a mobile version through JavaScript]]></title>
    <link href="http://www.sebarmeli.com/blog/2010/11/02/how-to-redirect-your-site-to-a-mobile-version-through-javascript/"/>
    <updated>2010-11-02T23:34:00+01:00</updated>
    <id>http://www.sebarmeli.com/blog/blog/2010/11/02/how-to-redirect-your-site-to-a-mobile-version-through-javascript</id>
    <content type="html"><![CDATA[<p><strong>SCENARIO</strong></p>

<p>The user needs to be redirected to the mobile version of the site (home page) if it&#8217;s trying to access the site from a mobile device.</p>

<p><strong>SOLUTION</strong></p>

<p><em>UPDATE 25/07/2011</em> : Version 0.9.5 released with support for &#8220;tablet_url&#8221;, &#8220;keep_path&#8221; and &#8220;keep_query&#8221; properties. Ipad and other tablet devices have been excluded from the list of mobile devices by default. You can use &#8220;tablet_redirection&#8221; and &#8220;tablet_url&#8221; parameters for tablets.</p>

<p>To solve this problem, the best approach is implementing something server-side, and I find a good approach using the <strong>WURFL</strong> file to check the capabilities and features of mobile devices. Read <a href="http://wurfl.sourceforge.net/">here</a> to know more about WURFL.</p>

<p>Sometimes, a server-side solution can become difficult to implement especially if we have a CDN or reverse proxy (sitting in front of our Web Server) caching our pages.</p>

<p>Here JavaScript comes to the rescue and I wrote a script that makes the redirection happen called &#8221;<strong>redirection_mobile.js</strong>&#8221;.</p>

<p>You can find the source <a href="http://github.com/sebarmeli/JS-Redirection-Mobile-Site">here</a> on Github.</p>

<p>The first thing to keep in mind is that the function implemented checks the User-Agent string from the Navigator object and from there it decides if the redirection needs to happen.</p>

<p>In some cases the user wants to access to the Desktop version of the site from a mobile device (sometimes the desktop version has more functionality). The script handles this situation as well, it checks if the previous page hit was one from the mobile site (we can suppose the user clicked on a link such &#8220;Go to full site&#8221;) or if there is a specific parameter in the querystring of the URL. In those cases the redirection won&#8217;t occur. To keep the user in the desktop version for the whole session, sessionStorage object has been used, specifically an item will be stored to distinguish if we&#8217;re browsing through the desktop site.
There is a fallback for old browsers that don&#8217;t support sessionStorage, and a cookie will be used. The cookie that makes the access to the desktop version from a mobile device possible will expiry in one hour or you configure the expiry time.</p>

<p>iPhone, iPad, iPod, Android phones support completely sessionStorage, there are still some versions of Blackberry that using IE don&#8217;t and so we still need the &#8220;cookie&#8221; fallback.</p>

<p>The function accepts an argument which is a configuration object with few properties:</p>

<ul>
<li><p><strong>mobile_prefix</strong> : prefix appended to the hostname, such as &#8220;m&#8221; to redirect to &#8220;m.domain.com&#8221;. &#8220;m&#8221; is the default value if the property is not specified.</p></li>
<li><p><strong>mobile_url</strong> : mobile url to use for the redirection (without the protocol), such as &#8220;whatever.com&#8221;/example to redirect to &#8220;whatever.com/example&#8221;. If &#8220;mobile_prefix&#8221; is existing as well, &#8220;mobile_prefix&#8221; will be ignored. Empty string is the default value.</p></li>
<li><strong>mobile_scheme</strong> : url scheme (http/https) of the mobile site domain, such as &#8220;https&#8221; to redirect to &#8220;https://m.domain.com&#8221;. The protocol of the current page is the default value.</li>
<li><strong>noredirection_param</strong> - up to version 0.6 param was used: parameter to pass in the querystring of the URL to avoid the redirection (the value must be equal to &#8220;true&#8221;). Default value is &#8220;noredirection&#8221;. Eg: http://domain.com?noredirection=true. It&#8217;s also the name of the item in the localStorage (or cookie name) used to avoid mobile redirection. Prior version 0.9.5 this parameter was called &#8220;redirection_paramName&#8221;, but I renamed it to make the meaning clearer.</li>
<li><strong>cookie_hours</strong> : number of hours the cookie needs to exist after redirection to desktop site. &#8220;1&#8221; is the default value.</li>
<li><strong>tablet_redirection</strong> : boolean value that enables/disables(default) the redirection for tablet such as iPad, Samsung Galaxy Tab, Kindle or Motorola Xoom. - Default:false. The value needs to be a string (so wrapped in double or single quotes). If &#8216;tablet_url&#8217; parameter not specified, the user will be redirected to the same URL as for mobile devices.</li>
<li><strong>tablet_url</strong> : url to use for the redirection in case the user is using a tablet to access the site. Default value is &#8220;&#8221;</li>
<li><strong>keep_path</strong> : boolean to determine if the destination url needs to keep the path from the original url. Default value is &#8216;false&#8217;</li>
<li><strong>keep_query</strong> : boolean to determine if the destination url needs to keep the querystring from the original url. Default value is &#8216;false&#8217;</li>
<li><strong>beforeredirection_callback</strong> : if specified, callback launched before the redirection happens. If a falsy value is returned from the callback the redirection doesn&#8217;t happen.</li>
</ul>


<p>To use &#8221;<strong>redirection_mobile</strong>&#8221; function, you need to load your script in the HTML of the &#8220;desktop&#8221; pages and call it as SA.redirection_mobile(config). See the code below:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>&lt;!doctype html&gt;
</span><span class='line'>&lt;html&gt;
</span><span class='line'>    &lt;head&gt;
</span><span class='line'>        &lt;title&gt;&lt;/title&gt;
</span><span class='line'>        &lt;script type="text/javascript" src="http://www.sebarmeli.com/blog/js/redirection_mobile.min.js"/&gt;
</span><span class='line'>        &lt;script type="text/javascript"&gt;
</span><span class='line'>            SA.redirection_mobile ({noredirection_param:"noredirection", mobile_prefix : "mobile", cookie_hours : "2" });
</span><span class='line'>        &lt;/script&gt;</span></code></pre></td></tr></table></div></figure>


<p>For instance, in this case, accessing from a mobile device to <strong>http://www.domain.com</strong>, you&#8217;ll be redirected to &#8221;<strong>http://mobile.domain.com</strong>&#8221;.</p>

<p>Considering the previous code, from version 0.6, if you hit a page such as &#8221;<strong>http://domain.com/?noredirection=true</strong>&#8221; the redirection won&#8217;t happen. For all the browser session, if sessionStorage is supported by the browser, the redirection won&#8217;t occur. If sessionStorage (HTML5) is not supported, a cookie &#8220;noredirection=true&#8221; will be stored for 2 hours and it will block the redirection to the mobile site.</p>

<p>If sessionStorage (HTML5) is not supported, a cookie named &#8220;noredirection&#8221; will be stored for 2 hours and it will block the redirection to the mobile site.</p>

<p>The script from version 0.5 allows you to redirect the user to whatever url. Thus if you need to redirect the user to &#8221;<strong>https://domain2.com/mobile</strong>&#8221; now you can invoke the function like this:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>&lt;script type="text/javascript"&gt;
</span><span class='line'>    SA.redirection_mobile ({mobile_scheme:"https", mobile_url : "domain2.com/mobile"});
</span><span class='line'>&lt;/script&gt;</span></code></pre></td></tr></table></div></figure>


<p>Alternatively you can use &#8220;redirection_mobile_self.js&#8221;, that is it&#8217;s an anonyimous self-executing function and it uses it uses the default values for the different properties:</p>

<ul>
<li>&#8220;mobile_prefix&#8221; : &#8220;m&#8221;</li>
<li>&#8220;redirection_paramName&#8221; : &#8220;mobile_redirect&#8221;</li>
<li>&#8220;cookie_hours&#8221; : 1</li>
<li>&#8220;mobile_url&#8221; : &#8220;&#8221;</li>
<li>&#8220;mobile_scheme&#8221; : protocol of the current page</li>
<li>&#8220;tablet_redirection&#8221; : false</li>
<li>&#8220;beforeredirection_callback&#8221; : n/a</li>
</ul>


<p>It doesn&#8217;t need any configuration or any invocation, so you just need to drop it on your webserver and call the script from the HTML of the &#8220;desktop&#8221; pages . See code below:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>&lt;!doctype html&gt;
</span><span class='line'>&lt;html&gt;
</span><span class='line'>    &lt;head&gt;
</span><span class='line'>        &lt;title&gt;&lt;/title&gt;
</span><span class='line'>        &lt;script type="text/javascript" src="http://www.sebarmeli.com/blog/js/redirection_mobile_self.min.js"/&gt;</span></code></pre></td></tr></table></div></figure>


<p>in this case, accessing from a mobile device to <strong>http://www.domain.com</strong>, you&#8217;ll be redirected to &#8221;<strong>http://m.domain.com</strong>&#8221;.</p>

<p>To redirect to a desktop/standard version of the site from a mobile device, you may need to embed a link in your mobile pages such as</p>

<p><a href="http://www.domain.com">Go to main site</a></p>

<p>and the script included in the desktop page will do the rest.</p>

<p>I also created &#8220;redirection_mobile_testable.js&#8221; that is just a copy from &#8220;redirection_mobile.js&#8221;, but it&#8217;s using few arguments such as &#8220;document&#8221;, &#8220;window&#8221;, &#8220;navigator&#8221; for testing purpose. Test cases have been written, using QUnit, to test this script and they mock &#8220;document&#8221;, &#8220;window&#8221; and &#8220;navigator&#8221; in a rudimentary way.</p>

<p>The scripts have their minified versions (used <strong>YUI</strong> compressor).</p>

<p>If you want to test the script on different devises within your desktop browser, you can use a plugin for Firefox called <strong>UserAgentSwitcher</strong>, that you can download <a href="https://addons.mozilla.org/en-US/firefox/addon/59/">here</a>.</p>

<p>Feel free to fork the project and improve it if necessary.</p>

<p>..and feel free to make a donation.</p>

<p><em>UPDATE</em> 20/12/2010 : Added support for more devices and fixed a critical issue on IE</p>

<p><em>UPDATE</em> 05/01/2011 : Version 0.5 released with support for &#8220;mobile_url&#8221; and &#8220;mobile_scheme&#8221; properties</p>

<p><em>UPDATE</em> 02/04/2011 : Version 0.8 released with support for &#8220;ipad_redirection&#8221; and &#8220;beforeredirection_callback&#8221; properties</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[How to update a Basic Asset in Fatwire Content Server]]></title>
    <link href="http://www.sebarmeli.com/blog/2010/10/20/how-to-update-a-basic-asset-in-fatwire-content-server/"/>
    <updated>2010-10-20T23:32:00+02:00</updated>
    <id>http://www.sebarmeli.com/blog/blog/2010/10/20/how-to-update-a-basic-asset-in-fatwire-content-server</id>
    <content type="html"><![CDATA[<p>These are the STEPS on how to update a Basic Asset Descriptor File, without causing any side effects:</p>

<ol>
<li><p>Update the Asset Descriptor File (from AssetType table)</p></li>
<li><p>Launching SQL scripts to update the DB keeping consistency with the ADF</p></li>
<li><p>Registering the Assets from the Fatwire Interface (Admin tab):</p></li>
</ol>


<p>Initially I was just updating the ADF, without updating the DB (thinking it was done automatically) but it was causing duplication of assets and weird messages for those fields I changed. So, remember to follow these steps and you’ll be safe</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Doctype for your pages]]></title>
    <link href="http://www.sebarmeli.com/blog/2010/10/05/doctype-for-your-pages/"/>
    <updated>2010-10-05T23:27:00+02:00</updated>
    <id>http://www.sebarmeli.com/blog/blog/2010/10/05/doctype-for-your-pages</id>
    <content type="html"><![CDATA[<p>Well..that’s how all the web pages start or should be…with a DOCTYPE or Document type Declaration</p>

<p>This is a list of handy XHTML 1.0/ XHTML 1.1 / HTML 5 Doctypes:</p>

<p><strong>XHTML 1.0 Strict</strong></p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span></span><span class="cp">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;</span>
</span><span class='line'><span class="cp">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p><strong>XHTML 1.0 Transitional</strong></p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span></span><span class="cp">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span>
</span><span class='line'><span class="cp">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p><strong>XHTML 1.0 Frameset</strong></p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span></span><span class="cp">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Frameset//EN&quot;</span>
</span><span class='line'><span class="cp">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd&quot;&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p><strong>XHTML 1.1 – DTD</strong></p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span></span><span class="cp">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.1//EN&quot;</span>
</span><span class='line'><span class="cp">&quot;http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd&quot;&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p><strong>HTML5</strong></p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span></span><span class="cp">&lt;!DOCTYPE HTML&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>You wanna know more? Read this article from the W3C and this one from A List Apart</p>

<p>After the DOCTYPE, you need to use <html>.
Rule of thumb: for XHTML 1.x use something like:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span></span><span class="p">&lt;</span><span class="nt">html</span> <span class="na">xmlns</span><span class="o">=</span><span class="s">&quot;http://www.w3.org/1999/xhtml&quot;</span> <span class="p">&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>you can add the lang and dir attribute as well to specify the language and the text direction(ltr or rtl)</p>

<p>for <strong>HTML5</strong> it’s enough to use:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span></span><span class="p">&lt;</span><span class="nt">html</span><span class="p">&gt;</span>
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Few things you may need to know on Mac (part2)]]></title>
    <link href="http://www.sebarmeli.com/blog/2010/09/21/few-things-you-may-need-to-know-on-mac-part2/"/>
    <updated>2010-09-21T23:22:00+02:00</updated>
    <id>http://www.sebarmeli.com/blog/blog/2010/09/21/few-things-you-may-need-to-know-on-mac-part2</id>
    <content type="html"><![CDATA[<ol>
<li><p><strong>How can I set an alias?</strong></p>

<p> You can set an alias with the command
 <code>
 alias cdlogs=”cd /opt/prog”
</code></p>

<p> but to store the alias you need to store the previous command in <em>~/.bash_profile</em></p></li>
<li><p><strong>Where can I find my Java version running on my Mac?</strong></p>

<p> <strong>which java</strong></p>

<p> <em>java –version</em> to know the version you’re using</p></li>
<li><p><strong>I often download some tar or tar.gz files, what are they?</strong></p>

<p> tar files are simply archived files. gz stands for Gzip, so means that the tar.gz files are archived and also Gzipped.</p>

<p> You usually can unzip it executing</p>

<p> <strong>tar xvzf file.tar.gz</strong></p>

<p> or create the zip executing</p>

<p> <strong>tar cvzf file1.txt file2.txt</strong></p></li>
<li><p><strong>What’s a DAEMON?</strong></p>

<p> It’s simply the equivalent of a Windows Service in Unix</p></li>
</ol>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Few things you may need to know on Mac (part1)]]></title>
    <link href="http://www.sebarmeli.com/blog/2010/09/13/few-things-you-may-need-to-know-on-mac-part1/"/>
    <updated>2010-09-13T23:08:00+02:00</updated>
    <id>http://www.sebarmeli.com/blog/blog/2010/09/13/few-things-you-may-need-to-know-on-mac-part1</id>
    <content type="html"><![CDATA[<p>In this article, I will go through few gotchas you may need to know when you start using a Mac (Leopard or later version), especially for development purpose. I’m assuming that you use the Terminal, but if you don’t ,look for it and open it. Even if at the beginning it can seem a bit boring or too geeky working with a shell, it can make your life much easier and it is really powerful.</p>

<ol>
<li><p><strong>Does Mac have a built-in web server?</strong></p>

<p> Yes, you can restart it from Console typing <strong>/usr/sbin/apachectl restart</strong>:</p>

<pre><code class="`"> sebastiano:~ sebastiano$ sudo /usr/sbin/apachectl restart
</code></pre>

<p> and you can find the configuration file at <strong>/etc/apache2/httpd.conf</strong> (I’m using “mate” to edit it, but you can use other editors):</p>

<pre><code class="`"> sebastiano:~ sebastiano$ sudo mate /etc/apache2/httpd/conf
</code></pre></li>
<li><p><strong>Where can I put my site to be hosted by built-in web server?</strong></p>

<p> Chuck your folder in <strong>~/Sites</strong> and you can display your folder at <em>http://localhost/~username/yoursite</em></p>

<p> or</p>

<p> put your files into <strong>/Library/WebServer/Documents</strong>, and you can access them on your web server at <em>http://localhost</em>.</p></li>
<li><p><strong>Where can I find my host file?</strong></p>

<p> Differently from Windows where you can find it at C:/Windows/System32/etc/drivers/hosts, on Mac it is located at <strong>/private/etc/hosts</strong></p>

<pre><code class="`"> sebastiano:~ sebastiano$ sudo mate /private/etc/hosts
</code></pre></li>
<li><p><strong>Where can I find the environment variable and how can I set them?</strong></p>

<p> If you type <strong>env</strong>, you can the environment variable set on your machine.
 To set an environment variable on your bash shell you need to use export command such as:</p>

<pre><code class="`"> sebastiano:~ sebastiano$ export JAVA_HOME=/Library/Java/Home
</code></pre>

<p> but if you want to store it, so that at the next access you still find this variable, you need to store it in <strong>/etc/bashrc</strong></p></li>
</ol>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Using Fatwire plugin for Eclipse]]></title>
    <link href="http://www.sebarmeli.com/blog/2010/08/28/using-fatwire-plugin-for-eclipse/"/>
    <updated>2010-08-28T22:32:00+02:00</updated>
    <id>http://www.sebarmeli.com/blog/blog/2010/08/28/using-fatwire-plugin-for-eclipse</id>
    <content type="html"><![CDATA[<p>Fatwire Content Server doesn’t offer any official plugin with Eclipse at the moment, but digging on the web, I found this nice plugin at <a href="http://www.nl.fatwire.com/ide/installation.html">http://www.nl.fatwire.com/ide/installation.html</a>.</p>

<p>The documentation is not extensive, but you can manage to install it quite quickly.</p>

<p>After installing the plugin for Eclipse (I think you need at least Eclipse 3.4), you need to:</p>

<ol>
<li>Right click on the project, select Enable ContentServer</li>
<li>Right click on the project, select ContentServer and then Checkout to checkout all the JSPs</li>
<li>Enter username, password, hostname, port, and servlet path (such as “/ContentServer”)</li>
<li>You can select the publication you’d like – publication is a Site in Fatwire “world”</li>
<li>Select the folder where you want to download all Templates and CSElements. Template and CSElements are the two main Fatwire code components (JSPs)</li>
</ol>


<p>and now you easily update and commit your JSPs from Eclipse and take advantage of all Eclipse cool features.</p>

<p>Just a couple of hints:</p>

<ul>
<li><p>Before you commit a jsp it’s safer to update it first. So right click on your JSP in Project Explorer View, select “Content Server” and then “Update” . After that select again “Content Server” and then “Commit”. This is because I have noticed that sometimes the plugin creates issues if you commit before updating.</p></li>
<li><p>Template and CSElement tables needs to have tracking off to let developers commit.</p></li>
</ul>


<p>The plugin doesn’t work perfectly sometimes especially if you edit your Template/CSElement from a IDE/Test Editor different than Eclipse.</p>

<p>Last suggestion, having disable tracking from “Template” and “CSElement” tables, you need another system to keep track of your change…use SVN!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Upgrade from Spring 2.0 to Spring 2.5]]></title>
    <link href="http://www.sebarmeli.com/blog/2010/06/15/upgrade-from-spring-2-dot-0-to-spring-2-dot-5/"/>
    <updated>2010-06-15T22:30:00+02:00</updated>
    <id>http://www.sebarmeli.com/blog/blog/2010/06/15/upgrade-from-spring-2-dot-0-to-spring-2-dot-5</id>
    <content type="html"><![CDATA[<p>I’ve been upgrading recently the webapp I’m working on the moment from Spring 2.0 to Spring 2.5 (in the process to upgrade to 3.0) to enjoy Spring Annotations power.</p>

<p>This task is pretty straightforward and requires to substitute jars from 2.0 version with 2.5 version. It’s enough to download Spring Framework from here.
Big gotcha : spring-webmvc.jar is not part of spring.jar any more so you need to include both the jars to use SpringMVC.</p>

<p>Now you can start enjoying annotation!
Start changing your Spring configuration file (dispatcher-servlet.xml or whatever you configure in web.xml) with something like:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='xml'><span class='line'><span></span><span class="err">&lt;</span>?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?;&gt;
</span><span class='line'><span class="nt">&lt;beans</span> <span class="na">xmlns=</span><span class="s">&quot;http://www.springframework.org/schema/beans&quot;</span>
</span><span class='line'><span class="na">xmlns:xsi=</span><span class="s">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
</span><span class='line'><span class="na">xmlns:aop=</span><span class="s">&quot;http://www.springframework.org/schema/aop&quot;</span>
</span><span class='line'><span class="na">xmlns:oxm=</span><span class="s">&quot;http://www.springframework.org/schema/oxm&quot;</span>
</span><span class='line'><span class="na">xsi:schemaLocation=</span><span class="s">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>
</span><span class='line'>// Declare all Beans here
</span><span class='line'>
</span><span class='line'><span class="nt">&lt;/beans&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Ok, now you’re ready to use @Autowired, @Controller, @RequestMapping and so forth!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[CSS fix for PNG(IE6)]]></title>
    <link href="http://www.sebarmeli.com/blog/2010/06/13/css-fix-for-png-ie6/"/>
    <updated>2010-06-13T22:18:00+02:00</updated>
    <id>http://www.sebarmeli.com/blog/blog/2010/06/13/css-fix-for-png-ie6</id>
    <content type="html"><![CDATA[<p>PNG (Portable Network Graphics) format for images is not well supported in IE6 as you know. To fix this issue, you can use some nasty JS, that will break the progressive enhancement of your page (but it will keep your CSS valid) or you can use some CSS that will use “filter” property and some expression. This will make your page not pass the W3C Validation for CSS, as expressions are not allowed, but I still prefer this solution on the JS one, even because this fix is related to presentation layer (CSS) and not the behaviour layer (JS).</p>

<p>Waiting that IE6 will be no longer supported, you can grab here the fix to support PNG in IE6.
In the following example, we need to support a .png image as background for a class “png-back”:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='css'><span class='line'><span></span><span class="p">.</span><span class="nc">png-back</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">background</span><span class="p">:</span><span class="kc">none</span><span class="p">;</span>
</span><span class='line'>  <span class="k">filter</span><span class="p">:</span><span class="n">progid</span><span class="o">:</span><span class="n">DXImageTransform</span><span class="o">.</span><span class="n">Microsoft</span><span class="o">.</span><span class="nf">AlphaImageLoader</span><span class="p">(</span><span class="n">enabled</span><span class="err">=</span><span class="s1">&#39;true&#39;</span><span class="p">,</span> <span class="n">src</span><span class="err">=</span><span class="o">/</span><span class="n">img</span><span class="o">/</span><span class="n">file</span><span class="err">.</span><span class="n">png</span><span class="s1">&#39;, sizingMethod=&#39;</span><span class="n">scale</span><span class="err">&#39;</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>This code will do the job and it’ll make your png image work perfectly as background for “png-back” class.</p>

<p>You can add this code to a specific IE6 stylesheet (e.g. ie6.css) and refer to it into your HTML through a conditional statements:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span></span><span class="c">&lt;!--[if lte IE 6]--&gt;</span>
</span><span class='line'><span class="p">&lt;</span><span class="nt">link</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;text/css&quot;</span> <span class="na">rel</span><span class="o">=</span><span class="s">&quot;stylesheet&quot;</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;/img/ie6.css&quot;</span> <span class="p">/&gt;</span>
</span><span class='line'><span class="c">&lt;!--[end if]--&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p><em>[if lte IE 6]</em> stands for “if your IE browser version is less or equal than 6″.
By the way, conditional expressions should be avoided, but often we need to use to support IE6 leaks. So the best solution will be…DO NOT USE IE6!</p>
]]></content>
  </entry>
  
</feed>
