<?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/tag/javascript/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>Rochak.js &#8211; Javascript class of common functions</title>
		<link>http://rochakchauhan.com/blog/2008/10/10/rochakjs-javascript-class-of-common-functions/</link>
		<comments>http://rochakchauhan.com/blog/2008/10/10/rochakjs-javascript-class-of-common-functions/#comments</comments>
		<pubDate>Fri, 10 Oct 2008 13:36:58 +0000</pubDate>
		<dc:creator>rochakchauhan</dc:creator>
				<category><![CDATA[Downloads]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[detect browers]]></category>
		<category><![CDATA[detect chorme]]></category>
		<category><![CDATA[explode]]></category>
		<category><![CDATA[find mouse position]]></category>
		<category><![CDATA[inArray]]></category>
		<category><![CDATA[rochak.js]]></category>
		<category><![CDATA[trim]]></category>
		<category><![CDATA[validate email]]></category>

		<guid isPermaLink="false">http://rochakchauhan.com/blog/2007/10/08/rochakjs-javascript-class-of-common-functions/</guid>
		<description><![CDATA[Rochak.js - Javascript class of common functions <a class="read-excerpt" href="http://rochakchauhan.com/blog/2008/10/10/rochakjs-javascript-class-of-common-functions/">Continue reading <span class="meta-nav">&#187;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This class is a collection of basic and commonly used functions in JavaScript.  I coded this class just to validate a form in one of my sites. But it turned out to be more than helpful. I hope it will be of some help for all the web developers.</p>
<p>If you want a new function added or face any difficulty, feel free to contact me.</p>
<ul>
<li>Initialize:<a title="Rochak.js" href="http://rochakchauhan.com/blog/wp-content/uploads/2007/10/rochak-10.js"></a>
<ul>
<li>Just include it at the very top of all the JavaScript file as:
<pre>&lt;script src="rochak.js" type="text/javascript"&gt;</pre>
</li>
</ul>
</li>
<li>Usage:
<ul>
<li>For example to trim a string:
<pre>rochak.Function_Name(Parameter);    eg:
var str=rochak.trim(str);</pre>
</li>
</ul>
</li>
<li>Complete JavaScript Function List:
<ol>
<li><strong>lTrim(string):</strong>
<ul>
<li>Function to trim all the white spacing from the left side of any string.</li>
</ul>
</li>
<li><strong>rTrim(</strong><strong>string</strong><strong>):</strong>
<ul>
<li>Function to trim all the white spacing from the right side of any string.</li>
</ul>
</li>
<li><strong>trim(</strong><strong>string</strong><strong>):</strong>
<ul>
<li>Function to trim all the white spacing from left and right side of the string.</li>
</ul>
</li>
<li><strong>isValidEmail(</strong><strong>string</strong><strong>):</strong>
<ul>
<li>Function to validate a typical email pattern.</li>
</ul>
</li>
<li><strong>getType(variable_name):</strong>
<ul>
<li>Function to find the type of the variable. A Clone of PHP\&#8217;s &#8220;<strong>var_dump()</strong>&#8221; function.</li>
</ul>
</li>
<li><strong>inArray(array, string):</strong>
<ul>
<li>Function to find if the string is in the array. Clone of PHP\&#8217;s &#8220;<strong>in_array()</strong>&#8221; function.</li>
</ul>
</li>
<li><strong>inArrayI(</strong><strong>array, string):</strong>
<ul>
<li>Function to find if the string is in the array. Clone of PHP\&#8217;s &#8220;<strong>in_array()</strong>&#8221; function. (its case insensitive)</li>
</ul>
</li>
<li><strong>getMouseX(event):</strong>
<ul>
<li>Function to find the current X position of the mouse/cursor.</li>
</ul>
</li>
<li><strong>getMouseY(event):</strong>
<ul>
<li>Function to find the current Y position of the mouse/cursor.</li>
</ul>
</li>
<li><strong>getMouseXY(event):</strong>
<ul>
<li>Function to find the current X and Y positions of the mouse/cursor as an array.</li>
</ul>
</li>
<li><strong>getBrowserName():</strong>
<ul>
<li>Function to return the name of the web browser.</li>
</ul>
</li>
<li><strong>isBrowserIe():</strong>
<ul>
<li>Function to return &#8220;<strong>true</strong>&#8221; if the browser is &#8220;<strong>Microsoft Internet Explorer</strong>&#8221; else return &#8220;<strong>false</strong>&#8220;.</li>
</ul>
</li>
<li><strong>isBrowserOpera():</strong>
<ul>
<li>Function to return &#8220;<strong>true</strong>&#8221; if the browser is &#8220;<strong>Opera</strong>&#8221; else return &#8220;<strong>false</strong>&#8220;.</li>
</ul>
</li>
<li><strong>isBrowserNetscape():</strong>
<ul>
<li>Function to return &#8220;<strong>true</strong>&#8221; if the browser is &#8220;<strong>Firefox</strong>&#8221; or &#8220;<strong>Netscape</strong>&#8221; else return &#8220;<strong>false</strong>&#8220;.</li>
</ul>
</li>
<li><strong>getTotalImages():</strong>
<ul>
<li>Function to return the number of images used in the current page.</li>
</ul>
</li>
<li><strong>explode(delimiter,</strong> <strong>string):</strong>
<ul>
<li>Function to Split a string by a specified delimiter. A Clone of PHP\&#8217;s &#8220;<strong>explode</strong>&#8221; function.</li>
</ul>
</li>
<li><strong>end(</strong><strong>string / array):</strong>
<ul>
<li>Function to return the last character of a string or the last element of the array. A Clone of PHP\&#8217;s &#8220;<strong>end</strong>&#8221; function.</li>
</ul>
</li>
</ol>
</li>
</ul>
<p><script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script> Change log (1.1)</p>
<ol>
<li><strong>isBrowserFirefox()</strong>:
<ul>
<li>Function to return &#8220;<strong>true</strong>&#8221; if the browser is &#8220;<strong>Firefox</strong>&#8221; else return &#8220;<strong>false</strong>&#8220;.</li>
</ul>
</li>
<li><strong>isBrowserSafari()</strong>:
<ul>
<li>Function to return &#8220;<strong>true</strong>&#8221; if the browser is &#8220;<strong>Safari</strong>&#8221; else return &#8220;<strong>false</strong>&#8220;.</li>
</ul>
</li>
<li><strong>isBrowserChrome()</strong>:
<ul>
<li>Function to return &#8220;<strong>true</strong>&#8221; if the browser is &#8220;<strong>Chrome</strong>&#8221; else return &#8220;<strong>false</strong>&#8220;.</li>
</ul>
</li>
<li><strong>$$(idName)</strong>:
<ul>
<li>Function to similar to <strong>&#8220;document.getElementById(&#8216;idName&#8217;)&#8221;</strong>.</li>
</ul>
</li>
</ol>
<p><em><strong>To </strong><strong>download the latest code, <a title="rochak.js" href="http://rochakchauhan.com/rochak1.1.js" target="_blank">right click here</a> and click on &#8220;Save link as&#8221; or &#8220;Save target as&#8221;.</strong></em></p>
<ul>
<li><a title="rochak-1.0.js" href="http://rochakchauhan.com/rochak1.0.js" target="_blank">rochak-1.0.js</a></li>
<li><a title="rochak1.1.js" href="http://rochakchauhan.com/rochak1.1.js" target="_blank">rochak-1.1.js</a></li>
</ul>
<p>Or visit:   <a href="http://www.jsclasses.org/package/6-JavaScript-Manipulate-text-arrays-and-get-browser-details.html" target="_blank">http://www.phpclasse.org</a></p>
]]></content:encoded>
			<wfw:commentRss>http://rochakchauhan.com/blog/2008/10/10/rochakjs-javascript-class-of-common-functions/feed/</wfw:commentRss>
		<slash:comments>397</slash:comments>
		</item>
		<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! -->
