Debugging JavaScript Code for Errors
Debugging JavaScript code has always been an headache for the JavaScript programmers. This is mainly due to improper errors given by the browsers for errors in JavaScript code. You cannot rely on the line no given by the browser for JavaScript code errors but you need to know some techniques on debugging the same. This article will give you an insight on how you can debug the JavaScript code for errors. This may not be 100% but here i am discussing my experience on how we can debug javascript codes for errors.
1. The most commonly debugging technique used is to insert alert statements into the JavaScript code. Try giving alert statements for the variables , function arguments, and existence of object by simply alert the object variable. This approach will give you an idea on value getting stored, function getting called etc.
2. Use different browsers for testing JavaScript code. This is helpful because every browser has different presentation for showing JavaScript Errors.
Example
<html>
<head>
<script type="text/javascript">
function detect_div1()
{
document.getElementById("test1");
}
window.onload = function()
{
detect_div();
}
</script>
</head>
<body>
<div id="test"></div>
</body>
</html>In Microsoft IE you get “Object Expected” error if you are referring to an JavaScript function which is not defined.
In Mozilla Firefox you get “detect_div” is not defined. This gives you a hint of where the error exists.
3. If your page uses multiple external js files then try to comment the individual include code and check for the JavaScript files giving JavaScript Error.
4. Check for window.onload event in your JavaScript Code and follow the code flow when the page loads.
5. If your page is loading multiple js files avoid using same variable names across these js files as there will be clash with the content.
6. For better error trapping try using try…catch statement in javascript. This can let your javascript code runs smoothly without halting the code execution.
7. Try to end the statements with semicolon especially when you are compressing the js code.
8. In Microsoft Internet Explorer, make sure to turn error reporting on. If this is disabled then Microsoft Internet Explorer will not give yellow flag error and programmer will end up scratching their heads on where the errors is. This is applicable to Opera Browsers.
Similar Posts
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.


Comments
No comments yet.
Leave a comment