reptile7's JavaScript blog
Saturday, April 09, 2005
JavaScript / JScript errors
Blog Entry #4

In my first blog entry, I brazenly admitted that I do not know everything, so it is fitting that I devote some discussion to JavaScript error messages, the focus of HTMLGoodies' JavaScript Primers #2. As you might expect, a JavaScript error message announces that a mistake has been made in the writing of a JavaScript script, preventing its execution in whole or in part. Beyond THAT, however, the instructive information that is extractable from "these little jewels", as Joe Burns affectionately calls them, can vary considerably. I have typically been able to decipher JavaScript errors, and the underlying mistakes they point to, when writing my own scripts, which heretofore have been written at a relatively simple level of JavaScript. It's a different story when I encounter them while surfing the Web, however; I look at the highlighted source code of the 'offending' page and am seldom, if ever, able to figure out what the problem is. And man, do these errors pop up on the Web, all the time - there sure are a lot of sloppy 'JavaScripters' out there (including our friends at Microsoft, as we'll see below).

We kick off our discussion of JavaScript error messages by outlining how to 'access' them. Joe tells you what to do if you're a PC user; here's how I do it on my iMac:

Internet Explorer (5.1.6): Go to the Preferences command under the Edit menu. On the left-hand side of the Preferences window, select the Web Content option under the Web Browser heading. At the bottom of the Web Content settings

is an Active Content box with a group of four checkboxes, two of which relate to JavaScript: (1) the "Enable scripting" checkbox and (2) the "Show scripting error alerts" checkbox; the latter of these checkboxes, when checked, will generate a JavaScript error message window whenever MSIE encounters a JavaScript error. (Of course, the "Enable scripting" checkbox should be checked as well.) The amount of information in the error message window will vary, depending on the error, but the window does not have a "Details" button that must be clicked as is inferred by the primer.

In the absence of doing this, MSIE gives no evidence of JavaScript errors, at least on my computer. There's no "triangular sign in the lower left-hand corner" with "an exclamation point in the triangle", nor any "text proclaiming there are errors on the page", in contrast to Joe's observations vis-à-vis his PC.

Netscape (7.02): Netscape does not have an option for the automatic generation of JavaScript error message windows; instead, JavaScript errors can be accessed at the Netscape "JavaScript Console". Under the Tools menu, go to the Web Development command, and then the JavaScript Console subcommand; alternatively, and paralleling what Joe describes for Netscape in the primer, you can also launch the JavaScript Console by typing "javascript:" (no quotes) in the browser window's address/location bar and hitting Return. Outside of the JavaScript Console, however, Netscape gives no evidence (e.g., "instructions in the status bar") of JavaScript errors on my machine.

Types of Errors

Using Microsoft's 'taxonomy', Joe subdivides JavaScript errors into two types: Syntax errors and Run-time errors. (This is an MSIE thing; Netscape does recognize some errors as "syntax errors" but otherwise generally refers to them all as simply "Error[s]".) These errors reflect the two-stage process of JavaScript interpretation in which each command statement is (1) first analyzed to make sure its syntax is correct and (2) then executed. Syntax errors pertain to problems with periods, parentheses (and other punctuating enclosure marks, e.g., square brackets and braces, that will come up later), quotes, and other aspects of, well, syntax; contra the primer, misspellings are NOT syntax errors, but are recognized as run-time errors, as are incorrect capitalizations. Correspondingly, run-time errors pertain generally to command statements whose syntax is OK but which still won't execute for one reason or another.

On my computer, MSIE calls syntax errors "compilation errors". Moreover, Microsoft's reference material does suggest that JScript, the Microsoft 'knockoff' of (Netscape's) JavaScript, is compiled as well as interpreted by the Internet Explorer browser.

Since working through this primer, I have kept a running log of the different JScript errors that have popped up while surfing the Web with MSIE. Here they are*:

Microsoft JScript compilation errors
Syntax error
Expected ')'
Expected identifier or string
Unterminated string constant
Invalid character
Conditional compilation is turned off

