<?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>Dmytro Shteflyuk&#039;s Home &#187; JavaScript</title>
	<atom:link href="http://kpumuk.info/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://kpumuk.info</link>
	<description>In my blog I&#039;ll try to describe about interesting technologies, my discovery in IT and some useful things about programming.</description>
	<lastBuildDate>Tue, 13 Jul 2010 11:18:45 +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>Do I need to buy additional memory?</title>
		<link>http://kpumuk.info/javascript/do-i-need-to-buy-additional-memory/</link>
		<comments>http://kpumuk.info/javascript/do-i-need-to-buy-additional-memory/#comments</comments>
		<pubDate>Tue, 29 May 2007 14:07:41 +0000</pubDate>
		<dc:creator>Dmytro Shteflyuk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[humor]]></category>
		<category><![CDATA[My Life]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://kpumuk.info/life/do-i-need-to-buy-additional-memory/</guid>
		<description><![CDATA[Today I spent almost full day for reflection on to buy or not additional memory for my computer. As a result new script was born, and he saved my brain: If you have questions like this &#8212; ask the script, maybe it would help you too :-)]]></description>
			<content:encoded><![CDATA[<p>Today I spent almost full day for reflection on to buy or not additional memory for my computer. As a result new script was born, and he saved my brain:</p>
<p align="center"><img src='http://kpumuk.info/wp-content/uploads/2007/05/additional_memory.png' alt='Do I need to buy additional memory?' /></p>
<p>If you have questions like this &#8212; <a href="http://kpumuk.info/projects/examples/yes-or-no/">ask the script</a>, maybe it would help you too :-)</p>
]]></content:encoded>
			<wfw:commentRss>http://kpumuk.info/javascript/do-i-need-to-buy-additional-memory/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>JavaScript optimization Part 3: Attaching events</title>
		<link>http://kpumuk.info/javascript/javascript-optimization-part-3-attaching-events/</link>
		<comments>http://kpumuk.info/javascript/javascript-optimization-part-3-attaching-events/#comments</comments>
		<pubDate>Wed, 09 May 2007 23:41:55 +0000</pubDate>
		<dc:creator>Dmytro Shteflyuk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[browsers]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://kpumuk.info/javascript/javascript-optimization-part-3-attaching-events/</guid>
		<description><![CDATA[This is a third part of the JavaScript optimization tutorial, and today I&#8217;m going to talk about events. Sorry for a long delay between posts, I hope remaining parts would not be delayed so much. Scenario: you have some elements and you need to add some actions to them (for example, when user moves mouse [...]]]></description>
			<content:encoded><![CDATA[<p>This is a third part of the JavaScript optimization tutorial, and today I&#8217;m going to talk about events. Sorry for a long delay between posts, I hope remaining parts would not be delayed so much.</p>
<p><strong>Scenario</strong>: you have some elements and you need to add some actions to them (for example, when user moves mouse cursor over element, or clicks on elements).</p>
<p class="more"><span id="more-128"></span></p>
<p>This is pretty usual task in web-development. And the first thing that everybody knows is a different event attaching syntax in Internet Explorer and Firefox: first uses <tt>element.attachEvent</tt>, second &#8212; <tt>element.addEventListeners</tt>. Therefor you need to add code, which would detect what browser is being used and what method to use in each case. In the most popular client script library <a href="http://www.prototypejs.org/">Prototype</a> it is already standardized and you can always use <tt>Event.observe</tt>. Let&#8217;s benchmark a little.</p>
<p>I&#8217;m going to start attaching events from manual detection of which browser is being used:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// Attaching events</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> items.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">--;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; items<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> e_onclick<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; items<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'onclick'</span><span style="color: #339933;">,</span> e_onclick<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #006600; font-style: italic;">// Detaching events</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> items.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">--;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">removeEventListener</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; items<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">removeEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> e_onclick<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">detachEvent</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; items<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">detachEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'onclick'</span><span style="color: #339933;">,</span> e_onclick<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>This approach shown best times: <em>188</em> and <em>203 ms</em> in Internet Explorer 6 and 7, <em>125</em> and <em>141 ms</em> in Firefox 1.5 and 2.0, and <em>63 ms</em> in Opera 9, but there is one problem with it &#8212; memory leaks: Internet Explorer usually forget to clean up memory, used by event listeners, when you navigating to another page. Therefor all JavaScript frameworks which implement event attaching/detaching functions, provide functionality for removing listeners when you navigating to another page. Let&#8217;s test them.</p>
<p>Code for testing Prototype library:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// Attaching events</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> items.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">--;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Event.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> e_onclick<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #006600; font-style: italic;">// Detaching events</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> items.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">--;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Event.<span style="color: #660066;">stopObserving</span><span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> e_onclick<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>This is very slow, <em>6453 ms</em> in Internet Explorer 6 and looks like some memory leaks are still exists (it became slower and slower with the lapse of time) and <em>365 &#8212; 653 ms</em> in other browsers.</p>
<p>There are tons of problems and solutions with adding events (just look at the <a href="http://www.quirksmode.org/js/events_advanced.html">Advanced event registration models</a>). Some time ago even <a href="http://www.quirksmode.org/blog/archives/2005/09/addevent_recodi.html">PPK&#8217;s addEvent() Recoding Contest</a> was over (but do not use the <a href="http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html">winner&#8217;s solution</a> because of it&#8217;s inefficiency and memory leaks).</p>
<p>Instead of winner&#8217;s solution, I decided to test incredible and impertinent solution from <a href="http://dean.edwards.name/weblog/2005/10/add-event/">Dean Edwards</a>, which even does not use the <tt>addeventListener</tt>/<tt>attachEvent</tt> methods, but it is entirely cross-browser!</p>
<p>Used code:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// Attaching events</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> items.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">--;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; addEvent<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> e_onclick<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #006600; font-style: italic;">// Detaching events</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> items.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">--;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; removeEvent<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> e_onclick<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Results are as much impressive as when we used manual events attaching, and this approach is extremely fast to use it in real-world applications. Of course, you would say: &#8220;Stop, what you are talking about? I don&#8217;t want to use additional methods while Prototype is already used in my code!&#8221; Calm down, you don&#8217;t need to do it, just download <a href="http://www.danwebb.net/2006/9/3/low-pro-unobtrusive-scripting-for-prototype">Low Pro</a> library (current version is <a href="http://www.danwebb.net/2007/4/16/low-pro-0-4-released">0.4</a>, stay tuned). Ruby on Rails developers could use <a href="http://www.ujs4rails.com/">UJS Rails Plugin</a> in their applications to improve performance &#8212; it uses Low Pro inside.</p>
<table class="bordered" cellspacing="0" cellpadding="2" width="100%">
<tr>
<th>No</th>
<th>Method</th>
<th>IE 6</th>
<th>IE 7</th>
<th>FF 1.5</th>
<th>FF 2.0</th>
<th>Opera 9</th>
</tr>
<tr>
<td>1</td>
<td><tt>manually<tt></td>
<td align="right">203</td>
<td align="right">188</td>
<td align="right">125</td>
<td align="right">141</td>
<td align="right">63</td>
</tr>
<tr>
<td>2</td>
<td><tt>prototype.js<tt></td>
<td align="right">6453</td>
<td align="right">653</td>
<td align="right">547</td>
<td align="right">469</td>
<td align="right">365</td>
</tr>
<tr>
<td>3</td>
<td><tt>addEvent<tt></td>
<td align="right">783</td>
<td align="right">344</td>
<td align="right">94</td>
<td align="right">141</td>
<td align="right">62</td>
</tr>
</table>
<p><a href="http://kpumuk.info/wp-content/uploads/2007/05/attaching_events.png" title="Benchmark: Attaching events"><img alt="Benchmark: Attaching events" src="http://kpumuk.info/wp-content/uploads/2007/05/attaching_events.thumbnail.png" /></a></p>
<p>You can review benchmark and get your own results <a href="http://kpumuk.info/projects/examples/javascript-optimization-part-3-attaching-events/">here</a>.</p>
<h2>Conclusions</h2>
<ul>
<li>Always use Low Pro along with Prototype.js (of course, if your application uses rich client logic).</li>
<li>Use manual events attaching when you need to get as much as possible speed from your application (for example, when you handling hundreds of elements). But don't forget about memory leaks!</li>
<li>Avoid events attaching whenever it's possible (for example, use <tt>:hover</tt> CSS selector instead of <tt>onmouseover</tt> event).</li>
<li>Keep your eyes opened, maybe tomorrow somebody would post another great performance optimization tip.</li>
</ul>
<h2>Links to other parts</h2>
<ul>
<li><a href="http://kpumuk.info/javascript/javascript-optimization-part-1-adding-dom-elements-to-document/">Part 1: Adding DOM elements to document</a></li>
<li><a href="http://kpumuk.info/javascript/javascript-optimization-part-2-applying-styles-to-elements/">Part 2: Applying styles to elements</a></li>
<li><strong>Part 3: Attaching events</strong></li>
<li>Part 4: Multiple anonymous functions (will be published soon)</li>
<li>Part 5: Attaching events on demand (will be published soon)</li>
<li>Part 6: Element hide and show (will be published soon)</li>
<li>Part 7: Elements collection enumeration (will be published soon)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://kpumuk.info/javascript/javascript-optimization-part-3-attaching-events/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>JavaScript optimization Part 2: Applying styles to elements</title>
		<link>http://kpumuk.info/javascript/javascript-optimization-part-2-applying-styles-to-elements/</link>
		<comments>http://kpumuk.info/javascript/javascript-optimization-part-2-applying-styles-to-elements/#comments</comments>
		<pubDate>Tue, 27 Mar 2007 20:02:41 +0000</pubDate>
		<dc:creator>Dmytro Shteflyuk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[browsers]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[style]]></category>

		<guid isPermaLink="false">http://kpumuk.info/javascript/javascript-optimization-part-2-applying-styles-to-elements/</guid>
		<description><![CDATA[This is second part of articles cycle devoted to JavaScript optimization. In this post I&#8217;ll cover dynamic elements styling and explore a little HTML-rendering process. Also you will find here some tricks on how to make your applications faster. Scenario: you have elements in your document and you need to change their color, background, or [...]]]></description>
			<content:encoded><![CDATA[<p>This is second part of articles cycle devoted to JavaScript optimization. In this post I&#8217;ll cover dynamic elements styling and explore a little HTML-rendering process. Also you will find here some tricks on how to make your applications faster.</p>
<p class="more"><span id="more-113"></span></p>
<p><strong>Scenario</strong>: you have elements in your document and you need to change their color, background, or something else related to theirs style. For example, highlight table rows on mouse over or mark them when corresponding checkbox is checked.</p>
<p>And again I know two ways: using styles or set foreground(or background) color directly from JavaScript. Let&#8217;s test them before discussion:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> items <span style="color: #339933;">=</span> el.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; items<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'selected'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>The average result is <em>187 &#8211; 291 ms</em>, for the InternetExplorer 6 it is <em>512 ms</em>, and in Opera 9 it is <em>47 ms</em>.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> items <span style="color: #339933;">=</span> el.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; items<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#007f00'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; items<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">color</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#ff0000'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>I got results starting from <em>282 ms</em> in Opera and ending with <em>1709 ms</em> in Internet Explorer 6.</p>
<p>Result are clean and easy to understand:</p>
<table class="bordered" cellspacing="0" cellpadding="2" width="100%">
<tr>
<th>No</th>
<th>Method</th>
<th>IE 6</th>
<th>IE 7</th>
<th>FF 1.5</th>
<th>FF 2.0</th>
<th>Opera 9</th>
</tr>
<tr>
<td>1</td>
<td><tt>element.className<tt></td>
<td align="right">512</td>
<td align="right">187</td>
<td align="right">291</td>
<td align="right">203</td>
<td align="right">47</td>
</tr>
<tr>
<td>2</td>
<td><tt>element.style.color<tt></td>
<td align="right">1709</td>
<td align="right">422</td>
<td align="right">725</td>
<td align="right">547</td>
<td align="right">282</td>
</tr>
</table>
<p><a href="http://kpumuk.info/wp-content/uploads/2007/03/applying_styles.png" title="Benchmark: Applying styles to elements"><img alt="Benchmark: Applying styles to elements" src="http://kpumuk.info/wp-content/uploads/2007/03/applying_styles.thumbnail.png" /></a></p>
<p>You can view benchmark test and get your own results <a href="http://kpumuk.info/wp-content/uploads/2007/03/applying_styles.html" target="_blank">here</a>.</p>
<p>Looks like this is simplest optimization tip, but... there is one issue with Internet Explorer -- page update. Are you remember scenario described in the beginning of chapter, about <tt>onmouseover</tt>? When you change element's class name, your code is much faster, but the page would not be updated immediately. Look at this <a href="http://kpumuk.info/wp-content/uploads/2007/03/applying_styles_mouseover.html" target="_blank">example</a>. Try to click "Generate elements" when "element.className" radio button is selected. Then move your mouse pointer over the items, scroll list to the bottom, move mouse again (for slow machines items number would be less than default, for fast ones - greater). Have you noticed, that background changes not so fast as the mouse pointer moves? Now switch selected radio button to "element.style.color". Background changing smoothly, right?</p>
<p>At the bottom of page you see the number of <tt>onmouseover</tt> events triggered and average time spent to dispatch them. As you noticed, first radio button works two times faster! Why it looks slower? I think it is because when you changed <tt>className</tt> property Internet Explorer isn't actually updates UI, but placed event in update queue. If you have other ideas, please post comments.</p>
<p>Anybody, who read article up to this place, would probably say: "Hey, guy, you are cheating. Where is the <tt>:hover</tt>?". I'm hurry up to fix this shortcoming and describing this case right now. First, this thing is not working in the Internet Explorer 6 (and don't ask me "who need it?"). But this is not most difficult problem, performance of this issue is much worse. Even don't want to comment it, just select third radiobutton on previously given page and move your mouse in Internet Explorer 7 and Opera 9 (Firefox works great).</p>
<h2>Conclusions</h2>
<ul>
<li>Use <tt>className</tt> when it's possible. It grants you more flexibility and control over site look.</li>
<li>If you have many items in your container element, and you need really fast UI, set styles directly through element's <tt>style</tt> property.</li>
</ul>
<h2>Links to other parts</h2>
<ul>
<li><a href="http://kpumuk.info/javascript/javascript-optimization-part-1-adding-dom-elements-to-document/">Part 1: Adding DOM elements to document</a></li>
<li><strong>Part 2: Applying styles to elements</strong></li>
<li><a href="http://kpumuk.info/javascript/javascript-optimization-part-3-attaching-events/">Part 3: Attaching events</a></li>
<li>Part 4: Multiple anonymous functions (will be published soon)</li>
<li>Part 5: Attaching events on demand (will be published soon)</li>
<li>Part 6: Element hide and show (will be published soon)</li>
<li>Part 7: Elements collection enumeration (will be published soon)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://kpumuk.info/javascript/javascript-optimization-part-2-applying-styles-to-elements/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>JavaScript optimization Part 1: Adding DOM elements to document</title>
		<link>http://kpumuk.info/javascript/javascript-optimization-part-1-adding-dom-elements-to-document/</link>
		<comments>http://kpumuk.info/javascript/javascript-optimization-part-1-adding-dom-elements-to-document/#comments</comments>
		<pubDate>Sun, 25 Mar 2007 01:40:49 +0000</pubDate>
		<dc:creator>Dmytro Shteflyuk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[browsers]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://kpumuk.info/javascript/javascript-optimization-part-1-adding-dom-elements-to-document/</guid>
		<description><![CDATA[Most web-developers writing tons of JavaScript, especially in our Web 2.0 century. It&#8217;s powerful technology, but most browsers has very slow implementation of engine, and everyone at some instant decide to review code and make it faster. In this post I&#8217;ll share my experience and explain several tricks how to make your JavaScript as fast [...]]]></description>
			<content:encoded><![CDATA[<p>Most web-developers writing tons of JavaScript, especially in our Web 2.0 century. It&#8217;s powerful technology, but most browsers has very slow implementation of engine, and everyone at some instant decide to review code and make it faster. In this post I&#8217;ll share my experience and explain several tricks how to make your JavaScript as fast as possible.</p>
<p>This is first article in 7 parts tutorial, stay tuned.</p>
<p class="more"><span id="more-70"></span></p>
<p><strong>Scenario</strong>: you&#8217;re developing rich Internet application and you need to load dynamic elements using AJAX and then add them to current document. For some reason you don&#8217;t want (or can&#8217;t) use fully generated HTML, and decided to fetch items in JavaScript array.</p>
<p>I know two classic ways to do so: create elements using <tt>document.createElement()</tt> method and concatenate HTML string and assign it to <tt>parentElement.innerHTML</tt> property. Of course, you can combine both ways. Let&#8217;s examine this ways in details.</p>
<p>Classic way (and in ideal world the best way) is to use DOM for element manipulations:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> li <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; li.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">createTextNode</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Element '</span> <span style="color: #339933;">+</span> i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; el.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>li<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Not so bad performance. Internet Explorer 6 is slowest &#8211; <em>1403 ms</em> (but it is a slower browser in the world, right?), and other browser displayed results quickly (<em>63 &#8211; 328 ms</em>). Ok, but what about creating DOM element from HTML code?</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> li <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;li&gt;Element '</span> <span style="color: #339933;">+</span> i <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/li&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; el.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>li<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>It works better in Internet Explorer 6 (<em>1134 ms</em>), but does not work in other browsers at all. Weird! Of course, you can add <tt>try/catch</tt> block and create elements using first approach in <tt>catch</tt> block for the other browsers. But I have better solution.</p>
<p>Every DOM node has attribute <tt>innerHTML</tt> which holds all child nodes as HTML string.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">el.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; el.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'&lt;li&gt;Element '</span> <span style="color: #339933;">+</span> i <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Wow, I&#8217;m highly impressed how slow could be adding elements procedure (<em>11391 &#8211; 307938 ms</em>)! Cool result, right? It&#8217;s because browser tried to render list while we updating and it&#8217;s take so long time. Little optimization:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; html <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'&lt;li&gt;Element '</span> <span style="color: #339933;">+</span> i <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
el.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> html<span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>All browsers shows great perfomance (<em>31 &#8211; 109 ms</em>), but Internet Explorer is still slow &#8211; <em>10994 ms</em>. I found solution which works very fast in all browsers: to create array of HTML chunks, and the join them using empty string:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; html.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;li&gt;Element '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; html.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; html.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;/li&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
el.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> html.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>It&#8217;s fastest approach for Internet Explorer 6 &#8211; <em>400 ms</em>, and very fast in other browsers. Why I&#8217;m not saying fastest in case of Firefox? I added another test to make in cleaner:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; html <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'&lt;li style=&quot;padding-left: '</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">%</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <br />
&nbsp; &nbsp; <span style="color: #3366CC;">'&quot; id=&quot;item-'</span> <span style="color: #339933;">+</span> i <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;Element '</span> <span style="color: #339933;">+</span> i <span style="color: #339933;">+</span> <span style="color: #3366CC;">' Column '</span> <span style="color: #339933;">+</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">%</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
el.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> html<span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>And second example:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; html.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;li style=&quot;padding-left: '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; html.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">%</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; html.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&quot; id=&quot;item-'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; html.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; html.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&quot;&gt;Element '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; html.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; html.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' Column '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; html.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">%</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; html.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;/li&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
el.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> html.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Here are the results in table and diagram formats.</p>
<table class="bordered" cellspacing="0" cellpadding="2">
<tr>
<th>No</th>
<th>Method</th>
<th>IE&nbsp;6</th>
<th>IE&nbsp;7</th>
<th>FF&nbsp;1.5</th>
<th>FF&nbsp;2.0</th>
<th>Opera&nbsp;9</th>
</tr>
<tr>
<td>1</td>
<td><tt>createElement()<tt></td>
<td align="right">1403</td>
<td align="right">219</td>
<td align="right">166</td>
<td align="right">328</td>
<td align="right">63</td>
</tr>
<tr>
<td>2</td>
<td><tt>createElement() full<tt></td>
<td align="right">1134</td>
<td align="center">-</td>
<td align="center">-</td>
<td align="center">-</td>
<td align="center">-</td>
</tr>
<tr>
<td>3</td>
<td><tt>innerHTML<tt></td>
<td align="right">39757</td>
<td align="right">20781</td>
<td align="right">41058</td>
<td align="right">307938</td>
<td align="right">11391</td>
</tr>
<tr>
<td>4</td>
<td><tt>innerHTML optimized<tt></td>
<td align="right">10994</td>
<td align="right">46</td>
<td align="right">50</td>
<td align="right">109</td>
<td align="right">31</td>
</tr>
<tr>
<td>5</td>
<td><tt>innerHTML/join<tt></td>
<td align="right">400</td>
<td align="right">31</td>
<td align="right">47</td>
<td align="right">125</td>
<td align="right">31</td>
</tr>
<tr>
<td colspan="7">&nbsp;</td>
</tr>
<tr>
<td>6</td>
<td><tt>innerHTML/optimized+<tt></td>
<td align="right">28934</td>
<td align="right">109</td>
<td align="right">84</td>
<td align="right">172</td>
<td align="right">62</td>
</tr>
<tr>
<td>7</td>
<td><tt>innerHTML/join+<tt></td>
<td align="right">950</td>
<td align="right">78</td>
<td align="right">110</td>
<td align="right">189</td>
<td align="right">62</td>
</tr>
</table>
<p><a href="http://kpumuk.info/wp-content/uploads/2007/03/adding_elements.png" rel="lightbox" title="Benchmark: Adding DOM elements to document"><img src="http://kpumuk.info/wp-content/uploads/2007/03/adding_elements.thumbnail.png" alt="Benchmark: Adding DOM elements to document" /></a></p>
<p>You can view benchmark test and get your own results <a href="http://kpumuk.info/projects/examples/javascript-optimization-part-1-adding-dom-elements-to-document/">here</a>.</p>
<h2>Conclusions</h2>
<ul>
<li>Always use DOM node functions to keep your code standards-compliant. This approach has satisfactory performance and works in all browsers.</li>
<li>If you need extremely high performance, use <tt>join</tt>+<tt>innerHTML</tt> method, which has best time in benchmark.</li>
<li>Never use appending HTML strings to the <tt>innerHTML</tt> (yeah, if you need append one small element).</li>
<li>Opera is fastest browser in the world, but Internet Explorer 7 is fast too, Firefox 2.0 surprised me with his low performance.</li>
<li>Never believe fanatics like me and benchmark different approaches by yourself (but don't worry, Microsoft does not paid me for their browser advertisement).</li>
</ul>
<h2>Links to other parts</h2>
<ul>
<li><strong>Part 1: Adding DOM elements to document</strong></li>
<li><a href="http://kpumuk.info/javascript/javascript-optimization-part-2-applying-styles-to-elements/">Part 2: Applying styles to elements</a></li>
<li><a href="http://kpumuk.info/javascript/javascript-optimization-part-3-attaching-events/">Part 3: Attaching events</a></li>
<li>Part 4: Multiple anonymous functions (will be published soon)</li>
<li>Part 5: Attaching events on demand (will be published soon)</li>
<li>Part 6: Element hide and show (will be published soon)</li>
<li>Part 7: Elements collection enumeration (will be published soon)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://kpumuk.info/javascript/javascript-optimization-part-1-adding-dom-elements-to-document/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Extending moo.fx with custom effect (fx.Flash)</title>
		<link>http://kpumuk.info/javascript/extending-moo-fx-with-custom-effect-fx-flash/</link>
		<comments>http://kpumuk.info/javascript/extending-moo-fx-with-custom-effect-fx-flash/#comments</comments>
		<pubDate>Sat, 29 Apr 2006 08:54:43 +0000</pubDate>
		<dc:creator>Dmytro Shteflyuk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[effects]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[moo.fx]]></category>

		<guid isPermaLink="false">http://kpumuk.info/php/extending-moo-fx-with-custom-effect-fx-flash/</guid>
		<description><![CDATA[Several days ago my best friend Alexey Kovyrin asked me to help him to create flashing effect on HTML page. I gave him advice to create this effect using moo.fx library. I wanted to show how to extend it, but I couldn&#8217;t keep from creating this effect myself. moo.fx is a superlightweight, ultratiny, megasmall javascript [...]]]></description>
			<content:encoded><![CDATA[<p>Several days ago my best friend <a href="http://blog.kovyrin.net/" target="_blank">Alexey Kovyrin</a> asked me to help him to create flashing effect on HTML page. I gave him advice to create this effect using <a href="http://moofx.mad4milk.net/" target="_blank">moo.fx</a> library. I wanted to show how to extend it, but I couldn&#8217;t keep from creating this effect myself.</p>
<p><a href="http://moofx.mad4milk.net/" target="_blank">moo.fx</a> is a superlightweight, ultratiny, megasmall javascript effects library, written with prototype.js. It&#8217;s easily to extend it with custom effect and in this post I will show you how you can do this. In this article I&#8217;ll show how to create flashing effect: element&#8217;s background will smoothly change from one color to another and go back to first color (maybe several times).</p>
<p class="more"><span id="more-43"></span></p>
<p>To extend <tt>fx.Base</tt> you can use following construction:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">fx.<span style="color: #660066;">Flash</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">Class</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
Object.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>Object.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>fx.<span style="color: #660066;">Flash</span>.<span style="color: #660066;">prototype</span><span style="color: #339933;">,</span> fx.<span style="color: #660066;">Base</span>.<span style="color: #660066;">prototype</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> &nbsp; <br />
<span style="color: #006600; font-style: italic;">// ...</span></div></td></tr></tbody></table></div>
<p>Now we need to decide in which format initial colors will be passed. I think the most simple to user is to pass color values in <em>#RRGGBB</em> format, the most popular format in web-design. We need to parse this format to get separate color&#8217;s parts (reg, green and blue), therefor it&#8217;s necessary to create function that makes hexadecimal => decimal conversion. To assign background color to the element while flashing reverse function is required.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">hD<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;0123456789ABCDEF&quot;</span><span style="color: #339933;">,</span><br />
<br />
d2h<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>d<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> h <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hD</span>.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span>d <span style="color: #339933;">&amp;</span> <span style="color: #CC0000;">15</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>d <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">15</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> d <span style="color: #339933;">&gt;&gt;=</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span> h <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hD</span>.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span>d <span style="color: #339933;">&amp;</span> <span style="color: #CC0000;">15</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> h<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>h.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> h <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;0&quot;</span> <span style="color: #339933;">+</span> h<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> h<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <br />
h2d<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>h<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> parseInt<span style="color: #009900;">&#40;</span>h<span style="color: #339933;">,</span> <span style="color: #CC0000;">16</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Now we are ready to develop constructor (method <tt>initialize()</tt>):</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">initialize<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>el<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">el</span> <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> color_from <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>options <span style="color: #339933;">&amp;&amp;</span> options.<span style="color: #660066;">color_from</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #3366CC;">&quot;#ffffff&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> color_to <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>options <span style="color: #339933;">&amp;&amp;</span> options.<span style="color: #660066;">color_to</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #3366CC;">&quot;#ff0000&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> color_f <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">h2d</span><span style="color: #009900;">&#40;</span>color_from.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> color_t <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">h2d</span><span style="color: #009900;">&#40;</span>color_to.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> _options <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; red<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>color_f <span style="color: #339933;">&gt;&gt;</span> <span style="color: #CC0000;">16</span><span style="color: #339933;">,</span> color_t <span style="color: #339933;">&gt;&gt;</span> <span style="color: #CC0000;">16</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; green<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#40;</span>color_f <span style="color: #339933;">&gt;&gt;</span> <span style="color: #CC0000;">8</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> <span style="color: #CC0000;">255</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>color_t <span style="color: #339933;">&gt;&gt;</span> <span style="color: #CC0000;">8</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> <span style="color: #CC0000;">255</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; blue<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>color_f <span style="color: #339933;">&amp;</span> <span style="color: #CC0000;">255</span><span style="color: #339933;">,</span> color_t <span style="color: #339933;">&amp;</span> <span style="color: #CC0000;">255</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; count<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; Object.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>_options<span style="color: #339933;">,</span> options <span style="color: #339933;">||</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>_options.<span style="color: #660066;">onComplete</span><span style="color: #009900;">&#41;</span> _options.<span style="color: #660066;">flashOnComplete</span> <span style="color: #339933;">=</span> _options.<span style="color: #660066;">onComplete</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setOptions</span><span style="color: #009900;">&#40;</span>_options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>This function can accept following options:</p>
<ul>
<li><strong>duration</strong> &#8211; duration in milliseconds of changing color from first to second or vice versa. For example, if you pass <tt>count = 3</tt>, complete effect execution time will be 3 * 2 * duration (default is <em>500</em>).</li>
<li><strong>onComplete</strong> &#8211; a function that will get called upon effect completion.</li>
<li><strong>transition</strong> &#8211; transition type. (default is <em>fx.sinoidal</em>)</li>
<li><strong>color_from</strong> &#8211; beginning color (default is <em>#ffffff</em>)</li>
<li><strong>color_to</strong> &#8211; ending color (default is <em>#ff0000</em>)</li>
<li><strong>count</strong> &#8211; flashes count (default is <em>1</em>)</li>
</ul>
<p>Effect dependents on <tt>onComplete</tt> function therefor this parameters is saved to another options variable <tt>flashOnComplete</tt>.</p>
<p>Function <tt>increase()</tt> is called during effect. In this function we can calculate current color based on internal variable <tt>this.now</tt>:</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">increase<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> r <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">d2h</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">now</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">red</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">red</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">255</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">red</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> g <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">d2h</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">now</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">green</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">green</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">255</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">green</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">d2h</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">now</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">blue</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">blue</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">255</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">blue</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">el</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;#&quot;</span> <span style="color: #339933;">+</span> r <span style="color: #339933;">+</span> g <span style="color: #339933;">+</span> b<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>To call effect function <tt>toggle()</tt> used. <tt>OnComplete</tt> is used to repeat effect from ending to beginnig color. It also decrements current count value to make it possible to repeat effect <tt>count</tt> times.</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">toggle<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">flashCount</span> <span style="color: #339933;">==</span> undefined<span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">flashCount</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">count</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">onComplete</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">onComplete</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">custom</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">255</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
<br />
onComplete<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">flashCount</span><span style="color: #339933;">--;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">flashCount</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">flashCount</span> <span style="color: #339933;">=</span> undefined<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">onComplete</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">flashOnComplete</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">onComplete</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">toggle</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">custom</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Thats all. You can download source file <a href="http://kpumuk.info/samples/moo-fx/fx.flash.js" target="_blank">here</a>. Samples are shown below this.</p>
<p><script src="http://kpumuk.info/samples/moo-fx/moo.fx.js" type="text/javascript"></script><script src="http://kpumuk.info/samples/moo-fx/fx.flash.js" type="text/javascript"></script></p>
<div id="sample-area" style="border:1px dashed #4e5706;margin:10px 0px">This is sample text. Click the link below to view fx.Flash effect in action.</div>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> sample1 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> fx.<span style="color: #660066;">Flash</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;sample-area&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>color_from<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;#ffffe3&quot;</span><span style="color: #339933;">,</span> color_to<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;#007f00&quot;</span><span style="color: #339933;">,</span> count<span style="color: #339933;">:</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> transition<span style="color: #339933;">:</span>fx.<span style="color: #660066;">circ</span><span style="color: #339933;">,</span> duration<span style="color: #339933;">:</span><span style="color: #CC0000;">300</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p><a href="#" onclick="sample1.toggle();return false;">Show effect #1</a><br/></p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> sample2 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> fx.<span style="color: #660066;">Flash</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;sample-area&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>color_from<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;#ffffe3&quot;</span><span style="color: #339933;">,</span> color_to<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;#7f0000&quot;</span><span style="color: #339933;">,</span> count<span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> duration<span style="color: #339933;">:</span><span style="color: #CC0000;">600</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p><a href="#" onclick="sample2.toggle();return false;">Show effect #2</a><br/></p>
<pre><script type="text/javascript"><!--
var sample1 = new fx.Flash("sample-area", {color_from:"#ffffe3",color_to:"#007f00",count:3,transition:fx.circ,duration:300});
var sample2 = new fx.Flash("sample-area", {color_from:"#ffffe3",color_to:"#7f0000",count:1,duration:600});
//--></script></pre></p>
]]></content:encoded>
			<wfw:commentRss>http://kpumuk.info/javascript/extending-moo-fx-with-custom-effect-fx-flash/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>U.S. Grants Patent For AJAX</title>
		<link>http://kpumuk.info/links/us-grants-patent-for-ajax/</link>
		<comments>http://kpumuk.info/links/us-grants-patent-for-ajax/#comments</comments>
		<pubDate>Thu, 23 Feb 2006 10:36:24 +0000</pubDate>
		<dc:creator>Dmytro Shteflyuk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[news]]></category>

		<guid isPermaLink="false">http://kpumuk.info/internet/us-grants-patent-for-ajax/</guid>
		<description><![CDATA[The patent covers all rich-media technology implementations, including Flash, Flex, Java, Ajax, and XAML, when the rich-media application is accessed on any device over the Internet, according to the patent holders (Source). As reported by CNET: Internet design company Balthaser announced Tuesday that the U.S. Patent and Trademark Office has granted it a patent for [...]]]></description>
			<content:encoded><![CDATA[<p>The patent covers all rich-media technology implementations, including Flash, Flex, Java, Ajax, and XAML, when the rich-media application is accessed on any device over the Internet, according to the patent holders (<a href="http://www.aviransplace.com/index.php/archives/2006/02/22/us-grants-patent-for-ajax/">Source</a>).</p>
<p>As reported by <a href="http://news.com.com/Company+claims+patent+win+in+online+rich+media/2100-1030_3-6042085.html?tag=nefd.top" target="_blank">CNET</a>:</p>
<blockquote><div>
Internet design company Balthaser announced Tuesday that the U.S. Patent and Trademark Office has granted it a patent for the design and creation of rich-media services over the Internet.</p>
<p>Balthaser apparently intends to license the patent to companies that deliver rich-media services over the Web using technologies such as Adobe Systems’ Flash, AJAX and Java.
</p></div>
</blockquote>
<p>There is interesting fact that the company filed the patent in 2001.</p>
]]></content:encoded>
			<wfw:commentRss>http://kpumuk.info/links/us-grants-patent-for-ajax/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>AJAX-enabled Smarty plugins Part 2: ajax_form</title>
		<link>http://kpumuk.info/php/ajax-enabled-smarty-plugins-part-2-ajax_form/</link>
		<comments>http://kpumuk.info/php/ajax-enabled-smarty-plugins-part-2-ajax_form/#comments</comments>
		<pubDate>Tue, 21 Feb 2006 07:47:28 +0000</pubDate>
		<dc:creator>Dmytro Shteflyuk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[smarty]]></category>

		<guid isPermaLink="false">http://kpumuk.info/ajax/ajax-enabled-smarty-plugins-part-2-ajax_formajax-%d0%bf%d0%bb%d0%b0%d0%b3%d0%b8%d0%bd%d1%8b-%d0%b4%d0%bb%d1%8f-smarty-%d1%87%d0%b0%d1%81%d1%82%d1%8c-2-ajax_form/</guid>
		<description><![CDATA[In my previous post I&#8217;ve described several simple AJAX plugins. Now I&#8217;ll show how to use one of them &#8212; ajax_form &#8212; in real applications. I think this is the most powerful plugin therefor I&#8217;ll dwell on it more elaborately. Most of Web-forms has variuos validation mechanisms. Simplest way is to process form on the [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://kpumuk.info/ajax/ajax-enabled-smarty-plugins/lang/en/">previous post</a> I&#8217;ve described several simple AJAX plugins. Now I&#8217;ll show how to use one of them &#8212; <tt>ajax_form</tt> &#8212; in real applications. I think this is the most powerful plugin therefor I&#8217;ll dwell on it more elaborately.</p>
<p class="more"><span id="more-25"></span></p>
<p>Most of Web-forms has variuos validation mechanisms. Simplest way is to process form on the server and parse result on client, where return data can be in following format:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">true;User was registered successfully;http://example.com/login/<br />
false;Please enter your name;Entered passwords doesn't match</div></td></tr></tbody></table></div>
<p>Result string can be splitted on client and results can be shown in some part of page. Process function may looks like following (it&#8217;s part of smarty_ajax):</p>
<div class="codecolorer-container javascript twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> SmartyAjax <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; onSubmit<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>originalRequest<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> results <span style="color: #339933;">=</span> originalRequest.<span style="color: #660066;">responseText</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>results<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;true&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; SmartyAjax.<span style="color: #660066;">Messages</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span>results<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SmartyAjax.<span style="color: #660066;">Messages</span>.<span style="color: #660066;">MT_WARNING</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; SmartyAjax.<span style="color: #660066;">Messages</span>.<span style="color: #660066;">clear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; SmartyAjax.<span style="color: #660066;">Messages</span>.<span style="color: #660066;">setType</span><span style="color: #009900;">&#40;</span>SmartyAjax.<span style="color: #660066;">Messages</span>.<span style="color: #660066;">MT_ERROR</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> results.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SmartyAjax.<span style="color: #660066;">Messages</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>results<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>This function uses several functions from <tt>SmartyAjax.Messages</tt> object which depends on following HTML elements:</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;messages&quot;</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;messages-title&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;messages-list&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></div></td></tr></tbody></table></div>
<p>To manage messages I&#8217;ve created Smarty template <tt>parts/warnings.tpl</tt>. You can use it in your PHP file in simple way:</p>
<div class="codecolorer-container php twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// is messages contains warning (or error)</span><br />
<span style="color: #000088;">$t</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'messages_warning'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #666666; font-style: italic;">// array of messages</span><br />
<span style="color: #000088;">$t</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'messages'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Please provide your account information'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Another thing we need to examine is message which informs user about processing AJAX request. There is SmartyAjax.Process object in the sample which has two methods: hide() and show() which can be used to hide and display message. They are depends on HTML-element with id=&#8221;ajax-process&#8221;. It&#8217;s necessary to add &#8220;position: absolute&#8221; style because of this messages will be shown in top-right corner of screen independing on scroll position.</p>
<p><tt>ajax_form</tt> is simple to use:</p>
<div class="codecolorer-container smarty twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="smarty codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #D36900;">&#123;</span>ajax_form method<span style="color: #D36900;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> id<span style="color: #D36900;">=</span><span style="color: #ff0000;">&quot;form_register&quot;</span><span style="color: #D36900;">&#125;</span><br />
Any form-element can be placed here<br />
<span style="color: #D36900;">&#123;</span><span style="color: #D36900;">/</span>ajax_form<span style="color: #D36900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Possible parameters:</p>
<ul>
<li><strong>url</strong> &#8211; URL for AJAX-query (when no URL was specified current one will be used)</li>
<li><strong>method</strong> &#8211; query method (by default <i>get</i>, can be <i>get</i> or <i>post</i>)</li>
<li><strong>params</strong> &#8211; URL-encoded parameters</li>
<li><strong>id</strong> &#8211; form ID</li>
<li><strong>callback</strong> &#8211; JavaScript function which will be called when query will be completed</li>
</ul>
<p>Example can be found <a href="http://kpumuk.info/samples/smarty_ajax/register.php" target="_blank">here</a>, and full sources can be downloaded <a href="http://kpumuk.info/samples/smarty_ajax/smarty_ajax.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://kpumuk.info/php/ajax-enabled-smarty-plugins-part-2-ajax_form/feed/</wfw:commentRss>
		<slash:comments>52</slash:comments>
		</item>
		<item>
		<title>Prototype Dissected</title>
		<link>http://kpumuk.info/javascript/prototype-dissected/</link>
		<comments>http://kpumuk.info/javascript/prototype-dissected/#comments</comments>
		<pubDate>Mon, 20 Feb 2006 11:42:31 +0000</pubDate>
		<dc:creator>Dmytro Shteflyuk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[library]]></category>

		<guid isPermaLink="false">http://kpumuk.info/development/prototype-dissected/</guid>
		<description><![CDATA[Jonathan have dissected Prototype in a very nice graphics, this will give you a better view of the code and how it works. In getting to know Prototype a little better, I decided to go through the latest version of the Prototype library (1.5.0_pre0) and detail every method and property that was available. In doing [...]]]></description>
			<content:encoded><![CDATA[<p>Jonathan have <a href="http://www.snook.ca/archives/000531.php">dissected Prototype</a> in a very nice graphics, this will give you a better view of the code and how it works.</p>
<blockquote><div>
In getting to know Prototype a little better, I decided to go through the latest version of the Prototype library (1.5.0_pre0) and detail every method and property that was available. In doing so, I got a much better understanding of how the code works (and how to work with library).
</div>
</blockquote>
<p><a href="http://www.snook.ca/archives/prototype1280.png"><img src="http://kpumuk.info/wp-content/uploads/2006/02/prototype1280-thumb.png" alt="Prototype Dissected" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://kpumuk.info/javascript/prototype-dissected/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>AJAX-enabled Smarty plugins</title>
		<link>http://kpumuk.info/php/ajax-enabled-smarty-plugins/</link>
		<comments>http://kpumuk.info/php/ajax-enabled-smarty-plugins/#comments</comments>
		<pubDate>Sat, 18 Feb 2006 23:49:25 +0000</pubDate>
		<dc:creator>Dmytro Shteflyuk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[smarty]]></category>

		<guid isPermaLink="false">http://kpumuk.info/ajax/ajax-enabled-smarty-pluginsajax-%d0%bf%d0%bb%d0%b0%d0%b3%d0%b8%d0%bd%d1%8b-%d0%b4%d0%bb%d1%8f-smarty/</guid>
		<description><![CDATA[Today I&#8217;ve created simple AJAX-enabled plugins for Smarty. I don&#8217;t try to develop powerful reach-applications framework. I can give you only idea how to integrate AJAX-technology into Smarty. But if you have any offers how to improve anything I&#8217;ve described or if you just want to leave feedback please post you comments on my site. [...]]]></description>
			<content:encoded><![CDATA[<p>Today I&#8217;ve created simple AJAX-enabled plugins for <a href="http://smarty.php.net/" target="_blank">Smarty</a>. I don&#8217;t try to develop powerful reach-applications framework. I can give you only idea how to integrate AJAX-technology into Smarty. But if you have any offers how to improve anything I&#8217;ve described or if you just want to leave feedback please post you comments on my site.</p>
<p class="more"><span id="more-20"></span></p>
<p>In my practice I need several things from AJAX: to update some nodes in DOM, to send forms without post-back, to retrieve some values or to perform server-side calculation (maybe using database or other server-side resources). It&#8217;s necessary to write tons of JavaScript to implement my requirements in spite of using cool JavaScript library <a href="http://prototype.conio.net/" target="_blank">Prototype</a>.
</p>
<p>I decided to integrate Smarty with AJAX. There are three Smarty plugins has been created: <tt>ajax_update</tt>, <tt>ajax_call</tt>, <tt>ajax_form</tt>. Below all this plugins will be described.</p>
<h2>ajax_update</h2>
<p>This Smarty function can be used for update some parts of web-page.</p>
<p>Sample usage:</p>
<div class="codecolorer-container smarty twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="smarty codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009000;">&lt;a href=&quot;#&quot; onclick=&quot;{ajax_update update_id='intro_content'</span><br />
<span style="color: #009000;"> &nbsp;function='update_intro' params='page=about'}&quot;&gt;</span>About<span style="color: #009000;">&lt;/a&gt;</span></div></td></tr></tbody></table></div>
<p>Possible parameters:</p>
<ul>
<li><strong>url</strong> &#8211; URL for AJAX-query (when no URL was specified current one will be used)</li>
<li><strong>method</strong> &#8211; query method (by default <i>get</i>, can be <i>get</i> or <i>post</i>)</li>
<li><strong>update_id</strong> &#8211; ID of HTML element for update</li>
<li><strong>function</strong> &#8211; function which will be called</li>
<li><strong>params</strong> &#8211; URL-encoded parameters</li>
</ul>
<h2>ajax_call</h2>
<p>This Smarty function can be used for call PHP function on server side and retrieve its output.</p>
<p>Sample usage:</p>
<div class="codecolorer-container smarty twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="smarty codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009000;">&lt;a href=&quot;#&quot; onclick=&quot;{ajax_call function='calculate'</span><br />
<span style="color: #009000;"> &nbsp;params_func='calc_params' callback='calc_cb'}&quot;&gt;</span>Calculate<span style="color: #009000;">&lt;/a&gt;</span></div></td></tr></tbody></table></div>
<p>Possible parameters:</p>
<ul>
<li><strong>url</strong> &#8211; URL for AJAX-query (when no URL was specified current one will be used)</li>
<li><strong>method</strong> &#8211; query method (by default <i>get</i>, can be <i>get</i> or <i>post</i>)</li>
<li><strong>function</strong> &#8211; function which will be called</li>
<li><strong>params</strong> &#8211; URL-encoded parameters</li>
<li><strong>callback</strong> &#8211; JavaScript function which will be called when query will be completed</li>
<li><strong>params_func</strong> &#8211; JavaScript function used when you need custom parameters calculated on client side</li>
</ul>
<h2>ajax_form</h2>
<p>This Smarty block can be used for submit Web-forms without post-back.</p>
<p>Sample usage:</p>
<div class="codecolorer-container smarty twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="smarty codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #D36900;">&#123;</span>ajax_form method<span style="color: #D36900;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> id<span style="color: #D36900;">=</span><span style="color: #ff0000;">&quot;form_register&quot;</span><span style="color: #D36900;">&#125;</span><br />
Any form-element can be placed here<br />
<span style="color: #D36900;">&#123;</span><span style="color: #D36900;">/</span>ajax_form<span style="color: #D36900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Possible parameters:</p>
<ul>
<li><strong>url</strong> &#8211; URL for AJAX-query (when no URL was specified current one will be used)</li>
<li><strong>method</strong> &#8211; query method (by default <i>get</i>, can be <i>get</i> or <i>post</i>)</li>
<li><strong>params</strong> &#8211; URL-encoded parameters</li>
<li><strong>id</strong> &#8211; form ID</li>
<li><strong>callback</strong> &#8211; JavaScript function which will be called when query will be completed</li>
</ul>
<h2>Samples</h2>
<p>These plugins are quite simple and I think everyone can create even better than mine. I&#8217;ve just wanted to show you idea how integration can be done. Working examples can be found <a href="http://kpumuk.info/samples/smarty_ajax/" target="_blank">here</a>. Also you can <a href="http://kpumuk.info/samples/smarty_ajax/smarty_ajax.zip">download</a> full sources.</p>
]]></content:encoded>
			<wfw:commentRss>http://kpumuk.info/php/ajax-enabled-smarty-plugins/feed/</wfw:commentRss>
		<slash:comments>71</slash:comments>
		</item>
		<item>
		<title>AjaxLine &#8212; the best resource about AJAX technology</title>
		<link>http://kpumuk.info/javascript/ajaxline/</link>
		<comments>http://kpumuk.info/javascript/ajaxline/#comments</comments>
		<pubDate>Thu, 16 Feb 2006 14:07:33 +0000</pubDate>
		<dc:creator>Dmytro Shteflyuk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[friends]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[news]]></category>
		<category><![CDATA[web-2.0]]></category>

		<guid isPermaLink="false">http://kpumuk.info/internet/ajaxline-the-best-resource-about-ajaxajaxline-%d0%bb%d1%83%d1%87%d1%88%d0%b8%d0%b9-%d1%81%d0%b0%d0%b9%d1%82-%d0%be%d0%b1-ajax/</guid>
		<description><![CDATA[Today I found an interesting resource about AJAX &#8211; AjaxLine. There are many news, articles and other usefull information about this cool technology can be found on this site. It&#8217;s posed as specialized community and it has a good chance to become the most popular AJAX-developers area in the Internet.]]></description>
			<content:encoded><![CDATA[<p>Today I found an interesting resource about AJAX &#8211; <a href="http://www.ajaxline.com/" target="_blank">AjaxLine</a>. There are many news, articles and other usefull information about this cool technology can be found on this site. It&#8217;s posed as specialized community and it has a good chance to become the most popular AJAX-developers area in the Internet.</p>
]]></content:encoded>
			<wfw:commentRss>http://kpumuk.info/javascript/ajaxline/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
