Search
Sponsors

Archive for the ‘Tutorials’ Category

Comments

Sunday, January 11th, 2009

JavaScript has support for a couple of variations of comments, comments are commonly used for a couple of purposes.

1. To make your code more readable, by commenting what a function does for example can aid the programmer at a later date when you may go back and look at your original code.

2. To prevent execution of a particular line(s) of code, you may have a debug function that you use but when it comes to a final release rather than removing it completely just comment it out.

Lets look at the various forms of comments

Single Line Comments

Single line comments begin with //

//this will print out a message to the browser
document.write(”This is a message”);
document.write(”This is a message”); // this is another message

Multi Line Comments

Multi line comments start with a /* and end with a */

/*
This is a multi line comment example
This will print multiple
*/
document.write(”<p>This is a message</p>”);
document.write(”<p>This is a message</p>”);
document.write(”<p>This is a message</p>”);
document.write(”<p>This is a message</p>”);

Comment out a line of code

document.write(”<p>This is a message</p>”);
document.write(”<p>This is a message</p>”);
//document.write(”<p>This is a message</p>”);

The bottom message will not be printed

Comment out a block of code

/*
document.write(”<p>This is a message</p>”);
document.write(”<p>This is a message</p>”);
document.write(”<p>This is a message</p>”);
*/

JavaScript methods

Saturday, January 10th, 2009
Method Object Description
abs(x) Math absolute value of a number
acos(x) Math Arccosine
alert(text) Window produces a dialog box with the
report “Text” and a OK-Button
asin(x) Math Arcsines
atan(x) Math Arctangents
atan2(x,y) Math supply the angle between the connection
line form the coordinate origin to the point (x,y)
and the x-axis
back() History loads the previous URL of the
History list
big() String corresponds with the HTML Tag
BIG
blink() String corresponds with the HTML Tag
BLINK
blur() Password, Select, Text, Textarea removes the focus of the concerning
object
bold() String corresponds with the HTML-Tag
B
ceil(x) Math grows to the next big complete
Number
charAt(n) String supplies the (n-1)st letter of
the string
clearTimeout(…) Frame, Window deletes a Timeout-setting, which
has been supplied before with the Timeout- Method
click() Button, Checkbox, Radio, Reset,
Submit
simulates a mouse click onto the
allocated object
close() Document, Window closes the concern object
confirm(text) Window supplies a dialog box with the
report “Text”, as well as a OK- Button and
a Cancel-Button
cos(x) Math Cosines
current History the URL of the current entry of
the History-List
eval(…) (all objects) executes the as argument of the
function declared sign string as JavaScript-Code
exp(x) Math Exponential function
fixed() String corresponds with the HTML-Tag
TT
floor(x) Math grows to the next smaller complete
Number
focus() Password, Select, Text, Textarea puts the focus onto the concerned
object
fontcolor(…) String corresponds with the HTML-Tag
FONT COLOR=
fontsize(…) String corresponds with the HTML-Tag
FONT SIZE=
forward() History loads the next url in the History-List
getDate() Date supplies the Day of the Month
of the concerned date object
getDay() Date supplies the Day of the concerned
date-object
getHours() Date supplies
the hour of the concerned date-object
getMinutes() Date supplies the minute of the concerned
date-object
getMonth() Date supplies the month of the concerned
date-object
getSeconds() Date supplies the second of the concerned
date-object
getTime() Date supplies the Number which represents
the time of the concerned date-object
getTimezoneOffset() Date supplies the time zone-shift of
the current position
getYear() Date supplies the year of the concerned
date-object
go(nURL) History loads a URL of the History-List
indexOf(…) String searches the Position of letters
or letter-groups inside a string
italics(…) String corresponds with the HTML-Tag
I
javaEnabled() Navigator specifies the fact, if the carrying
out of Java-Applets is possible or not
join(…) Array unites all elements of a array
in a string. the string, specified in the argument,
serves here the separate-sign
lastIndexOf(…) String see IndexOf(…)
link(…) String produces a Hypertext-Link
log(x) Math natural logarithm
max(x,y) Math supplies the bigger of two values
min(x,y) Math supplies the smaller of two values
open() Document, Window opens the concerned object
parse() Date calculates the difference of a
date to the 1. Jan 1990 0 Hour in Milliseconds
pow(x,y) Math x ^ y
prompt(…) Window supplies a dialog box in a input
field
random() Math supplies a random-number between
0 and 1
refresh() Plugins makes new installed Plugins available
reload(URL) Location forces a renewed loading of the
current document
replace(URL) Location replaces the current entry in
the History-List with the specified URL
reset() Form simulates a mouse click at the
Reset-Switch of the concerned form
reverse Array turns back the indication-order
of the array
round(x) Math grows to the nearest complete
number
select() Password, Text, Textarea serves the Choice of the Position,
on this its possible to make changes at the User-inputs
setDate(…) Date places the day of the Month of
the concerned date-object
setHours(…) Date places the hour of the concerned
date-object
setMinutes(…) Date places the minute of the concerned
date-object
>setMonth(…) Date places the month of the concerned
date-object
setSeconds(…) Date places the second of the concerned
date-object
setTime(…) Date places the Time of the concerned
date-object
setTimeout(…) Frame, Window carries out a instruction, after
a prescribed time is passed
setYear(…) Date places the year of the concerned
date-object
sin(x) Math Sinus
small(…) String corresponds with the HTML-Tag
SMALL
sort() Array sorts the elements of the array
split(…) String reduces the concerned string in
sub strings, which are stored as elements of a array.
the in the argument specified string serves here as
a sign-sign
sqrt(x) Math square root
strike(…) String corresponds with the HTML-Tag
STRIKE
sub(…) String corresponds with the HTML-Tag
SUB
submit() Form has the same effect as a mouse
click to a Submit-button of a form
substring(n1,n2) String forms a sub string, whose positions
prescribes two numbers in the original string
sup(…) String corresponds with the HTML-Tag
SUP
taintEnabled() Navigator gives information whether the
date-tainting is active or not
tan(x) Math Tangent
toGMTString() Date converts a date-object to a string
with the look at the GMT-Convention
toLocalString() Date converts the date-object to a
string with the look at the current position
toLowerCase(…) String produces a string which contains
only small letters
toString() (all objects) produces a string which represent
the concerned object
toUpperCase(…) String produces a string which contains
only capital letters
UTC(…) Date supplies the time which is passed
since the 1. Jan 1970 00:00 Hour until the date-object
in the argument (in milliseconds)
valueOf() (all objects) supplies the value of the concerned
object
write(…) Document distributes a text in the browser
window
writeln(…) Document distributes a text with a closing
line-formatting in the browser window

