JavaScript Primer #2
Error Messages

Use these to jump around or read it all

The Concept Error Message Fixing Errors What You've Learned Your Assignment

The Concept

You know what topic I've found missing from the myriad JavaScript books I've read? Error messages. I guess the assumption is that you'll get it all right the first time and never see one. Welcome to reality.
     If you've ever attempted to write or install a JavaScript on your Web pages, then you know these little jewels are part of the fun. Just when you think you've got it right, boom! One of these pops up:

This lesson is intended to tell you what to do when you encounter your own error messages. I've worked through thousands of them. If you start writing JavaScript, you'll get your share, too.

     Please Note! In later versions of MSIE and Navigator, you may not get the box above. I guess the programmers felt there was a better way.

     If you are using MSIE, the error message will first appear as a triangular sign in the lower left hand corner. There will be an exclamation point in the triangle. There will also be some text proclaiming there are errors on the page. Click on that triangle to get the error message we'll discuss in this tutorial. OR! If you want to get the error message box each time without clicking, go to the Tools menu, choose Internet Options. In Internet Options, click on the Advanced tab and make sure the line that reads: "Display a notification about every script error" is checked.

     Then again, depending on how your system was configured when you got it, you may already be getting the box. You'll also note on the error box that you'll have to click the "Details" button to see the text describing the error.

     If you are using a later version Netscape Navigator, then you'll get instructions in the status bar. If there in an error, you'll be told to type javascript: into the location bar. Then, you'll get the error and the text regarding it.

     I don't understand the change either...but them's the breaks. Moooooving along...

The Error Message

There are basically two types of errors you can produce: Syntax errors and Run-Time errors. A Syntax error means that you've misspelled something or the JavaScript is not configured correctly. A Run-Time error means that you have used an incorrect command. Either way, they both mean the same thing. Somewhere, something's messed up.
     Now, there are programs out there that will help you fix your errors, a process called "debugging," but I still prefer to do it by hand. It's actually easier than you think.

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?



<SCRIPT LANGUAGE="javascript">
document.write("text for the page"



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

The Concept Error Message Fixing Errors What You've Learned Your Assignment

On To JavaScript Lesson #3