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 most common errors/mistakes made during JavaScript implementation.
- Calling JavaScript Code without any event: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 “content”. The novice developer would write:
-
<html>
-
<head>
-
<title>Invalid JavaScript Code</title>
-
<script type="text/javascript" language="javascript">
-
var content=document.getElementById(‘content’).innerHTML;
-
alert(content);
-
</script>
-
</head>
-
<body>
-
<div id="content">This is the sample content</div>
-
</body>
-
</html>
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:
-
<html>
-
<head>
-
<title>Valid JavaScript Code</title>
-
<script type="text/javascript" language="javascript">
-
function init(){
-
var content=document.getElementById(‘content’).innerHTML;
-
alert(content);
-
}
-
</script>
-
</head>
-
<body onload="init()">
-
<div id="content">This is the sample content</div>
-
</body>
-
</html>
-
- Reading string as array: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:
-
<html>
-
<head>
-
<title>Invalid JavaScript Code</title>
-
<script type="text/javascript" language="javascript">
-
function readString(){
-
var str="JavaScript";
-
alert(str[2]);
-
}
-
</script>
-
</head>
-
<body>
-
<input type="button" onclick="readString();" />
-
</body>
-
</html>
Quite interestingly, the above code if alert “v” 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:
-
<html>
-
<head>
-
<title>Valid JavaScript Code</title>
-
<script type="text/javascript" language="javascript">
-
function readString(){
-
var str="JavaScript";
-
alert(str.charAt(2));
-
}
-
</script>
-
</head>
-
<body>
-
<input type="button" onclick="readString();" />
-
</body>
-
</html>
-
- Validating for an blank field in JavaScript: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:
-
<html>
-
<head>
-
<title>Valid JavaScript Code</title>
-
<script type="text/javascript" language="javascript">
-
function validate(){
-
var value=document.getElementById(‘uname’).value;
-
if(value.length<1) {
-
alert("ERROR: Username cannot be left blank");
-
return false;
-
}
-
}
-
</script>
-
</head>
-
<body onload="init()">
-
Username: <input type="text" id="uname">
-
<input type="button" onclick="validate()" />
-
</body>
-
</html>
-
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 rochak.js as used in the following example.
-
<html>
-
<head>
-
<title>Valid JavaScript Code</title>
-
<script type="text/javascript" language="javascript" src="http://rochakchauhan.com/rochak.js"></script>
-
<script type="text/javascript" language="javascript">
-
function validate(){
-
var value=document.getElementById(‘uname’).value;
-
value=rochak.trim(value);
-
if(value.length<1) {
-
alert("ERROR: Username cannot be left blank");
-
return false;
-
}
-
}
-
</script>
-
</head>
-
<body onload="init()">
-
Username: <input type="text" id="uname">
-
<input type="button" onclick="validate()" />
-
</body>
-
</html>

If you liked my post, feel free to subscribe to my rss feeds





































2 Comments so far (Add 1 more)