<?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>Rochak Chauhan::Unpredictably Exciting &#187; JavaScript</title>
	<atom:link href="http://rochakchauhan.com/blog/category/tips-and-tricks/javascript-tips-and-tricks/feed/" rel="self" type="application/rss+xml" />
	<link>http://rochakchauhan.com/blog</link>
	<description>Know your limits, but never stop trying to exceed them.</description>
	<lastBuildDate>Thu, 03 May 2012 11:48:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Most common JavaScript mistakes and their solutions</title>
		<link>http://rochakchauhan.com/blog/2008/09/25/most-common-javascript-mistakes/</link>
		<comments>http://rochakchauhan.com/blog/2008/09/25/most-common-javascript-mistakes/#comments</comments>
		<pubDate>Thu, 25 Sep 2008 10:39:27 +0000</pubDate>
		<dc:creator>rochakchauhan</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JavaScript mistakes]]></category>
		<category><![CDATA[rochak.js]]></category>
		<category><![CDATA[solutions]]></category>
		<category><![CDATA[Tips and Tricks]]></category>

		<guid isPermaLink="false">http://rochakchauhan.com/blog/?p=410</guid>
		<description><![CDATA[JavaScript has come a long way from being used just as Client Side Form Validator to becoming the backbone of Web 2.0 rich web applications. In the current scenario, every web developer have to learn JavaScript as there is hardly any Web 2.0 application which does not implement JavaScript. In my opinion followings are the &#8230; <a class="read-excerpt" href="http://rochakchauhan.com/blog/2008/09/25/most-common-javascript-mistakes/">Continue reading <span class="meta-nav">&#187;</span></a>]]></description>
			<content:encoded><![CDATA[<p>JavaScript has come a long way from being used just as <em>Client Side Form Validator</em> to becoming the backbone of Web 2.0 rich web applications. In the current scenario, every web developer have to learn JavaScript as there is hardly any Web 2.0 application which does not implement JavaScript.</p>
<p>In my opinion followings are the most common errors/mistakes made during JavaScript implementation.</p>
<ol>
<li><strong><em>Calling JavaScript Code without any event:</em></strong>This is the first error any JavaScript developer is bound to make. Allow me to illustrate it with a real life example. Suppose, we need to alert the content inside a div with id &#8220;content&#8221;. The novice developer would write:
<pre lang="javascript">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Invalid JavaScript Code&lt;/title&gt;
    &lt;script type="text/javascript" language="javascript"&gt;
        var content=document.getElementById('content').innerHTML;
        alert(content);
    &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="content"&gt;This is the sample content&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>Needless to say, the above code will throw an error. The technical explanation of the error is as follows. As JavaScript is an interpreted scripting language, it would try to read the content on the line 6. However, at that moment, the HTML had not been rendered and hence JavaScript will find no div. Its solution is very simple and elementary. All you have to do is to call this code on some event, like onload , onclick etc.  Following is the correct way to do it:</p>
<pre lang="javascript">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Valid JavaScript Code&lt;/title&gt;
    &lt;script type="text/javascript" language="javascript"&gt;
        function init(){
            var content=document.getElementById('content').innerHTML;
            alert(content);
        }
    &lt;/script&gt;
&lt;/head&gt;
&lt;body onload="init()"&gt;
&lt;div id="content"&gt;This is the sample content&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
</li>
<li><strong><em>Reading string as array:</em></strong>Well, once I committed this mistake and spent over 2 days to find this issue from 102 lines of JavaScript. Suppose you need to read and display the 3rd  character of a string. any C or PHP developer is bound to write the code like this:
<pre lang="javascript">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Invalid JavaScript Code&lt;/title&gt;
    &lt;script type="text/javascript" language="javascript"&gt;
        function readString(){
            var str="JavaScript";
            alert(str[2]);
        }
    &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;input type="button" onclick="readString();" /&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>Quite interestingly, the above code if alert &#8220;v&#8221; in Firefox or Mozilla. On few Internet Explorers it will throw an error. The reason is that reading string as an array (str[2]) is not an cross browser compatible. The correct way code is as follows:</p>
<pre lang="javascript">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Valid JavaScript Code&lt;/title&gt;
    &lt;script type="text/javascript" language="javascript"&gt;
        function readString(){
            var str="JavaScript";
            alert(str.charAt(2));
        }
    &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;input type="button" onclick="readString();" /&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
</li>
<li><strong><em>Validating for an blank field in JavaScript:</em></strong>This is one of the most common bug your quality analyst or client would let you know. When you are validating a blank field (say username). Most of the JavaScript developers  would write the following code to validate it:
<pre lang="javascript">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Valid JavaScript Code&lt;/title&gt;
    &lt;script type="text/javascript" language="javascript"&gt;
        function validate(){
            var value=document.getElementById('uname').value;
            if(value.length&lt;1) {
                 alert("ERROR: Username cannot be left blank");
                 return false;
             }
        }
    &lt;/script&gt;
&lt;/head&gt;
&lt;body onload="init()"&gt;
Username: &lt;input type="text" id="uname"&gt;
&lt;input type="button" onclick="validate()" /&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
</li>
</ol>
<p>Although the above code may seems to work, but it fails as soon as you enter any invisible character like space, newline/enter or a tab. The check is that the length of the string entered should not be less than 1 (in this case). What the developer has failed to negotiate that three invisible characters like spaces would count 3 but would be invalid. The solution is simple. Trim (similar to one in PHP) the value before applying this check. Unfortunately, there is no inbuilt function to trim a string. However you can use it from my free library <a title="Click to view source of rochak.js" href="http://rochakchauhan.com/rochak.js" target="_blank">rochak.js</a> as used in the following example.</p>
<pre lang="javascript">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Valid JavaScript Code&lt;/title&gt;
    &lt;script type="text/javascript" language="javascript" src="http://rochakchauhan.com/rochak.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript" language="javascript"&gt;
        function validate(){
            var value=document.getElementById('uname').value;
            value=rochak.trim(value);
            if(value.length&lt;1) {
                 alert("ERROR: Username cannot be left blank");
                 return false;
             }
        }
    &lt;/script&gt;
&lt;/head&gt;
&lt;body onload="init()"&gt;
Username: &lt;input type="text" id="uname"&gt;
&lt;input type="button" onclick="validate()" /&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://rochakchauhan.com/blog/2008/09/25/most-common-javascript-mistakes/feed/</wfw:commentRss>
		<slash:comments>432</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
