Newsletter "Behind the Scenes" Newsletter

December 2013The monthly newsletter by Felgall Pty Ltd

My Word

Finding and Fixing JavaScript Errors

Or - What can Happen when any class fails to keep up to date and omits essential information from the first lesson

When I see the sorts of questions being asked in JavaScript forums it seems really obvious that many of the people asking those questions have not learnt anything about how to test their code. If they had then in many cases they'd be able to fix the code themselves without having to ask for help and in the remaining cases their code would be a lot closer to working than the point that they got to before asking for help.

It isn't just the JavaScript that they haven't learnt to test though. In many cases there are HTML errors in their code as well and these may potentially be contributing to the problem. The first step in getting JavaScript to work is to ensure that the HTML it is interacting with is valid. So the place to start is at http://validator.w3.org/ to validate the HTML. The best doctype to validate against at the moment is the HTML 4.01 strict doctype. If your page is using HTML 5 it is still best to validate with that doctype and then disregard and errors that the validator reports that relate specifically to the new HTML 5 tags and attributes.

The next step is to validate your JavaScript in exactly the same way. The site to do this is http://www.jslint.com/ and as your JavaScript is going in a web page you need to check the 'Assume a Browser' option before pressing the button to validate. Doing this will not only find all of the errors in the JavaScript before you even start testing, it will also find all of the places where the code may or may not work correctly because you are using old or inefficient code where far superior alternatives exist.

By fixing all of the code that this process identifies most of the errors that are asked about on forums would no longer occur. The main type of error that may still occur in the code if the above validations are passed are logic errors where the valid instructions in the code do not match the processing that is required to produce the desired result. To find and fix logic errors you need to actually run tests of the code and use the browser console and debugger to work out which piece of the code isn't doing what you expected.

Now beginners may not be able to work out how to fix all of the errors that the validator reports. In that case if they post a question on a forum specifying their code and the validation error they don't know how to fix they will make it much easier for others to help them to fix their code than if they posted it and just said that it doesn't work. First it demonstrates that they have made an attempt to find and fix the errors themselves and secondly the question is more specific making it easier to provide an answer without having to spend too much time investigating - which makes getting an answer more likely.

In many cases the questions being asked relate to homework for a JavaScript course the person is undertaking. That these students are not validating their code before asking for help indicates that these courses are leaving out a quite significant part of what they ought to be teaching as a part of the first lesson - that you need to make sure your code is valid before you try to run it.

Those learning to write JavaScript (whether in a class or on their own) would have a far easier time getting their code to work if they learnt to validate their code before they try to run it. That way they eliminate most of the potential causes of errors (including many of these that are more difficult to find during testing) right at the start.

Of course you may never have a need to learn JavaScript. That doesn't matter though because the same principal applies to almost anything. Where essential information is omitted from any course right at the start the chances of anyone being able to learn the subject properly is dramatically reduced.
 

On Site

Pages on both JavaScript and SQL this month. I expect to add more SQL and some CSS pages over the next couple of months - as well as the occasional page on something else (particularly if someone asks a question where the answer will make a useful page).
 

What's New

The following links will take you to all of the various pages that have been added to the site or undergone major changes in the last month.

Main Links

Ask Felgall
Past Newsletters
Sign Up/Unsubscribe
Question Forum

Categories

Browsers
HTML
Javascript
Interactive Web
Mainframe
PC Software
Networking
Comms Software
Word Processing
DTP
Graphics
OS/2
Linux
DOS/Windows
NT/2000/XP
Book Reviews
Links

Other Links

My Javascript Site
My Blog

http://www.felgall.com/