Microsoft JScript runtime errors
Object doesn’t support this property or method
Object doesn't support this action
‘whatever’ is undefined
Object expected
Invalid procedure call or argument
Argument not optional
Permission denied
Out of memory
Out of stack space

(*All of the compilation (syntax) errors but only a couple of the runtime errors appear on the corresponding Microsoft lists linked above. Note that the "Invalid character" compilation error plagues Microsoft's MSDN Library Web sites.)

I will not attempt to define these various errors but am merely listing them here 'for the record'.

Error message information

Joe informs us, "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"; this is often, but not always, true. Consider the code below containing a script whose document.write( ) statement is missing a right parenthesis:

<title>JavaScript Error Messages</title>
<script language="javascript">
document.write("Today is Thursday."

With MSIE, the script above generates an "Expected ')' " compilation error:

As you can see, the error message in this case specifies the line of the document (line 7, counting down from the top of the document, as Joe notes) and the character of that line (character 35, counting from the left with the first character indexed as 0, and not 1) that is problematic. Clicking the window's Source button opens a source-code window in which the error location is highlighted:

Unfortunately, some MSIE JScript error message windows do not give the aforementioned line and/or character information, nor a meaningful highlight in the source code - you're on your own in those cases.

Netscape is less helpful. Netscape's JavaScript Console error message for the above script is:

appears here. Netscape's description of the error, "missing ) after argument list", is similar to but less intuitive than that of MSIE. With respect to the error location, Netscape identifies line 8 as the problem, perhaps because the script will run if a right parenthesis is put at the start of line 8 (just before </script>), but I again find this less satisfying than MSIE's marking the error at line 7. In lieu of character information, Netscape points, sort of, with a little green arrow to the error, but not very accurately, as you can see. Finally, the source-code window that is opened by clicking the Source File link

has no highlight of the error.

Quick comments on other issues

I don't have anything to add to Joe's remarks (a) on margins and command lines in the "Fixing the Errors" section and (b) on propagating errors in the "Multiple Errors" section of the primer, but I do want to comment on...

"The Error Line"

This section of the primer contains some of the infamous HTMLGoodies sloppiness on which I commented in my first blog entry. Joe attempts to illustrate a truncation syntax error with some code that is initially - and wrongly, with respect to Joe's discussion of the error location - typed as 7 lines but is a little later typed rightly as 15 lines. Much more serious than this minor-but-annoying mistake is that, as written, the code in question does not in fact throw an error, but is OK. I discussed line truncation in JavaScript ad nauseam in my last blog entry, so I won't belabor the point here.

The end-of-primer assignment and its answer

Joe asks the reader to identify two errors in an 18-line block of code.

The first error, which is generated by the "...x" 3rd line, is indeed a syntax error but occurs because the line begins with a period, a no-no in JavaScript, NOT because " '...x' was not defined", as claimed in the assignment answer. (On the other hand, if the 3rd line is changed to simply "x" (without the 3 periods), then an " 'x' is undefined" run-time error does in fact pop up - try it yourself.) Joe himself notes that the " 'whatever' is undefined" error is a run-time error, as opposed to a syntax error, in the "Something's Not Defined" section of the primer.

The second error, which is generated by the "document.wrte(" ",month,"/",day,"/",year," ")" 15th line, gives an "Object doesn’t support this property or method" run-time error with MSIE and a "document.wrte is not a function" error with Netscape - the misspelling of "write" is NOT a syntax error, in accord with our discussion earlier.

And that concludes our dissection of Primer #2. HTMLGoodies' JavaScript Primers #3, which we'll hit next time, is somewhat of a 'trial by fire'; the progression in difficulty in going from Primer #2 to Primer #3 is as great, IMO, as that between any two of the HTMLGoodies JavaScript primers. If you can make it through Primer #3, then you should be able to slog your way through the rest of them.


Comments: Post a Comment

<< Home

Powered by Blogger

Actually, reptile7's JavaScript blog is powered by Café La Llave. ;-)