Fixing the Errors
It is said that the best way to fix errors is to avoid creating them. That's a great deal easier said than done. However, you can up your chances of getting fewer error messages by writing in a text editor that does not have margins. Also, allow each JavaScript command to remain on its own line. There's no need to break longer lines into two. In fact, doing that will probably throw errors. That said, I'll bet you get errors just about every time you start to play with this new language, so let's get into how to repair them.
The wonderful thing about a JavaScript error message box is that the little window that pops up tells you where and what the problem is. Look again at the error message above. It's a syntax error, meaning I have not configured the script correctly, and the error is on line 29. What's more, the error message is pointing at the problem area. Wouldn't it be great to get that in HTML?
The Error Line
When an error message denotes an error line, that line is counted down from the top of the HTML document, not the top of the JavaScript. For instance, the document below has an error in line 9. It's a syntax error because the instance was not allowed to close on the same line it started on. See how the parenthesis was jumped to the next line?
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="javascript">
document.write("text for the page"
)
</SCRIPT>
</BODY>
</HTML>
|
But why is the error on line 9? That's because you count from the top of the HTML document down, counting every line. Here's the document again, but this time with the lines counted for you.
(line 1) <HTML>
(line 2) <HEAD>
(line 3) <TITLE></TITLE>
(line 4) </HEAD>
(line 5) (line 6) <BODY>
(line 7)
(line 8) <SCRIPT LANGUAGE="javascript">
(line 9) document.write("text for the page"
(line 10) )
(line 11)
(line 12) </SCRIPT>
(line 13)
(line 14) </BODY>
(line 15) </HTML>
|
Notice that when you count the lines, you count all the lines, even the blank ones.
Now What?
Once you're actually at the line that has an error, you need to decide what to do. More times than not, if it's a Syntax error, the line has been chopped off early (truncated), something is misspelled, or you have used double quotes where a single quotes should go (unbalanced quotes).
If the error is Run-Time, then the command the error message is pointing at is a command that doesn't logically follow in the sequence. For instance, you call for a button by using a command that actually calls for a text box.
Multiple Errors
Nothing gives me heartburn faster than running a script and getting multiple errors. All you can do is sit while a whole slew of gray error boxes pile up on your desktop. I used to think multiple boxes meant there were actually multiple errors. Not always so.
JavaScript is an extremely logical language that likes things to move in a linear fashion. Let's say you have 10 errors throughout a long script. When the error messages pile up, the error that the computer found last in the script will be sitting on top of the pile of boxes. Do not go after that last error. It probably doesn't exist.
You see, the first error in the script may very well be creating all the other errors. So, fix the errors in sequence from the top of the HTML document to the bottom. Many times I've found a script that threw ~20 error boxes, but by fixing only the first error I solved all the problems.
So, fix the errors one at a time, from top to bottom. And each time you fix an error, run the script again. You might get 20 error messages, but only have to
fix one or two.
Something's Not Defined
This is also very common. This is a Run-Time error that means that something in the script doesn't jibe quite right. The text, to the computer anyway, has come out of the clear blue sky. I always make sure the text wasn't created by jumping a line down too early. If that's not the case, I try erasing it. It can always be put back at another time. Typos occur. See if this isn't one of those typos. It happens more times than you'd believe.
There's not much more that can be said about error messages at this point. You now have enough knowledge to fix 99% of the problems that pop up. Just remember that getting error messages is actually a plus. If you didn't get them, then all you would have is a blank page with no suggestions about what the problem might be. They're quite helpful if you think about them in the right light.
What You Have Learned
Your Assignment
Below there is a link to a page with a script. When you click on the link, the script will throw two errors. Your assignment is to fix the two errors so that the script runs. Now, you probably won't recognize some of the commands in this script, but that doesn't matter. The error boxes that appear will give you enough information to make this script run.
If the script runs correctly, the current date will display on the page. Again, each of these links will open a new window.
Hint: You may only get one error when you run it. The second error might then come after you fix the first.
Click Here For The Corrupted Script
Click Here For The Answer