<?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>Javascript Kata &#187; dhtml</title>
	<atom:link href="http://www.javascriptkata.com/category/dhtml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javascriptkata.com</link>
	<description>Advanced katas for javascripters</description>
	<lastBuildDate>Tue, 15 Jun 2010 14:21:03 +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>3 reasons why I use jQuery</title>
		<link>http://www.javascriptkata.com/2007/05/29/3-reasons-why-i-use-jquery/</link>
		<comments>http://www.javascriptkata.com/2007/05/29/3-reasons-why-i-use-jquery/#comments</comments>
		<pubDate>Tue, 29 May 2007 14:13:15 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[dhtml]]></category>
		<category><![CDATA[white belt]]></category>

		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=43</guid>
		<description><![CDATA[These days, you can&#8217;t write a web application without using one of the billionth javascript library. Two of them stood out of the crowd : prototype and jQuery. I will explain why did I choose jQuery. Prototype Prototype is comfortably installed in a lot of developers mind because of two reasons : it was the [...]]]></description>
			<content:encoded><![CDATA[<p>These days, you can&#8217;t write a web application without using one of the billionth <a href="http://www.google.com/search?q=javascript+library" target="_blank" title="javascript library">javascript library</a>. Two of them stood out of the crowd : <a href="http://www.prototypejs.org/" target="_blank" title="prototype">prototype</a> and <a href="http://docs.jquery.com/Main_Page" target="_blank" title="jQuery">jQuery</a>. I will explain why did I choose jQuery.</p>
<h3>   Prototype</h3>
<p>Prototype is comfortably installed in a lot of developers mind because of two reasons : it was the first widely used and <a href="http://script.aculo.us/" target="_blank" title="script.aculo.us">script.aculo.us</a>. Being the first accepted library is a hard thing to do and it will always help them. Scriptaculous (I hate to write it with the dots) had an incredible effect on the popularity of prototype. For the first time, we could do &#8220;flash animation&#8221; without using flash. This was instant popularity.</p>
<h3>   jQuery</h3>
<p>jQuery is one of the bastard child of prototype. It reuses the <span style="font-style: italic">$() function</span> but brings it to a level you never could have hope for. In fact, jQuery is almost completly based on the $ sign. Why? Maybe because it&#8217;s fast, maybe because it&#8217;s trendy. I don&#8217;t know. Example, you want to download a JSON from a server, <span style="font-style: italic">$.getJSON(&#8220;http://www.thesite.com/thejson&#8221;)</span>.</p>
<h3>Reason #1 : The selectors</h3>
<p>jQuery supports a lot of selectors. And when I say <span style="font-style: italic">a lot</span>, I really mean <span style="font-style: italic">a lot</span>. First, there&#8217;s <a href="http://docs.jquery.com/Selectors#CSS_Selectors" target="_blank" title="CSS selectors">CSS selectors</a>. You just have to apply what you know about CSS and you can write many complex selectors.</p>
<p>Second, there&#8217;s <a href="http://docs.jquery.com/Selectors#XPath_Selectors" target="_blank" title="XPath selectors">XPath selectors</a>. When I was young (sic!), I wrote a lot of XSL. And when I say <span style="font-style: italic">a lot</span>, I really mean <span style="font-style: italic">a lot</span>. XSL makes an intensive use of that XPath thingy. Because XHTML is basically XML, we could use XPath to easily navigate throught the DOM. The problem was that before jQuery, nothing could do that. Now, we can!</p>
<p>In comparison, prototype just has the <span style="font-style: italic">$(&#8216;theId&#8217;)</span> to select a element. Ouch!</p>
<h3>Reason #2 : The Attributes</h3>
<p>It&#8217;s easy to <a href="http://docs.jquery.com/DOM/Attributes" target="_blank" title="add/remove attributes">add/remove attributes</a> to any HTML element. I use it when I create new elements or when I need to add/remove a css class on an element. The interesting thing is that you can <a href="http://docs.jquery.com/How_jQuery_Works#Chainability_.28The_Magic_of_jQuery.29" title="chain" target="_blank">chain</a> them and it will work. Example, I want</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">$<span class="br0">&#40;</span><span class="st0">&quot;div&quot;</span><span class="br0">&#41;</span>.<span class="me1">attr</span><span class="br0">&#40;</span><span class="st0">&quot;title&quot;</span>, <span class="st0">&quot;This is a div&quot;</span><span class="br0">&#41;</span>.<span class="me1">addClass</span><span class="br0">&#40;</span><span class="st0">&quot;newClass&quot;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>You can add as many as you want&#8230;</p>
<h3>Reason #3 : Ajax and JSON</h3>
<p>Once again, <a href="http://docs.jquery.com/Ajax" target="_blank" title="ajax">ajax</a> is now easy as 1,2,3. I wrote an <a href="http://www.javascriptkata.com/2007/05/08/how-to-use-json/" target="_blank" title="article on JSON">article on JSON</a> and in my mind, there&#8217;s no real alternative to JSON. jQuery helps you with it.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">$.<span class="me1">getJSON</span><span class="br0">&#40;</span><span class="st0">&quot;http://www.thesite.com/thejson&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">function</span><span class="br0">&#40;</span>json<span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&quot;I received the json and put it in the json var : &quot;</span> + json.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>It&#8217;s a little <a href="http://www.prototypejs.org/learn/json" target="_blank" title="more complex with prototype">more complex with prototype</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2007/05/29/3-reasons-why-i-use-jquery/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Do not use the alert function in javascript</title>
		<link>http://www.javascriptkata.com/2007/05/16/do-not-use-the-alert-function-in-javascript/</link>
		<comments>http://www.javascriptkata.com/2007/05/16/do-not-use-the-alert-function-in-javascript/#comments</comments>
		<pubDate>Wed, 16 May 2007 13:30:14 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[dhtml]]></category>
		<category><![CDATA[do not]]></category>
		<category><![CDATA[white belt]]></category>

		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=40</guid>
		<description><![CDATA[Picture yourself on a beautiful beach on a warm day. The sounds of the waves is music to your hears. You&#8217;re on a comfortable chair with a laptop on your knees connected via an incredible WiFi. You&#8217;re using a brand new web2.0 application that must be the gratest web site on the web. Then, you [...]]]></description>
			<content:encoded><![CDATA[<p>Picture yourself on a beautiful beach on a warm day. The sounds of the waves is music to your hears. You&#8217;re on a comfortable chair with a laptop on your knees connected via an incredible WiFi. You&#8217;re using a brand new web2.0 application that must be the gratest web site on the web. Then, you see an infamous <span style="font-style: italic">alert</span> from javascript&#8230;<br />
<span style="font-weight: bold"><br />
</span></p>
<h3><span style="font-weight: bold"></span>Back to reality</h3>
<p>This alert box from javascript brought you back to reality. You&#8217;re in your shitty room, alone. The smell is disgusting. Everything is dirty. All because a developer has used the <span style="font-style: italic">alert</span> function to tell you something in an application. Don&#8217;t let this happen on your web application. <span style="font-weight: bold">Never, ever, ever use an alert box.</span></p>
<h3>History of the alert<span style="font-weight: bold"></span></h3>
<h3><span style="font-weight: bold"></span></h3>
<p>The alert was invented in the web1.0 era when it was the only way of showing a dynamic message to a user. That was the time when people loved win32 applications because they were so easy to use (compared to a web application). To have a little bit of win32 in their web world, they (some bad people) created a monster : the <span style="font-style: italic">alert</span> function that shows a win32 window.</p>
<p>It was it a big  mistake because lazy developers still use it because it is so easy. Don&#8217;t fall in the trap. <span style="font-weight: bold">Do not use the <span style="font-style: italic">alert</span> function.</span></p>
<h3>The problem with alert</h3>
<p>The user have to click on the message before he can do anything else on the page. <span style="font-weight: bold">Users hate to click</span>. Don&#8217;t make them click and show an unobtrusive message that the user doesn&#8217;t have to click to continue.<span style="font-weight: bold"></span></p>
<h3>What should I use instead of alert?</h3>
<p>Anything else. This includes (but is not limited to)</p>
<p><span style="font-weight: bold">Light boxes<br />
</span>I personnally don&#8217;t like <a href="http://www.google.com/search?q=lightbox+javascript" title="light boxes" target="_blank">light boxes</a> but they have the momentum at this moment. There are hundreds of library that can help you with them so don&#8217;t implement another one and re-use an existing one please&#8230;<br />
<span style="font-weight: bold"><br />
Contextual messages<br />
</span>Why not show a message where it happened. A little knowledge of <a href="http://www.javascriptkata.com/category/dhtml/" title="DHTML" target="_blank">DHTML</a> and you&#8217;re ready.</p>
<p>Example. On <a href="http://www.ecstatik.com/" title="Ecstatik!" target="_blank">Ecstatik!</a> (a project on which I worked), when there&#8217;s an error with the ajax vote, the label &#8220;I laughed!&#8221; is changed to &#8220;error!&#8221; written in red. Maybe it&#8217;s not the idea of the century but it works and it tells you where the error is.<br />
<span style="font-weight: bold"><br />
Fading in/out messages<br />
</span>These ones are more difficult to do but Google and WordPress do it elegantly. Use a library with a fading in/out function and show a message at the top of the page (or anywhere visible by the user). The user will get used to see messages at this place and will like the fact that they don&#8217;t have to click.</p>
<p><span style="font-style: italic">Note</span><br />
I expect a lot of people to tell me that I use them in almost all my examples. These are examples and I don&#8217;t want to begin to write DHTML just to show the result of bunch of javascript lines.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2007/05/16/do-not-use-the-alert-function-in-javascript/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Do not use the innerHTML property on HTML objects</title>
		<link>http://www.javascriptkata.com/2007/04/17/do-not-use-the-innerhtml-property-on-html-objects/</link>
		<comments>http://www.javascriptkata.com/2007/04/17/do-not-use-the-innerhtml-property-on-html-objects/#comments</comments>
		<pubDate>Tue, 17 Apr 2007 14:41:46 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[dhtml]]></category>
		<category><![CDATA[do not]]></category>
		<category><![CDATA[white belt]]></category>

		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=26</guid>
		<description><![CDATA[I already said Do not use the style property on HTML objects and as I was thinking how bad the innerHTML property can be, I considered doing an article on that subject. The problem was that stones were thrown at me after the first &#8220;Do not&#8221; article. Now I fear that this one may be [...]]]></description>
			<content:encoded><![CDATA[<p>I already said <a href="http://www.javascriptkata.com/2007/04/03/do-not-use-the-style-property-on-html-objects/" title="Do not use the style property on HTML objects">Do not use the style property on HTML objects</a> and as I was thinking how bad the innerHTML property can be, I considered doing an article on that subject. The problem was that stones were thrown at me after the first &#8220;Do not&#8221; article. Now I fear that this one may be worst&#8230; but I&#8217;ll stand proud&#8230;</p>
<h3>innerHTML seems easy</h3>
<p>I like easy things : Diner Kraft and automatic cars. It&#8217;s so easy to write HTML, why should I spend time with the DOM when I can easily do it with <span style="font-style: italic">innerHTML</span>?</p>
<p>With the Dom<br />
[source:javascript]<br />
var aLink = document.createElement(&#8220;a&#8221;);<br />
aLink.href=&#8221;http://www.javascriptkata.com&#8221;;<br />
aLink.appendChild(document.createTextNode(&#8220;Javascript Kata&#8221;);<br />
document.body.appendChild(aLink);<br />
[/source]</p>
<p>VS with innerHTML<br />
<code><br />
document.body.innerHTML += "&lt;a href=\"http://www.javascriptkata.com\"&gt;Javascript Kata&lt;/a&gt;";<br />
</code><br />
<span style="font-weight: bold">Why???</span></p>
<h3>innerHTML is dirty<span style="font-weight: bold"></span></h3>
<h3><span style="font-weight: bold"></span></h3>
<p>It may look good at first sight but it&#8217;ll look real bad the next morning without make-up. Did you noticed the <span style="font-weight: bold">\&#8221;</span>? Prepare yourself because with <span style="font-style: italic">innerHTML</span> you will have a truck load of these. Take a look at this.<br />
<code><br />
document.body.innerHTML += "&lt;a href=\"http://www.javascriptkata.com\" id=\"theLink\" class=\"aLink\" title=\"Javascript\"&gt;Javascript Kata&lt;/a&gt;";<br />
</code><br />
Ouch! My eyes burn.</p>
<h3>A lot of HTML</h3>
<p>iInnerHTML looks worst if you have a lot of And now, what if you have a lot of HTML code embedded in your javascript, it will win the war over you. I tell you.</p>
<h3>innerHTML and ajax</h3>
<p>I have an idea, if my ajax request returns some HTML, I could use the innerHTML property on an HTML object and assign the ajax response to it and it would be magic!</p>
<p>NO! In fact, you can do it but for every reasons in the world, <span style="font-weight: bold">don&#8217;t ever do that</span>. Yeah, I know, Yahoo!TV is doing it but don&#8217;t fall in that trap.</p>
<p><span style="font-weight: bold"> Ajax should return data to your application</span>. Why? Because you&#8217;ll never know what you&#8217;ll have to do with that information. For the moment, maybe you just do a simple display of the data but sooner or later, you&#8217;ll have to re-use for another section of the page. If you return HTML, you will end up parsing it to extract the data contained in it. I tell you.</p>
<p>By the way, forget about that old XML thingy, try <a href="http://www.json.org/" title="JSON" target="blank_">JSON</a>&#8230;</p>
<h3>It is not standard</h3>
<p>It doesn&#8217;t really matter to me but if you&#8217;re a <span style="font-style: italic">W3C-compliant</span> kind of guy, that may be a good point.</p>
<h3>Your friend is a library</h3>
<p>You have a lot a HTML code to produce through you javascript, go and get a good library. I can&#8217;t tell you which one to use because they all have the ups and downs. Choose one and stick to it.</p>
<ul>
<li><a href="http://jquery.com/" title="jquery" target="blank_">jquery</a></li>
<li><a href="http://mochikit.com/" title="mochikit" target="blank_">mochikit</a></li>
<li><a href="http://www.prototypejs.org/" title="prototype" target="blank_">prototype</a></li>
<li>another javascript library</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2007/04/17/do-not-use-the-innerhtml-property-on-html-objects/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>Javascript is not ajax</title>
		<link>http://www.javascriptkata.com/2007/04/13/javascript-is-not-ajax/</link>
		<comments>http://www.javascriptkata.com/2007/04/13/javascript-is-not-ajax/#comments</comments>
		<pubDate>Fri, 13 Apr 2007 15:02:42 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[dhtml]]></category>
		<category><![CDATA[white belt]]></category>

		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=25</guid>
		<description><![CDATA[I see a lot of person talking about how ajax is cool and it helps you doing things that you couldn&#8217;t do before it was &#8220;invented&#8221;. What most of these people don&#8217;t know is that most of the coolest features are not ajax. Ajax is just a way to communicate between a client and a [...]]]></description>
			<content:encoded><![CDATA[<p>I see a lot of person talking about how ajax is cool and it helps you doing things that you couldn&#8217;t do before it was &#8220;invented&#8221;. What most of these people don&#8217;t know is that most of the coolest features are not ajax. Ajax is <span style="font-weight: bold">just a way to communicate between a client and a server without reloading the whole page</span>. The rest of it is javascript, DHtml (dynamic Html) and CSS (cascading style-sheet).</p>
<h3>Ajax forces javascript</h3>
<p>Most people hate javascript but they want to do ajax, this is a duality. How do they overcome that problem? By using tons of javascript library and copying javascript snippets from tons of different sites. You know what? That won&#8217;t be any help for having well structured javascript. You just have to take javascript as seriously as your server-side language.</p>
<h3>The structure</h3>
<p>I saw a lot of web applications that were (almost) well documented. The developers spent hours thinking about doing a framework (argh!) that would help him handle all the code he will one day try to write. They didn&#8217;t have any thought about how they would write the javascript that will handle all that shit. <span style="font-style: italic">&#8220;It will just flow&#8221; </span>they say. No. I won&#8217;t <span style="font-style: italic">&#8220;just flow&#8221;</span>. It will be a mess.</p>
<p><span style="font-weight: bold">Think about the javascript you&#8217;ll have to write and treat it with respect</span>.</p>
<h3>DHtml???</h3>
<p>Once again, people do not make any difference between ajax and DHtml. When something in a page moves, it&#8217;s ajax. No, it&#8217;s DHtml. Ajax is just the response from the server that triggered the moving request. With DHtml, you can dynamically change the Html of your page using Javascript/DHtml (hey, I&#8217;m not talking about the innerHTML property). That&#8217;s where the javascript librairies (like <a href="http://jquery.com/" title="jquery">jquery</a>, <a href="http://script.aculo.us/" title="script.aculo.us">script.aculo.us</a> or <a href="http://mochikit.com/" title="mochikit">mochikit</a>) will help because the basic DHtml offered by javascript (with the DOM) is a real pain in the ass.</p>
<h3>And CSS???</h3>
<p>I also wrote <a href="http://www.javascriptkata.com/2007/04/03/do-not-use-the-style-property-on-html-objects/" title="Do not use the style property on HTML objects">Do not use the style property on HTML objects</a>. That&#8217;s where CSS enters the battle.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2007/04/13/javascript-is-not-ajax/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Do not use the style property on HTML objects</title>
		<link>http://www.javascriptkata.com/2007/04/03/do-not-use-the-style-property-on-html-objects/</link>
		<comments>http://www.javascriptkata.com/2007/04/03/do-not-use-the-style-property-on-html-objects/#comments</comments>
		<pubDate>Tue, 03 Apr 2007 13:17:01 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[dhtml]]></category>
		<category><![CDATA[do not]]></category>
		<category><![CDATA[white belt]]></category>

		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=20</guid>
		<description><![CDATA[Since ajax, a lot of people have to create HTML on the fly depending on response of the request. One mistake that I see a lot is that people are using the style property of their HTML objects. Why shouldn&#8217;t I do it? The style property has a precedence on every style Because the style [...]]]></description>
			<content:encoded><![CDATA[<p>Since ajax, a lot of people have to create HTML on the fly depending on response of the request. One mistake that I see a lot is that people are using the <span style="font-style: italic">style</span> property of their HTML objects. Why shouldn&#8217;t I do it?</p>
<h3>The style property has a precedence on every style</h3>
<p>Because the <span style="font-style: italic">style</span> property is so &#8220;close&#8221; to the HTML object, it will override every other style from a CSS file. So if you begin to write styles in your javascript code, you&#8217;ll have to do it all the time. It might be easy and clean for a basic project but it will become a mess in the future. <span style="font-weight: bold">You should centralize every style in your CSS files</span> so you won&#8217;t have to search where you assigned a style.</p>
<h3>Example</h3>
<p>I have this CSS</p>
<p>[source:css]<br />
div.theCssClass {<br />
	background-color:blue;<br />
}<br />
[/source]</p>
<p>and I create a new HTML object of this class</p>
<p>[source:javascript]<br />
// I create the DIV<br />
var div = document.createElement(&#8220;div&#8221;);<br />
div.appendChild(document.createTextNode(&#8220;This div is a test&#8221;));</p>
<p>div.className = &#8220;theCssClass&#8221;; // I assign theCssClass<br />
div.style.backgroundColor = &#8220;red&#8221;; // The background is red</p>
<p>document.body.appendChild(div);<br />
[/source]</p>
<p>You&#8217;ll see that the even if I set the background color to blue in the CSS file, the background will be red because I used the <span style="font-style: italic">style</span> property.</p>
<h3>The !important in CSS</h3>
<p>If you want to override a style assigned in javascript, you could always use the <span style="font-style: italic">!important</span> statement in your CSS file.</p>
<p>[source:css]<br />
div.useThisOne {<br />
	background-color:blue !important;<br />
}<br />
[/source]</p>
<p><span style="font-weight: bold">Use it only if you really have to. </span>I wouldn&#8217;t recommend <span style="font-style: italic">!important</span> because it breaks the default behavior of styles and can become very messy. When all is important, nothing is important.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2007/04/03/do-not-use-the-style-property-on-html-objects/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
