Old IE style error message box will not give the correct line number and error location for error.
good. This will only reduce scope of your script.
Use alert for debugging. This will tell whether you have real object at a time or not for which
you are trying to access/assign property. This will also tell you how far your code is good.
But for very serious syntactical error it will not work.
Check that the object's property you are using really exists-
Below if you click on testbox it will tell you their object type.
<form> <input type="text" name="t1" onclick="alert(this)"/> <input type="button" name="submit1" value="submit" onclick="call1(this.form)"/> </form>
And same with the variables.
<script> var string1 = 'PHP'; alert(string1.length); //alert(string3.length); // Error will show: "string3 is not defined". </script>
But if no string is defined, then what will return.
You can access the variable in any function if you have defined variable in global scope.
Define like: var variableName = 'Value';
outside of function and without 'var' keyword inside function. Var used inside function makes it local to that function.
Don't use reserved words.
Reserved words are:
break, case, catch, continue, default, delete, do, else, finally, for, function, if,
in, instanceof, new, return, switch, this, throw, try, typeof, var, void, while, with.
This example will show the problem. Click on submit and you will get error:
"frm1.submit is not a function". Rename input "type=button" to "submit1". Now it
will submit the form.
For solving this error I spent lots of hrs many time.
Proper variable names
small letter for all variable name and function. Underscore is visibly better than character change.
EvalError - raised when the eval() functions is used in an incorrect manner;
RangeError - raised when a numeric variable exceeds its allowed range;
ReferenceError - raised when an invalid reference is used;
TypeError - raised when the type of a variable is not as expected;
URIError - raised when the encodeURI() or decodeURI() functions are used in an incorrect manner;
You can trap more than one exception, and handle each one in a different way,
by using multiple "if" constructs within a single "catch" block.
Copy this code in different page. As error handler (handleErr) is defined above,
so that will directly catch error.
Update- Aug, 2009: Better way of error handling: Console.log() in place of alert() -