If the code doesn't work, begin the troubleshooting process with these two steps:
- Check to see if there are any error messages.
- Check the common sources of problems listed below.
Common Sources of Problems
- Missing DOM elements or problems with ID
TypeError: [something] is null.
- Missing or incorrect style specifications
- Be sure that any style specifications required by the code are in place and consistent with instructions and example documents. The code may fail silently or display incorrectly without them.
- Code not properly initialized
- Check that any necessary initialization routines are in place and that they are being properly invoked. Modern code will generally not display errors when not properly initialized.
- Syntax errors introduced during implementation
Common Syntax Error Messages
A few of the common syntax errors which may be inadvertently introduced during implementation are listed here along with their meaning and possible solutions.
- Unterminated string constant (or literal)
- Check to see whether 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.
- 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 may contain quoted HTML attributes, in which case it is easiest to use single quotes to enclose the variable content. Here is an example which also contains an apostrophe:
var msg = '<div class="info">Here\'s the content.</div>';
- Expected ')' or missing ) after argument list
- Perhaps you have left out a comma between array elements, or inserted an unescaped apostrophe in an array element enclosed in single quotes.
- Expected ']' or missing ] after element list
- Same as above when using array literal syntax.
If you are encountering errors when working with object literals, perhaps our tutorial on the subject would be of assistance.
- Some older less robust code may trigger errors when event handlers are activated, for example, when users hover over links set up for use by the code. ^