Home : Internet : JavaScript : Error

JavaScript Error

This page explains what a JavaScript error is and how to deal with it.

Most internet users have encountered the annoying JavaScript error at some stage. This happens when a script in a web page contains an error or fails to execute correctly. Exactly what happens depends on the browser and type of error, but in most cases some sort of error notification will be displayed.

The Error Message

Status Bar Error Alert

In Internet Explorer's default setting, a small warning icon appears in the status bar like the one pictured left. Double-click this icon to display a window with the complete error message. The complete error message looks something like this:

Error - Object Expected

Note: If you see a less detailed version of this window, click the "Show Details" button.

You can choose to have this error window appear automatically whenever an error occurs. This could be annoying for general surfing, but it is very useful when writing your own JavaScript. This is because the error message always lets you know when something is wrong with your code, and even better, it usually tells you exactly where in the HTML document the problem lies (in the above example, line 71 character 1).

What to Do When You See an Error

If you are visiting a website which shows an error, you obviously can't fix it but if you're feeling generous you might like to contact the webmaster and let them know. You can still view the page (disabling automatic error messages will make it easier), but whatever the script was supposed to do won't work. This may or may not effect on the functionality of the page.

If you are the webmaster, you need to understand what the error message is telling you and fix it. Keep reading....


How to Fix JavaScript Errors

There are two types of JavaScript error:

Syntax Error

The function below contains an error and will result in the error message as pictured. In this case the message tells you that a closing parenthesis is missing.

function showAlert() {
alert('Hello World!';
}
Error - Expected ')'

Not all error messages are as clear as this but once you've seen a few (and you will see a few!) you'll start to recognise what they mean.

Runtime Error

The function below is constructed correctly and will run with no problems as long as the variable something can be found (remember that a text string without quotes is assumed to be a variable). If this variable is not defined an error will occur.

function showAlert2() {
alert(something);
}
Error - Undefined variable

Note: Syntax error messages appear as soon as the page loads, even if the error is inside a function. Runtime error messages appear when the affected code runs, so if an error is inside a function the message will appear when that function is executed.


Summary

Whilst these error messages may seem annoying at first, learn to love them - they are your friends! As long as you always test your web pages you'll know if any errors have crept in, and JavaScript error messages are very helpful.