Two Hoots Banner



Javascript Tips

  • To insert the date and time when the page was last modified use Javascript's document.lastModified property:
<script language="Javascript" type="text/javascript">
var d = new Date(document.lastModified)
document.write("Last modified on " + d)
</script>
  • Some of the main causes of Javascript coding errors are shown below:
Don't forget that Javascript variables are case sensitive so that MyVar is not the same as myvar.

Ensure that function names are spelled correctly and do not have any spaces or extraneous characters.

Ensure that all function names in a script are unique.

Except for the 'For' statement all argument lists are separated by commas.

Check that the {} characters use to identify a statement block are placed correctly.

All strings should have quotes around them, also ensure that the same quotation marks are used on the string, either (') or (").

Names of objects must be spelled with the correct capitalisation, i.e. Date, Array, Object etc.

Use the double equals signs (==) in a comparison expression.


  • Here are some of the more common error messages you may come across when working with Javascript:
'function does not always return a value' - The return statements in your function don't always return a value, ensure that they all either return a value or not.

'identifier is a reserved word' - The variable is a reserved word in Javascript.

'missing (...' or 'missing )...' - Either the open or close brackets is missing from a statement.

'missing ; after for-loop condition' or 'missing ; after for-loop initialise' - The 'for' statement is missing one of it's semicolons.

'missing { before function body' or 'missing } after function body' or 'missing } in compound statement' - Missing brace for a function or statement.

'nested comment' - You can't nest comments in Javascript.

'syntax error' - An error that Javascript has detected but unable to determine it's exact nature.

'unterminated string literal' - Missing " or ' at the end of a string literal.

'... cannot be indexed as an array' - Trying to use a non object as an array.

'... has no properties' - Attempting to reference properties of an object that has no properties.

'... is not defined' - Referencing a variable that does not exist.


  • Use the alert('message') to set watchpoints to help identify variable values within your code. Remove the alert watchpoints after the script has been debugged.

  • To add a new line to an alert message use \r. For example alert("This is line one.\r This is line two.").

  • To set focus to the first form field use the following code snippet with the associated html tags:
<script language="Javascript" type="text/javascript">
document.myform.myinput.focus()
</script>
<form id="myform" name="myform" action=...>
<input type="text" id="myinput" name="myinput"...>
</form>
  • Javascript provides a number of event handlers which handle event which occur on the web page. Here are some of the main ones -
onAbort - occurs with the browser STOP button is selected

onClick - occurs when the object is clicked

onFocus - occurs when the object receives focus

onBlur - occurs when the object looses focus

onMouseOver - occurs when the mouse passes over the object

onMouseOut - occurs when the mouse leaves the object

onSubmit - occurs when a forms Submit button is selected

onChange - occurs when the text field contents change

onSelect - occurs when the text field contents are selected

onReset - occurs when a forms Reset button is selected

onLoad - occurs when a image or document is loaded

onUnload - occurs when a image or document is unloaded



  • Use the following code to determine the size of the browser window
// Where supported in NN: (>NN4.0)
var winWidth = window.innerWidth;
var winHeight = window.innerHeight;

// Where supported in IE: (>IE4.0)
var winWidth = document.body.clientWidth;
var winHeight = document.body.clientHeight;