ava script tutorial - a new approach by Ken J Ward

Java Script Tutorial - Help!!!

help.gif (2043 bytes)


 

The following are common errors in JavaScript:

Lack of Matching Quotes or Brackets

Misspelled Words

Abbreviations

And that's it! Of course, there may be errors in the code itself that you have written, but usually the above are the reason for most errors! When an error occurs, the Browser may something like:

Line: 12

Character: 24

Error: Unterminated string constant

Code: 0

Lack of Matching Quotes.

You must not break a line of text. For example:

alert('Hello People!');

If you write:

alert('Hello
People!');

The text is broken. When JavaScript gets to the end of the line, it can't find the matching (') so it raises an error: No matter, that it is really on the next line, JavaScript stops and complains. 'Unterminated string constant', or whatever it feels like saying at the time! The solution is to write all the text between quotes on one line.

When you begin to use any quote - single or double - on a line there must be a matching end quote on that line, or you get an error! (This is the probable reason why (and if) the code in this tutorial might not work - the Browser has wrapped the demonstration code).

Solution: Put all the text in one line!


A similar error occurs when you do not have a matching bracket. When you start with one bracket ('(' or '{' or '[') then there has to be a matching end bracket somewhere (although not necessarily on the same line!) For example:

top.location.assign((MyUrl);

I have forgotten the end bracket. JavaScript may say something like "expected )".

Solution: Supply the missing bracket!


Misspelled Words

If you write:

message='Hello';

alert(messag);

Then you will get an error message like 'messag is undefined'. This could mean you really haven't defined what 'messag' is - so how can it write it! - or you have defined it, but spelled it differently this time! (Most likely!)

Abbreviations

By abbreviations, I means words like 'don't'. Did you notice that extra single quote to show something is missed out? So:

alert('Don't forget the milk!');

Gives an error 'Expected )'. This error has a line number and characters similar to where the single quote in 'don't' occurs. JavaScript thought that was the end of your alert, but really you had used an abbreviation.

Solution: Precede the ' in don't with a \, so JavaScipt knows you aren't talking to it, but this is still the text. So:

alert('Don\'t forget the milk!);

And JavaScript ignores the single quote!


Almost all the errors you have with JavaScript will be one of the above!

happyHelp.gif (1661 bytes)

[If you need to learn more about HTML visit the HTML Tutorial]

I am always pleased to hear from you.
Send your comments to