Operators

Saturday, January 10th, 2009
operator Meaning
x+=y x=x+y
x-=y x=x-y
x*=y x=x*y
x/=y x=x/y
x%=y x=x%y supplies the remainder-value
of a division
x++ x=x+1
x– x=x-1
y=x++ x = x and then increments it by
1
y=++x x increments
by 1 and afterwards y =x
y=x– y =x and afterwards x decrements
by 1
y=–x x decements by 1 and afterwards
y=x
-x x =-x
&& logical and
|| logical or
! logical not
== Comparison operator “is equal”
!= Comparison operator “is not
equal”
> Comparison operator “greater
than”
< Comparison operator “less than”
>= Comparison operator “greater
than or equal to”
<= Comparison operator “less than
or equal to”

JavaScript event handlers

Saturday, January 10th, 2009
Event Handler relevant HTML tags
onAbort <img>
onBlur <a>
<area>
<body>

<button>
<frameset>
<input>

<label>
<select>
<textarea>

onChange <input>
<select>
<textarea>
onClick majority of tags
onDblClick majority of tags
onError <img>
onFocus <a>
<area>
<body>

<button>
<frameset>
<input>

<label>
<select>
<textarea>

onKeyDown majority of tags
onKeyPress majority of tags
onKeyDown majority of tags
onLoad <body>
<frameset>

<img>

onMouseDown most tags
onMouseMove most tags
onMouseOver most tags
onMouseUp most tags
onReset <form>
onSelect <input>
<textarea>
onSubmit <form>
onUnload <body>
<frameset>

Regular expressions

Saturday, January 10th, 2009

JavaScript 1.2 supports regular expressions , if you have programmed in PERL you are in luck they both use the same syntax . A regular expression is specified as a sequence of characters within forward slashes / . They can also be a JavaScript string passed to the RegExp( ).

There are 2 optional modifiers that you can use and these are g ( for global search ) and i ( which stands for case insensitive search ) . These modifiers follow the second / or are passed to the RegExp( ).

Character what it means
\n , \r , \t matches newline , carriage return and
tab
\\ , \ / , \* , \+ , |? , etc etc etc matches a special character , ignores
its special meaning
[ . . . . . . ] matches anyone of the characters between
the brackets
[^ . . . . . ] matches any character not between the
brackets
. match any character other than a new line
\w , \W match any word / non word character
\s , \S match any whitespace / non whitespace
\d , \D match any digit / non digit
^ , $ require match at beginning / end of string
or if in multiline ode beginning / end of a line
\b , \B require match at a word boundary / non
boundary
? optional term , match 0 or 1 times
+ match previous term one or more times
* match term zero or more times
{n} match previous term exactly n times
{n,} match previous term n or more times
{n,m} match at least n times but no more then
m times
a | b match either a or b
(sub) group sub expression sub into a single
term and remember text that it matched
n match exactly the same characters that
were matched by subexpression number n
$n in replacement strings , substitute the
text that matched the nth expression
Translate