<?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; do not</title>
	<atom:link href="http://www.javascriptkata.com/category/do-not/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>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 javascript for validations</title>
		<link>http://www.javascriptkata.com/2007/04/25/do-not-use-javascript-for-validations/</link>
		<comments>http://www.javascriptkata.com/2007/04/25/do-not-use-javascript-for-validations/#comments</comments>
		<pubDate>Wed, 25 Apr 2007 13:11:14 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[do not]]></category>
		<category><![CDATA[white belt]]></category>

		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=29</guid>
		<description><![CDATA[This one will be widely accepted. The problem with it is that it&#8217;s so obvious that the question is asked here and there. Javascript is not secure I repeat : javascript is not secure. Your code is readable and it can be modified by anyone. It is great when you want to do a bookmarklet. [...]]]></description>
			<content:encoded><![CDATA[<p>This one will be widely accepted. The problem with it is that it&#8217;s so obvious that the question is asked here and there.</p>
<h3>Javascript is not secure</h3>
<p>I repeat : <span style="font-weight: bold">javascript is not secure</span>. Your code is readable and it can be modified by anyone. It is great when you want to do a <a href="http://www.javascriptkata.com/2007/04/19/how-to-do-a-bookmarklet-with-javascript/" title="bookmarklet">bookmarklet</a>. It is not so great when you want to have a <span style="font-style: italic">hard as a rock</span> web application.</p>
<p>Example, you want to validate that a manually entered date is less than today. The javascript code for it is not complex.</p>
<p>[source:javascript]<br />
function validateDate(theDate) {<br />
	var now = new Date();<br />
	return (theDate &lt; now);<br />
}<br />
[/source]</p>
<p>But anyone could override the function to always return <span style="font-style: italic">true</span> and all the dates that he enters would be good. This is bad.</p>
<h3>Always validate on the server-side</h3>
<p>Whenever you have to validate something in your web application, use the server-side. I know, it is redundant and long to do but you must do it. Someone once said that <span style="font-style: italic">you must consider every input from the user has evil</span>.I prefer the word <span style="font-style: italic">dirty</span>. Users sometimes don&#8217;t want to be evil to a web application but they are. And sometimes, users want to be evil.</p>
<p>If you have little time to implement an application, just do the server-side validations.</p>
<p>You must consider javascript validations as &#8220;pretty&#8221; validations. It validates things but it just to make your application prettier. In facts, it helps you with usability and it simplifies the life of the users of your application.</p>
<h3>The &#8220;cheap return&#8221; method</h3>
<p>To avoid to write a lot of code on the server-side for validations, I would suggest you a simple method that will help you code faster. It is called the cheap return method.</p>
<ol>
<li>You do all the validations on the client-side in javascript.</li>
<li>You do all the validations on the server-side but if it fails, you display a <em>generic</em> page to the users that says that it has failed.</li>
<li>You pretend to have done it completly on the client-side and server-side.</li>
<li>You spend the rest of your time watching the ceiling.</li>
</ol>
<p>The idea behind that is that if all javascript validations have passed, the users must have done something between the client-side and server-side. So, he is responsible of that error.</p>
<p>I don&#8217;t really like it either&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptkata.com/2007/04/25/do-not-use-javascript-for-validations/feed/</wfw:commentRss>
		<slash:comments>24</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>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>
