| HTML Tags |
JavaScript code is published within the HTML of the Web page, delimited
by the script tags:
<script language="javascript"> ... </script> |
| Coding |
Everything written between the script tags is the JavaScript
code itself. Lines of the code are terminated with a semi-colon (optional
on the last line within the script):
<script language="javascript">
sText="<b>Generated
by </b><i>JavaScript</i>.";
document.write(sText);
</script>
Generates:
|
| Comments |
Comments can be incorporated into scripts using the following methods:
| // text of comment |
everything following the double slashes is ignored |
| /* text of comment */ |
everything between the '/*' and '*/' is ignored |
|
| Variable Declaration |
| The working data within a script is made up of variables which are
either in-built types of data, such as numeric or string values, or they
are instances of an object type that is defined within the language. In
either case, the naming of the variable is important as it must not clash
with any other element that is known to the script, another variable or
any of the in-built names (objects, methods, etc.- the reserved words).
Variable names cannot contain spaces, but they can use under-scores. |
Declaration of a non-object variable is done as follows:
var variable = value; |
| A variable can be numeric or text, depending on the type of
data that is assigned to it. The numeric value of a text variable
is zero. |
| The context within which a variable is declared is all important.
Defining a variable within the <head> tags but outside of a function
ensures that it is visible to all JavaScript code within the document. |
| Assignment |
Values are assigned to variables (including object instances) using
the equals sign:
variable = value; |
| Object Instantiation |
The creation of objects is acheived as follows, where object
is the type of object to be created and instance is the name of
this instance of the object (the name that will be used to reference this
instance in future):
instance = new object(...);
For example, to create a Date object that corresponds to the
current date and time:
today = new Date(); |
| Method Invocation |
Actions are performed on objects by suffixing the object instance with
a dot ('.') and the method name and enclosing any parameters in brackets.
Any value returned by the method can be assigned to a variable as above:
variable = instance.method(parameters); |
| Object Properties |
The properties of an object correspond to values of variables that
are held inside of the object itself. Each instance of the same type
of object will exhibit the same range of properties, but the values of
each may be different. The current value of each property is accessed
in a similar way to methods above:
variable = object.property;
// Retrieve the value
object.property = variable;
// Update the value |
| Function Declaration |
| Functions allow self-contained blocks of code to be written once and
used throughout the document as and where necessary. This code must
be placed within the <head> section of the document so that it is available
throughout the body. The comment structure in the following template
example demonstrates how to ensure that the code is not visible
to browsers that do not support JavaScript: |
<SCRIPT LANGAUGE="javascript">
<!-- Hide from browsers
that do not understand Javascript
function Function_Name
(parameters ...)
{
// Body of function:
// ...
}
// end hiding -->
</SCRIPT> |
| Conditional structure |
JavaScript supports the following structure that allows conditional
control over the sequence in which the code is executed:
if (condition)
{ statements }
else
{ statements }
If the 'statements' consist of a single code phrase,
the delimiting curly brackets can be left out. If no curly brackets
open immediately after the 'if' or 'else' statements, only the immediately
following code phrase is treated as part of the conditional structure. |
The condition may be any combination of the following:
variable == value
// or other conditional-operation
object.property
// where the value is equivalent to a boolean
object.method()
// return value is boolean |
| Loops |
| There are two distinct types of loops that are available, both of which
are identical to their Java equivalent. |
'For' loops are driven by a counter that has an the following structure:
for (initial value; condition;
step
operation)
{ statements; }
For instance, the following:
for (i=0; i < 3; i++)
document.write(i+"<br>"); |
Produces:
|
|
'While' loops perform a very similar role, except that the loop is
only driven by the condition that determines whether another iteration
is required and the initialisation and step operation can
be isolated:
while (condition)
{ statements; }
Equivalent to the above:
var i = 0, max = 2;
while (i <= max)
{ document.write(i+"<br>"); i++; } |
Produces:
|
|
| Arrays |
The declaration and initialisation of an array takes the following
form:
variable=new Array();
variable[0]="0";
variable[1]="1"; |
There are a number of pre-defined arrays in the object hierarchy
within JavaScript:
document.form_name is equivalent to document.forms[n]
document.image_name is equivalent to document.images[n] |