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

<channel>
	<title>Kamibu &#187; Javascript</title>
	<atom:link href="http://blog.kamibu.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.kamibu.com</link>
	<description></description>
	<lastBuildDate>Sat, 14 Nov 2009 10:51:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Frontend optimizations</title>
		<link>http://blog.kamibu.com/2008/06/15/frontend-optimizations/</link>
		<comments>http://blog.kamibu.com/2008/06/15/frontend-optimizations/#comments</comments>
		<pubDate>Sun, 15 Jun 2008 20:35:22 +0000</pubDate>
		<dc:creator>Izual</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Excalibur]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.kamibu.com/?p=39</guid>
		<description><![CDATA[In Excalibur Phoenix one aspect we are giving major attention to is frontend. In contrast to Excalibur Reloaded (the previous version) we try not only to provide a user-friendly interface but we also want it to be as fast as possible for the client. The latter is mostly a need that came up as more [...]]]></description>
			<content:encoded><![CDATA[<p>In Excalibur Phoenix one aspect we are giving major attention to is frontend.</p>
<p>In contrast to Excalibur Reloaded (the previous version) we try not only to provide a user-friendly interface but we also want it to be as fast as possible for the client. The latter is mostly a need that came up as more and more users started using Zino. Furthermore,  through the process of optimization we learn new techniques we weren&#8217;t keen with, that extend by far our knowledge especially on the way the browsers render a page and the lower level mechanisms that play a major part when there is a greater need for speed (the term is stolen from Google Blog <img src='http://blog.kamibu.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  ). So I want to write a few lines for some optimizations we started implementing in the new version.</p>
<p>1) Lessen HTTP requests</p>
<p>One of the most important aspects is to lessen the HTTP requests that are needed for a page to show properly. Till now, our pages had icons, images, many stylesheets and scripts. Each of those needed a separate HTTP request in order to download since all these files were separated. For example, we used to have the stylesheet for the user profile on a different file from the one for the frontpage.  What we do now, is combine all the scripts in one file, all the stylsheets in another and so we use just two separate files for all our styling and javascript. Another thing we did is known with the term &#8220;spriting&#8221;. We combined all of our background images into one bigger image file. Everytime we need to show an icon, we use this image we created as a source and through the background-position we specify exactly which icon we want to be visible. Those are the most common and effective ways we are currently using in Phoenix</p>
<p>2) Cache content</p>
<p>Another way of reducing HTTP requests and the weight of downloading for the client is to use caching. More specifically caching with far-future headers set. To make things a bit clearer if the browser has cached some of the needed components he has to check whether those are valid according to the Last-Modified header in the response. If the component hasn&#8217;t been modified there is no need to download it, so the size of the download decreases.  Though a request has been made, that could have been avoided if we used an Expires header. When we set an expires header, we are actually telling the browser to use the components in its cache till a specified date.  Until this expiry date the browser uses its cached content without the need to ask for modification of the content. This is more useful with static data such as javascript, stylesheets and icons.</p>
<p>3) Minification</p>
<p>In projects where great amount of javascript is in use, minifying the code makes a lot sense. With the term minification we mean the elimination of spaces, line breaks and comments from the original code. In other words all unnecessary stuff is removed in order to lessen the size of the data the client will download. This technique can also be used for stylesheets but is not so effective. Stylesheets optimization is a bit more complex, as you can get the most of it if you find duplicate rules for elements and remove them. Some developers go a step further with obfuscation but I believe it&#8217;s not worth. In obfuscation variable and function names are changed automatically to shorter ones, in order to reduce even more the size. Another reason for using obfuscation is to make the code unreadable to others. Implementing this technique is not as simple as minification and using it should be examined carefully as bugs are likely to appear because of core function name substitution.</p>
<p>4) Gzipping content</p>
<p>One last technique worth mentioning is to use gzip compression. The use of this technique is pretty obvious. Compression reduces download sizes leading to faster downstreaming times for the client. Gzipping is supported by almost all modern browsers so it doesn&#8217;t need further browser compatibility thoughts. This technique is more effective when combined with the others described. Minification combined with gzip compression decreases significantly the size of the content. To use it though the web server should be configured properly.</p>
<p>Last but not least few simple things to look after are: avoiding duplicate script inclusion, loading stylesheets at the beginning of the document, scripts at the bottom and sometimes clever content preloading that will be needed later on, while the user is interacting with other parts of the page.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kamibu.com/2008/06/15/frontend-optimizations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery Style Rules</title>
		<link>http://blog.kamibu.com/2008/06/12/jquery-style-rules/</link>
		<comments>http://blog.kamibu.com/2008/06/12/jquery-style-rules/#comments</comments>
		<pubDate>Thu, 12 Jun 2008 21:57:13 +0000</pubDate>
		<dc:creator>kostis90gr</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[rules]]></category>
		<category><![CDATA[style]]></category>

		<guid isPermaLink="false">http://blog.kamibu.com/?p=38</guid>
		<description><![CDATA[In Excalibur Phoenix a really powerful JavaScript library is used, jQuery. It allows developers to create JavaScript code fast, using only a few lines, and also offers a great variety of features that make coding life easier. jQuery code, though, doesn&#8217;t have many similarities with regular JavaScript DOM code. This post contains a few suggestions [...]]]></description>
			<content:encoded><![CDATA[<p>In Excalibur Phoenix a really powerful JavaScript library is used, <a href="http://jquery.com">jQuery</a>. It allows developers to create JavaScript code fast, using only a few lines, and also offers a great variety of features that make coding life easier. jQuery code, though, doesn&#8217;t have many similarities with regular JavaScript DOM code. This post contains a few suggestions about how Kamibu developers should write jQuery code. Before you start reading this post, please take a quick look on <a href="http://www.dionyziz.com/Style">this<br />
article</a>, since those style rules should be also applied whenever possible.</p>
<p>1) jQuery allows developers to execute more than one jQuery class methods using a technique called method chaining. Method Chaining should be used to the maximum by Kamibu developers.</p>
<p><em>Do not use this:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.newcomment&quot;</span> <span style="color: #66cc66;">&#41;</span>.clone<span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #66cc66;">&#41;</span>;
$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.newcomment&quot;</span> <span style="color: #66cc66;">&#41;</span>.css<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;opacity&quot;</span>, <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span>.removeClass<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;newcomment&quot;</span> <span style="color: #66cc66;">&#41;</span>;</pre><br />
<em>Use this instead:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.newcomment&quot;</span> <span style="color: #66cc66;">&#41;</span>.clone<span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #66cc66;">&#41;</span>.css<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;opacity&quot;</span>, <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span>.removeClass<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;newcomment&quot;</span> <span style="color: #66cc66;">&#41;</span>;</pre></p>
<p>The first example is not as fast as the second, since two DOM searches are performed.</p>
<p>2) Sometimes, however, chaining makes the code complex and difficult to read, especially when find() is used a lot of times. Therefore, when end() is used, the developer should change a line exactly after it.</p>
<p><em>Do not use this:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.comments&quot;</span> <span style="color: #66cc66;">&#41;</span>.find<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;span.time&quot;</span> <span style="color: #66cc66;">&#41;</span>.text<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;πριν λίγο&quot;</span> <span style="color: #66cc66;">&#41;</span>.<a href="http://www.php.net/end"><span style="color: #000066;">end</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.find<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.text&quot;</span> <span style="color: #66cc66;">&#41;</span>.<a href="http://www.php.net/empty"><span style="color: #000066;">empty</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.append<span style="color: #66cc66;">&#40;</span> document.createTextNode<span style="color: #66cc66;">&#40;</span> texter <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>.<a href="http://www.php.net/end"><span style="color: #000066;">end</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre><br />
<em>Use this instead:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.comments&quot;</span> <span style="color: #66cc66;">&#41;</span>.find<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;span.time&quot;</span> <span style="color: #66cc66;">&#41;</span>.text<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;πριν λίγο&quot;</span> <span style="color: #66cc66;">&#41;</span>.<a href="http://www.php.net/end"><span style="color: #000066;">end</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
.find<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.text&quot;</span> <span style="color: #66cc66;">&#41;</span>.<a href="http://www.php.net/empty"><span style="color: #000066;">empty</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.append<span style="color: #66cc66;">&#40;</span> document.createTextNode<span style="color: #66cc66;">&#40;</span> texter <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>.<a href="http://www.php.net/end"><span style="color: #000066;">end</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></p>
<p>3) This new JavaScript library provides a lot of ways to select a DOM node, apart from getElementById and getElementsByTagName. You should always use jQuery selectors, before trying to find another way to select the node you want.</p>
<p><em>Do not use this:</em><br />
<pre class="php"><span style="color: #000000; font-weight: bold;">var</span> node = $<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.comments&quot;</span> <span style="color: #66cc66;">&#41;</span>.get<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span>;
node = node.childNodes<span style="color: #66cc66;">&#91;</span> node.childNodes.length<span style="color: #cc66cc;">-1</span> <span style="color: #66cc66;">&#93;</span>;</pre><br />
<em>Use this instead:</em><br />
<pre class="php"><span style="color: #000000; font-weight: bold;">var</span> node = $<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.comments:last-child&quot;</span> <span style="color: #66cc66;">&#41;</span>.get<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span>;</pre></p>
<p><em>And do not use this:</em><br />
<pre class="php"><span style="color: #000000; font-weight: bold;">var</span> node = $<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.comments&quot;</span> <span style="color: #66cc66;">&#41;</span>.eq<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span>;</pre><br />
<em>Use this instead:</em><br />
<pre class="php"><span style="color: #000000; font-weight: bold;">var</span> node = $<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.comments:first&quot;</span> <span style="color: #66cc66;">&#41;</span>;</pre></p>
<p>4) Even though selectors could become really long, do not break them into parts, unless you have a good reason to do so.</p>
<p><em>Do not use this:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;#dilution div.comments div.text&quot;</span> <span style="color: #66cc66;">&#41;</span>.find<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div div:last-child&quot;</span> <span style="color: #66cc66;">&#41;</span>;</pre><br />
<em>Use this instead:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;#dilution div.comments div.text div div:last-child&quot;</span> <span style="color: #66cc66;">&#41;</span>;</pre></p>
<p>5) When you want to create a node and append it to an element do not use jQuery&#8217;s append method using a string as an argument. Firstly, create the element you want using DOM JavaScript, and then append it to the node you want. This solution should be preffered since it is more object-oriented. Use jQuery&#8217;s append( &#8220;string&#8221; ) only when you would use innerHTML in normal DOM</p>
<p><em>Do not use this:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.bottom&quot;</span> <span style="color: #66cc66;">&#41;</span>.append<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;&lt;a href='http://www.zino.gr'&gt;Click Me&lt;/a&gt;&quot;</span> <span style="color: #66cc66;">&#41;</span>;</pre><br />
<em>Use this instead:</em><br />
<pre class="php"><span style="color: #000000; font-weight: bold;">var</span> a = document.createElement<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'a'</span> <span style="color: #66cc66;">&#41;</span>;
a.href = <span style="color: #ff0000;">&quot;http://www.zino.gr&quot;</span>;
a.appendChild<span style="color: #66cc66;">&#40;</span> document.createTextNode<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'Click Me'</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;div.bottom&quot;</span> <span style="color: #66cc66;">&#41;</span>.append<span style="color: #66cc66;">&#40;</span> a <span style="color: #66cc66;">&#41;</span>;</pre></p>
<p>6) Always try to use jQuery&#8217;s characteristics as much as you can, before you try to find another &#8220;manual&#8221; way. A characteristic example is jQuery&#8217;s <a href="http://docs.jquery.com/Attributes/toggleClass">toggleClass</a> method.</p>
<p>7) Always prefer jQuery&#8217;s events except for the following situations.<br />
        7.1 When a DOM element that will be appended is going to be bound to an event.<br />
        7.2 When an argument from backend is going to be used.<br />
                   <em>You should use this:</em><br />
                   <pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
				<span style="color: #0000ff;">$foo</span> = <span style="color: #cc66cc;">5</span>;
			?&gt;&lt;a onclick=<span style="color: #ff0000;">&quot;function() { alert( &lt;?php
			echo $foo;
			?&gt; );return false;&quot;</span> &gt;Test&lt;/a&gt;</pre><br />
         7.3 When an event that was not bounded by jQuery is going to be replaced.<br />
                   <em>Do not use this:</em><br />
                   <pre class="php"><span style="color: #000000; font-weight: bold;">var</span> a = document.createElement<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'a'</span> <span style="color: #66cc66;">&#41;</span>;
		a.onclick = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
			alert<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'lolek'</span> <span style="color: #66cc66;">&#41;</span>; 
		<span style="color: #66cc66;">&#125;</span>;
		$<span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span>.click<span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> alert<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'bolek'</span> <span style="color: #66cc66;">&#41;</span>; <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;</pre><br />
                   <em>Use this instead:</em><br />
                   <pre class="php"><span style="color: #000000; font-weight: bold;">var</span> a = document.createElement<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'a'</span> <span style="color: #66cc66;">&#41;</span>;
		a.onclick = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
			alert<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'lolek'</span> <span style="color: #66cc66;">&#41;</span>; 
		<span style="color: #66cc66;">&#125;</span>;
		a.onclick = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			alert<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'bolek'</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>;</pre></p>
<p>                  <strong>Notice:</strong> Keep in mind that jQuery supports multiple events. The first example, will just add another function to be executed when a is clicked. So two alerts will pop up (lolek,bolek). Moreover, the unbind method cannot be used since the first event was not bounded using jQuery.</p>
<p>8 ) Use fadeIn, fadeOut, fadeTo, show, hide methods, instead of the more general animate method, whenever possible.</p>
<p><em>Do not use this:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'chiki'</span> <span style="color: #66cc66;">&#41;</span>.animate<span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> opacity : <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#125;</span>, <span style="color: #cc66cc;">400</span> <span style="color: #66cc66;">&#41;</span>;</pre><br />
<em>Use this instead:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'chiki'</span> <span style="color: #66cc66;">&#41;</span>.fadeOut<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">400</span> <span style="color: #66cc66;">&#41;</span>;</pre></p>
<p>9) Prefer jQuery&#8217;s methods instead of the body&#8217;s onload event.</p>
<p><em>Do not use this:</em><br />
<pre class="php">&lt;body onload=<span style="color: #ff0000;">&quot;alert( 'done' );&quot;</span>&gt;&lt;/body&gt;</pre><br />
<em>Use this instead:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span>document<span style="color: #66cc66;">&#41;</span>.ready<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		alert<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'done'</span> <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;</pre></p>
<p>Moreover,<br />
<em>Do not use this:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		alert<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'done'</span> <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;</pre><br />
<em>Use this instead:</em><br />
<pre class="php">$<span style="color: #66cc66;">&#40;</span>document<span style="color: #66cc66;">&#41;</span>.ready<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		alert<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'done'</span> <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;</pre></p>
<p><strong>Notice:</strong> Use &#8220;jQuery(function($) {&#8221; format only when absolutely necessary</p>
<p>10) Always prefer $( &#8216;something&#8217; ) instead of jQuery( &#8216;something&#8217; ), unless a compatibility issue emerges.</p>
<p>11) Kamibu Specific:<br />
        11.1 Use jQuery&#8217;s effects instead of the objects of animation.js<br />
               <em>Do not use this:</em><br />
               <pre class="php">Animations.Create<span style="color: #66cc66;">&#40;</span> document.getElementById<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'fire'</span> <span style="color: #66cc66;">&#41;</span> , <span style="color: #ff0000;">'opacity'</span> , <span style="color: #cc66cc;">2000</span> , <span style="color: #cc66cc;">1</span> , <span style="color: #cc66cc;">0.3</span> <span style="color: #66cc66;">&#41;</span>;</pre><br />
                <em>Use this instead:</em><br />
                <pre class="php">$<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'#fire'</span> <span style="color: #66cc66;">&#41;</span>.css<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'opacity'</span>, <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span>.fadeTo<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">2000</span>, <span style="color: #cc66cc;">0.3</span> <span style="color: #66cc66;">&#41;</span>;</pre><br />
        11.2 Prefer Coala instead of jQuery.ajax</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kamibu.com/2008/06/12/jquery-style-rules/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>onmousedown vs onclick</title>
		<link>http://blog.kamibu.com/2007/10/20/onmousedown-vs-onclick/</link>
		<comments>http://blog.kamibu.com/2007/10/20/onmousedown-vs-onclick/#comments</comments>
		<pubDate>Sat, 20 Oct 2007 06:59:16 +0000</pubDate>
		<dc:creator>kostis90gr</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://blog.kamibu.com/2007/10/20/onmousedown-vs-onclick/</guid>
		<description><![CDATA[This may seem like a stupid post to some of you, but personally I never quite understood the need for all those click events to exist (onmouseup,onmousedown,onclick). Only the circumstances under which those events occured were slightly different. Recently, I watched a speech at the Yahoo! Videos by Joseph Smarr titled &#8220;High-performance JavaScript: Why Everything [...]]]></description>
			<content:encoded><![CDATA[<p>This may seem like a stupid post to some of you, but personally I never quite understood the need for all those click events to exist (onmouseup,onmousedown,onclick). Only the circumstances under which those events occured were slightly different. Recently, I watched <a href="http://video.yahoo.com/video/play?vid=1041101">a speech at the Yahoo! Videos by Joseph Smarr titled &#8220;High-performance JavaScript: Why Everything You&#8217;ve Been Taught Is Wrong&#8221;</a>. He suggested some changes in the way JS is used and one of them was the replacement of the onclick event by onmousedown. Inspired by this, I am going to present the results by some &#8220;experiments&#8221; that took place in my lab <img src='http://blog.kamibu.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> .</p>
<ul><strong>1)When Are Those Events Triggered?</strong></ul>
<p>Obviously, onmousedown is triggered when the user presses on one of the mouse buttons, onmouseup when he releases one of them and onclick when the user clicks somewhere. But hey! Clicking is done in 2 steps! Firstly, the user presses the mouse button and then he releases it. So when is the onclick event actually triggered? It turns out that it&#8217;s triggered only after the user has released his mouse button. But hey! That is the onmouseup event! Even though it may seem logical, things are a bit different. The onmouseup event is triggered <strong>only</strong> when the mouse button is released. For example, you have an HTML button with an onmouseup listener attached to it. You can click anywhere on the page and while having your mouse button pressed, drag the pointer on that HTML button. Then release the mouse button and the event will be triggered. With the onclick event, however, the user must have his pointer on the HTML button when his presses his mouse button <strong>and</strong> also must have his pointer on the HTML button when he releases his mouse button. Therefore, someone can assume that onclick is something like a combination of the onmousedown and onmouseup events. This, also, explains the reason why the order the events take place is: onmousedown-onmouseup-onclick</p>
<ul> <strong>2)How could this affect my code?</strong></ul>
<p>Since onclick is the combination of the two events, it takes a certain amount of time before the programmer&#8217;s defined function in the onclick event will be run. This amount equals to the time the user needs to press the mouse button and release it. The following code is an attempt to measure that time:</p>
<p><pre class="php">&lt;html&gt;
&lt;head&gt;&lt;title&gt;onmousedown vs onclick&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;
&lt;form&gt;
&lt;input type=<span style="color: #ff0000;">&quot;button&quot;</span> value=<span style="color: #ff0000;">&quot;Click Me&quot;</span> onmousedown=<span style="color: #ff0000;">&quot;start();return false;&quot;</span> onmouseup=<span style="color: #ff0000;">&quot;end();return false&quot;</span> /&gt;
&lt;/form&gt;
&lt;script type=<span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;
<span style="color: #000000; font-weight: bold;">var</span> startt;
<span style="color: #000000; font-weight: bold;">var</span> endt;
<span style="color: #000000; font-weight: bold;">function</span> start<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> now = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.php.net/date"><span style="color: #000066;">Date</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	startt = now.getMilliseconds<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + now.getSeconds<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>*<span style="color: #cc66cc;">1000</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> <a href="http://www.php.net/end"><span style="color: #000066;">end</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> now = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.php.net/date"><span style="color: #000066;">Date</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	endt = now.getMilliseconds<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + now.getSeconds<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>*<span style="color: #cc66cc;">1000</span> - startt;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span>
&lt;/body&gt;
&lt;/html&gt;</pre></p>
<p>It turns out that a &#8220;fast&#8221; click takes about 20-30ms and a &#8220;normal&#8221; click about 70-120ms and sometimes more. This means that <strong>at least</strong> 70-80ms will be spared before the actual function will run!</p>
<ul><strong>3)Cons of the onmousedown event</strong></ul>
<p>Even though the onmousedown event saves you all this time each time something is clicked it doesn&#8217;t allow the user to &#8220;regret&#8221; for his action. For example, by the nature of the onclick event, a user can click on an HTML button, and while this button is pressed, regret for his action. He can then drag the mouse out of that HTML button and then release the mouse button and nothing will happen. Personally, I believe that Chit-Chat users don&#8217;t do this often. And even if they somethimes do, most parts of our site somehow provide an undo action. For example: Comments, Frelations, Albums, Questions and so on.</p>
<p>Except for that, I have a feeling that onmousedown is not fully compatible with IE. I wasn&#8217;t yet able to test this out, but I will soon.</p>
<p>I hope you learnt something from this post <img src='http://blog.kamibu.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kamibu.com/2007/10/20/onmousedown-vs-onclick/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JSLinting</title>
		<link>http://blog.kamibu.com/2007/08/11/jslinting/</link>
		<comments>http://blog.kamibu.com/2007/08/11/jslinting/#comments</comments>
		<pubDate>Sat, 11 Aug 2007 18:12:14 +0000</pubDate>
		<dc:creator>dionyziz</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://blog.kamibu.com/2007/08/11/jslinting/</guid>
		<description><![CDATA[I just recently found out about jslint, a Javascript library for Javascript parsing. Made a JS checker for the Excalibur Development Team: I hope you guys find it useful.]]></description>
			<content:encoded><![CDATA[<p>I just recently found out about <a href="http://www.jslint.com/">jslint</a>, a Javascript library for Javascript parsing.</p>
<p>Made a JS checker for the Excalibur Development Team:</p>
<p><a href='http://blog.kamibu.com/wp-content/uploads/2007/08/jslintxc.png' title='jslint'><img src='http://blog.kamibu.com/wp-content/uploads/2007/08/jslintxc.thumbnail.png' alt='jslint' /></a></p>
<p>I hope you guys find it useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kamibu.com/2007/08/11/jslinting/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Entering a child = Leaving parent?</title>
		<link>http://blog.kamibu.com/2007/07/15/entering-a-child-leaving-parent/</link>
		<comments>http://blog.kamibu.com/2007/07/15/entering-a-child-leaving-parent/#comments</comments>
		<pubDate>Sun, 15 Jul 2007 15:10:54 +0000</pubDate>
		<dc:creator>abresas</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://blog.kamibu.com/2007/07/15/entering-a-child-leaving-parent/</guid>
		<description><![CDATA[At least this is what internet browsers think.. or ok, not exactly. Let me explain: If you have an element which has onmouseover and onmouseout events, and children inside it, if you enter one of the children, onmouseout of parent element will fire, then the onmouseover of the child, and then again the onmouseout of [...]]]></description>
			<content:encoded><![CDATA[<p>At least this is what internet browsers think.. or ok, not exactly.</p>
<p>Let me explain:</p>
<p>If you have an element which has <b>onmouseover</b> and <b>onmouseout</b> events, and children inside it,  if you enter one of the children, onmouseout of parent element will fire, then the onmouseover of the child, and then again the onmouseout of the parent.</p>
<p>Example:</p>
<p><pre class="html4strict"><span style="color: #009900;"><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">&lt;body&gt;</span></a></span>
     <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;width: 500px; height: 500px; background-color: red;&quot;</span> 
     <span style="color: #000066;">onmouseout</span>=<span style="color: #ff0000;">&quot;alert( 'leaving parent' );&quot;</span>  
     <span style="color: #000066;">onmouseover</span>=<span style="color: #ff0000;">&quot;alert( 'entering parent' );&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;parent&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
          <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;width: 100px; height: 100px; background-color: blue;&quot;</span> 
         <span style="color: #000066;">onmouseover</span>=<span style="color: #ff0000;">&quot;alert( 'entering child' );&quot;</span> 
         <span style="color: #000066;">onmouseout</span>=<span style="color: #ff0000;">&quot;alert( 'leaving child' );&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;child&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body&gt;</span></span></pre></p>
<p>If you enter the parent div, you will get an alert saying: &#8220;Entering parent&#8221; (obviously).  But if you then enter the child-div,</p>
<p>you will get these alerts (consecutively):</p>
<p><i>&#8220;Leaving parent&#8221;</i>, <i>&#8220;Entering child&#8221;</i>, <i>&#8220;Entering parent&#8221;</i></p>
<p>The same thing happens if you then leave the child: you will get these alerts:</p>
<p><i>&#8220;Leaving child&#8221;</i>, <i>&#8220;Leaving parent&#8221;</i>, <i>&#8220;Entering parent&#8221;</i> </p>
<p></p>
<p>Now, suppose that you have a &lt;div&gt; element,<br />
which on mouse-over creates a &lt;form&gt; child and on mouse-out destroys it.</p>
<p>This won&#8217;t lead to the result you &#8216;d expect: </p>
<p>When you mouse-over it, it will create the &lt;form&gt;,<br />
but instantly destroy it because you entered the child. </p>
<p>And then again, as you are not on the child,<br />
you are re-entering the &lt;div&gt; and thus creating the &lt;form&gt; again.</p>
<p>&#8230;and so on.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kamibu.com/2007/07/15/entering-a-child-leaving-parent/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Odd empty textNodes</title>
		<link>http://blog.kamibu.com/2007/06/04/5/</link>
		<comments>http://blog.kamibu.com/2007/06/04/5/#comments</comments>
		<pubDate>Mon, 04 Jun 2007 21:03:33 +0000</pubDate>
		<dc:creator>kostis90gr</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://blog.kamibu.com/2007/06/04/5/</guid>
		<description><![CDATA[Not long ago, as I walked through the valley of the shadow of DOM, trying to help a friend of mine in a script he was writing, I found out that many elements had more children nodes than I thought they should have. For example: &#60;html&#62; &#60;head&#62; &#60;title&#62;Kamibu&#60;/title&#62; &#60;/head&#62; &#60;body&#62; &#60;i&#62; &#60;b&#62;a&#60;/b&#62; &#60;/i&#62; &#60;/body&#62; &#60;/html&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>Not long ago, as I walked through the valley of the shadow of DOM, trying to help a friend of mine in a script he was writing, I found out that many elements had more children nodes than I thought they should have. For example:</p>
<p><pre class="html4strict"><span style="color: #009900;"><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">&lt;html&gt;</span></a></span>
  <span style="color: #009900;"><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">&lt;head&gt;</span></a></span>
    <span style="color: #009900;"><a href="http://december.com/html/4/element/title.html"><span style="color: #000000; font-weight: bold;">&lt;title&gt;</span></a></span>Kamibu<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head&gt;</span></span>
  <span style="color: #009900;"><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">&lt;body&gt;</span></a></span>
    <span style="color: #009900;"><a href="http://december.com/html/4/element/i.html"><span style="color: #000000; font-weight: bold;">&lt;i&gt;</span></a></span>
      <span style="color: #009900;"><a href="http://december.com/html/4/element/b.html"><span style="color: #000000; font-weight: bold;">&lt;b&gt;</span></a></span>a<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/b&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/i&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html&gt;</span></span></pre></p>
<p>From the first look it seems like the &#8216;i&#8217; element has only one child node, the &#8216;b&#8217; element. Using the following code at my browser though:</p>
<p><pre class="javascript"><span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>document.<span style="color: #006600;">body</span>.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">childNodes</span>.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#41;</span>;</pre></p>
<p>the number 3 comes up! But how could it be? :S By trying to alert the nodeName of these children</p>
<p><pre class="javascript"><span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>document.<span style="color: #006600;">body</span>.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">nodeName</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>document.<span style="color: #006600;">body</span>.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">nodeName</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>document.<span style="color: #006600;">body</span>.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">nodeName</span><span style="color: #66cc66;">&#41;</span>;</pre></p>
<p>I got:</p>
<p>#text<br />
B<br />
#text</p>
<p>And the thing is getting even more strange when the following code returns empty pop-ups:</p>
<p><pre class="javascript"><span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>document.<span style="color: #006600;">body</span>.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">nodeValue</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>document.<span style="color: #006600;">body</span>.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">nodeValue</span><span style="color: #66cc66;">&#41;</span>;</pre></p>
<p>After a short discussion with dionyziz about this subject, he explained to me that the empty textNodes were actually the spaces that existed in the source code for it to be easily readable. Therefore, the following pieces of code are different:</p>
<p><pre class="html4strict"><span style="color: #009900;"><a href="http://december.com/html/4/element/i.html"><span style="color: #000000; font-weight: bold;">&lt;i&gt;</span></a></span>
 <span style="color: #009900;"><a href="http://december.com/html/4/element/b.html"><span style="color: #000000; font-weight: bold;">&lt;b&gt;</span></a></span>a<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/b&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/i&gt;</span></span></pre><br />
and<br />
<pre class="html4strict"><span style="color: #009900;"><a href="http://december.com/html/4/element/i.html"><span style="color: #000000; font-weight: bold;">&lt;i&gt;</span></a></span><span style="color: #009900;"><a href="http://december.com/html/4/element/b.html"><span style="color: #000000; font-weight: bold;">&lt;b&gt;</span></a></span>a<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/b&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/i&gt;</span></span></pre><br />
, since the &#8216;i&#8217; element of the second one has only 1 child &#8216;b&#8217;.</p>
<p>This may result in unexplainable scripting errors when someone is trying to access a child element of a node and instead he accesses a blank text Node. The following short function can be used in order to determine whether a node is a textNode or not:</p>
<p><pre class="javascript">&lt;script language=<span style="color: #3366CC;">'text/javascript'</span>&gt;
<span style="color: #003366; font-weight: bold;">function</span> isTextNode<span style="color: #66cc66;">&#40;</span>node<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>node.<span style="color: #006600;">nodeType</span> == <span style="color: #CC0000;">3</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>;
<span style="color: #66cc66;">&#125;</span>
&lt;/script&gt;</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kamibu.com/2007/06/04/5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Handling .nodeName in different browsers</title>
		<link>http://blog.kamibu.com/2007/05/27/handling-nodename-in-different-browsers/</link>
		<comments>http://blog.kamibu.com/2007/05/27/handling-nodename-in-different-browsers/#comments</comments>
		<pubDate>Sun, 27 May 2007 17:13:20 +0000</pubDate>
		<dc:creator>dionyziz</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://blog.kamibu.com/2007/05/27/handling-nodename-in-different-browsers/</guid>
		<description><![CDATA[Recently, Izual and I were experimenting with DOM in order to add some more eye-candy to Chit-Chat, and experienced a mistake that often causes browser incompatibilities, but is looked over without paying much attention: The nodeName attribute of a DOM element is not always reported in the case that it appears in your document. According [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, Izual and I were experimenting with DOM in order to add some more eye-candy to <a href="http://www.chit-chat.gr/">Chit-Chat</a>, and experienced a mistake that often causes browser incompatibilities, but is looked over without paying much attention:</p>
<p>The <a href="http://developer.mozilla.org/en/docs/DOM:element.nodeName">nodeName</a> attribute of a DOM element is not always reported in the case that it appears in your document. According to XHTML, all tags should be in lower-case; however, Internet Explorer 7, even if your tags are in lower case, returns the nodeName in upper case. This happens even when the rendering is done in XML strict and XHTML strict mode, as close as IE7 can get to that.</p>
<p>Take the following example:</p>
<p><pre class="javascript">&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Sample&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;a href=<span style="color: #3366CC;">&quot;&quot;</span> onclick=<span style="color: #3366CC;">&quot;alert(document.body.nodeName); return false;&quot;</span>&gt;
            Click me
        &lt;/a&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre></p>
<p>While in Firefox the nodeName attribute value reported is &#8220;body&#8221;, Internet Explorer 7 reports &#8220;BODY&#8221;. This causes particular problems when comparing nodeName in order to detect a particular node. While it is more common to use .getElementsByTagName, we often need to compare tags one-by-one to avoid checking indirect children. Take the following example:</p>
<p><pre class="javascript">&lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;Sample&lt;/title&gt;
    &lt;script type=<span style="color: #3366CC;">&quot;text/javascript&quot;</span>&gt;
        <span style="color: #003366; font-weight: bold;">function</span> UpdateParagraph<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> k = document.<span style="color: #006600;">body</span>.<span style="color: #006600;">childNodes</span>;
            <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #66cc66;">&#40;</span> i <span style="color: #000066; font-weight: bold;">in</span> k <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span> k<span style="color: #66cc66;">&#91;</span> i <span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">nodeType</span> == <span style="color: #CC0000;">1</span>
                    &amp;&amp; k<span style="color: #66cc66;">&#91;</span> i <span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">nodeName</span> == <span style="color: #3366CC;">'p'</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                    k<span style="color: #66cc66;">&#91;</span> i <span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span> <span style="color: #CC0000;">0</span> <span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">nodeValue</span> = <span style="color: #3366CC;">'Altered Paragraph!'</span>;
                <span style="color: #66cc66;">&#125;</span>
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
    &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;p&gt;Some paragraph&lt;/p&gt;
        &lt;a href=<span style="color: #3366CC;">&quot;&quot;</span> onclick=<span style="color: #3366CC;">&quot;UpdateParagraph(); return false;&quot;</span>&gt;Click me&lt;/a&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre></p>
<p>In this case, Internet Explorer&#8217;s approach seems utterly problematic, as it will fail to recognize the tag. This can be easily fixed by converting the reported tag name to lower case:</p>
<p><pre class="javascript"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span> k<span style="color: #66cc66;">&#91;</span> i <span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">nodeType</span> == <span style="color: #CC0000;">1</span>
    &amp;&amp; k<span style="color: #66cc66;">&#91;</span> i <span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">nodeName</span>.<span style="color: #006600;">toLowerCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #3366CC;">'p'</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    k<span style="color: #66cc66;">&#91;</span> i <span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">childNodes</span><span style="color: #66cc66;">&#91;</span> <span style="color: #CC0000;">0</span> <span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">nodeValue</span> = <span style="color: #3366CC;">'Altered Paragraph!'</span>;
<span style="color: #66cc66;">&#125;</span></pre></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kamibu.com/2007/05/27/handling-nodename-in-different-browsers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
