JavaScript Errors
This discussion covers the types of error messages that can occur when implementing code from dyn-web or other sources. For example, it is very easy to introduce syntax errors into the code when adding images for rotation or content for tooltips, etc.
Another common source of problems is forgetting to upload any necessary external JavaScript files or specifying the path incorrectly. Read on for assistance in correcting these errors by deciphering the error messages they generate.
JavaScript Error Messages - Finding and Deciphering
How do you know whether a JavaScript error has occurred or not? That depends upon what browser you are using and it's configuration.[1] Internet Explorer displays a yellow warning icon in the status bar. Double-click to open a dialog box, then click on Show Details for information about the problem. You can adjust your settings so that the dialog box will always open up when an error occurs, but unfortunately Internet Explorer's error messages are not as informative as they could be. Especially the line number reported for the error!
My recommendation: if the JavaScript you are using is not working, if you are not already viewing the document in Firefox please do so. Then view Firefox's Error Console, which can be found under the Tools menu. Click on any error messages reported to go straight to the problem location.
Common error messages
When an error message reads Object expected or [something] undefined, that most likely means you are missing an external JavaScript file, or have the wrong path to a JavaScript file. More generally, it means a function being called or a variable being referred to is not available.
The message unterminated string constant (or literal) means you have inserted a carriage return in the middle of a string variable value or array element, or deleted a quote or have mismatched quote pairs. The line number in the error message will point you to the location.
If the error message reads: Expected ')' or missing ) after argument list perhaps you have left out a comma between array elements, or inserted an unescaped apostrophe in a array element enclosed in single quotes.
You can use double quotes or single quotes to enclose a string. But they must be in pairs. If you want an apostrophe to appear in your string enclosed in single quotes, put a backslash before the apostrophe.
String variables for tooltips often need to contain quoted html attributes. So single quotes are used to enclose the variable content. Here is an example which also contains an apostrophe:
var msg1='<div class="info">Here\'s the content.</div>';
When you try to add an event listener using addEventListener, or attachEvent for Internet Explorer, but the function you are trying to attach is unavailable, browsers report rather mysteries error messages. For Firefox, it begins with unable to convert, for Internet Explorer, not implemented.
These are just a few of the most common error messages that might occur when you try to implement code that you know works (i.e., it worked in the demo). Error messages during code development and debugging are another matter entirely.
If you are encountering errors when working with object literals perhaps our brief tutorial on the subject would be of assistance.
- If you are using Firefox's very handy Web Developer extension you will see a red exclamation mark in the far right of the toolbar when a JavaScript error has occurred. Click on it to open the Error Console. ^