Date Object: Javascript Quick Reference Card
Date Object: Javascript Quick Reference Card
1.01
Copyright, 2007 Brandsatch !!C
http"##$$$.e%p&ainth.at
Co&or key over&eaf
Code Structure
var ...
//Global variable declarations
function funcA([para'1,para'2,...])
(
var ...
//Local variable declarations visible in nested
functions
[function innerFuncA)[ipara'1,ipara'2...]*
(
var ...
//Variables local to innerFuncA
//your code here
+]
aName,-.%p&ain/hat0-1
//implicit global variable creation
//your code here
+
Nomenclature Rules
2unction and varia3&e na'es can consist of any 45C66
or nu'eric character. 7 and 8 are a&&o$ed. /he first
character cannot 3e nu'eric. .%tended 45C66
characters are a&&o$ed. /here is no practica& &i'it on
na'e &ength. 9a'es are case:sensitive.
6f t$o or 'ore varia3&es, t$o or 'ore functions or a
varia3&e ; a function are dec&ared $ith the sa'e na'e
the &ast dec&aration o3&iterates a&& previous ones.
Visibility & Scope
<aria3&es assign'ents $ithout the use of the var
key$ord resu&t in a ne$ g&o3a& varia3&e of that na'e
3eing created.
<aria3&es dec&ared $ith the var key$ord out$ith the
3ody of a function are g&o3a&. <aria3&es dec&ared $ith
the var key$ord inside the 3ody of a function are &oca&
to that function. !oca& varia3&es are visi3&e to a&& nested
functions.
!oca& entities hide g&o3a&s 3earing the sa'e na'e.
Variable ypes
strin!" var s # -e%p&ainthat- or =e%p&ainthat>
number" var n # ?.1@1AB, 100, 0...
boolean" var fla! , fa&se or true
ob$ect" var d , ne$ Cate)*1
function" var %reet # function sayDe&&o)* (a&ert)-De&&o-*+
Javascript is a $eak&y typed &anguage E i.e. a si'p&e
assign'ent is sufficient to change the varia3&e type. /he
typeof key$ord can 3e used to check the current
varia3&e type.
Special Values
/he specia& va&ues false& 'nfinity& NaN& null& true ;
undefined are recogniFed. null is an o3Gect. 'nfinity
and NaN are nu'3ers.
(perators
(perator )*ample Result
H ? H 2
-e%p&ain- H -that-
A
e%p&ainthat
+ ? : 2 :1
I ?I2 J
# ?#2 1.A
K ?K2 1
HH i , 21
iHH
,
, HHi
-
?
:: i , 21
i::
,
, ::i
-
1
,,
,,
? , -?-
2 ,, ?
true
fa&se
,,, ? ,,, ?
? ,,, -?-
true
fa&se
L 2 L ?
-a- L -4-
true
fa&se
L, 2 L, ? true
M 2 M ? fa&se
M, 2 M ? fa&se
, i , 2 i is assigned
the va&ue 2
H, iH,1 ?
:, i:,1 2
iI, iI,? J
#, i#,2 ?
K, iK,2 1
i , 21G , A1
;; )49C* )i L, 2* ;; )G L 7* true
NN )OR* )iK2 M 0* NN )GK2 ,, 0* fa&se
. )9O/* )i,,2* ;; .)GK2 ,, 0* true
i , 21G , 71
; )3it$ise* i ; G 2
N )3it$ise* iNG 7
P)QOR* iPG A
LL 2LL1 @
MM 2MM1 1
MMM i,10 )3inary 1010*
iMMM2
2
?
'nternal Functions
decode/R' : reverses encodeRR6
decode/R'Component : reverses
encodeRR6Co'ponent
encode/R' 0 encodes RR6 3ut not the reserved
characters, i.e. "12&3
encode/R'Component 0 encodes everything
escape 0 he%adeci'a& string encoding.
unescape E reverses escape
eval 0 eva&uates Javascript e%pressions
isNaN 0 true if the argu'ent is not a nu'3er.
isFinite 0 is2inite)2#0* returns fa&se
parse'nt : parse6nt)?1.AS* returns ?1
parseFloat : parse2&oat)?1.AS* returns ?1.A
Array (b$ect
len!t4 E nu'3er of e&e'ents in the array
concat E concatenates argu'ent, returns ne$ array.
$oin E returns e&e'ents as a string separated 3y
argu'ent )defau&t is &*
pop E suppress ; return &ast e&e'ent
pus4 E adds ne$ e&e'ents to end of array ; returns
ne$ len!t4.
reverse E inverts order of array e&e'ents
s4ift E suppress ; return first e&e'ent
slice E returns array s&ice. 1
st
arg is start position. 2
nd
arg
is &ast position H 1
sort E a&phanu'eric sort if no argu'ent. ass sort
function as argu'ent for 'ore specificity.
splice E discard and rep&ace e&e'ents
uns4ift E append e&e'ents to start ; return ne$ len!t4
5ate (b$ect
!et6
!et/C6
set6
set/C6
$here T is one of Cate, Cay, 2u&&Uear, Dours,
Vi&&iseconds, Vinutes, Vonth, 5econds, /i'e,
/i'eFoneOffset
to5ateStrin! E the date in .ng&ish.
to%7Strin! E the date ; ti'e in .ng&ish.
to8ocale5ateStrin! E the date in the &oca&e &anguage.
to8ocaleStrin! E date ; ti'e in the &oca&e &anguage.
to8ocaleimeStrin! E ti'e in the &oca&e &anguage.
toimeStrin! E ti'e in .ng&ish.
to/CStrin! E date ; ti'e in R/C, .ng&ish
value(f E 'i&&iseconds since 'idnight 01 January 1B70,
R/C
7at4 (b$ect
), 8N,9, 8N-, 8(%,9), 8(%-), :', S;R,<-, S;R-
abs E a3so&ute va&ue
6(n) : trigono'etric functions
a6(n) : inverse trigono'etric functions
$here 6 is one of cos, sin or tan
ceil(n) E s'a&&est $ho&e nu'3er M, n
e*p(n) E returns e
n
floor(n) 0 3iggest $ho&e nu'3er L, n
lo!(n) 0 &ogarith' of n to the 3ase e
ma*(n,&n-) E 3igger of n, and n-
min(n,&n-) E s'a&&er of n, and n-
po=(a&b) : a
b
random E rando' nu'3er 3et$een 0 and 1
round(n) E n rounded do$n to c&osest integer
s>rt(n) E sWuare root of n
Number (b$ect
7A?<VA8/) : ca 1.7B77.H?0X
7'N<VA8/) E ca A.:?2@
N)%A'V)<'NF'N'@& :(S''V)<'NF'N'@
nAto)*ponential(m) E n in scientific notation $ith m
deci'a& p&aces.
nAtoFi*ed() : n rounded to the closest $ho&e nu'3er.
nAto:recision(m) E n rounded to m figures.
De%adeci'a& nu'3ers are designated $ith the prefi%
9* or 9?A e.g. 0%22 is the nu'3er 2AA.
Strin! (b$ect
len!t4 E nu'3er of characters in the string
sAc4arAt(n) E returns s[n]. n starts at 0
sAc4arCodeAt(n) E Rnicode va&ue of s[n]
sAfromC4arCode(n,&n-AA) : string 3ui&t fro' Rnicode
va&ues n,, n-...
s,Ainde*(f(s-&n) E &ocation of s- in s, starting at
position n
s,Alast'nde*(f(s-) E &ocation of s- in s, starting fro'
the end
sAsubstr(n,& n -) E returns su3string starting fro' n, upto
character preceding n-. 9o n- , e%tract ti&& end. n, L 0 ,
e%tract fro' end.
sAto8o=erCase() : returns s in &o$er case characters
sAto/pperCase() : care to guessY
)scape Se>uences
Bn : ne$ &ine, Br : carriage return,
Bt E ta3 character, BB : Z character
Javascript Quick Reference Card
1.01
Z- : apostrophe Z-- : Wuote
Zu9999 E Rnicode character at 999
e.g. Zu2AB4 gives the character [
Cavascript in D78
)*ternal Cavascript
Lscript type,>te%t#Gavascript> defer,>defer>
src,>#scripts#e%p&ainthat.Gs>ML#scriptM
'nline Cavascript
Lscript type,>te%t#Gavascript>M
##your code here
L#scriptM
Comments
1E Co''ents spanning 'u&tip&e &ines E1
11 5i'p&e, sing&e &ine, co''ent
Conditional )*ecution
if )Condition* CodeIfTrue1e&se CodeIfFalse
@
Vu&ti&ine Code6fT 'ust 3e p&aced in 3races, FG
s=itc4 )variable*
(
case <a&ue1"Code1
breaH1
case <a&ue2"Code1
3reak1
.....
default"Code;
+
variable can 3e 3oo&ean, nu'3er, string or even date.
)condition*2(CodeIfTrue)"(CodeIfFalse)
arentheses are not necessary 3ut advisa3&e
)rror Dandlin!
7et4od ,"/he onerror event
Iscript type#Jte*t1$avascriptJK
function whenError)'sg,ur&,&ine9o*F
//use parameters to provide meaningful messages
G
=indo=Aonerror # whenError
I1scriptK
&ace this code in a separate IscriptKAAI1scriptK tag
pair to trap errors occurring in other scripts. /his
techniWue 3&ocks errors $ithout taking corrective action.
7et4od -"/he try..catc4AAfinally state'ent
function showLogValue)nu'*F
var s = 'No Error';
try
Fif )nu' L 0* t4ro= -3adnu'-1
if )nu' ,, 0* t4ro= -Fero-1 G
catc4 )err*
F s , err1
s=itc4 )err* F
case -3adnu'-"nu' , :nu'1
breaH1
case -Fero-"nu' , 11
breaH1 G
G
[finallyF alert)\s,Vath.&og)nu'*]*1G]
G
/he fina&&y 3&ock is optiona&. /he t$o techniWues can 3e
used in concert.
8oopin!
function whileLoop)nu'*F
=4ile )nu' M 0*
F alert)nu'*1
nu'::1G
G
function doLoop)nu'*F
doF
alert)nu'*1
nu'::1
G=4ile )nu' M 0*1
G
function forLoop)nu'*F
var i1
for )i,01iLnu'1iHH*F
alert)nu'*1
G
G
breaH causes i''ediate ter'ination of the &oop.
&oop state'ents after continue are skipped and the ne%t
e%ecution of the &oop is perfor'ed.
function for6n!oop)*F
var s,%1
for )% in document*
F
s,% H - , - H document\%]1
alert)s*1
G
G
/his code is 3est tested in Opera $hich offers the option
of stopping the script at each a&ert. 6n p&ace of
document any Javascript o3Gect or an array can 3e
used to &oop through its properties#e&e'ents.
return
return causes i''ediate ter'ination of the Javascript
function. 6f no va&ue is returned, or if return is 'issing
the function return type is undefinedA
document (b$ect
body : the 3ody of the docu'ent
cooHie : read#$rite the docu'ent cookies
domain E $here $as the docu'ent served fro'Y
forms[] : array of a&& for's in the docu'ent
ima!es[] : array of a&& i'ages in the docu'ent
referrer E $ho pointed to this docu'entY
/R8 E the RR! for the docu'ent
!et)lementLy'd(id) E e&e'ent 3earing 6C of id
!et)lementsLyName(n) E array of e&e'ents na'ed n
!et)lementsLya!Name(t) : array of t tagged
e&e'ents
=rite E $rite p&ain or D/V! te%t to the docu'ent
onload E occurs $hen the docu'ent is &oaded
onunload E occurs $hen user 3ro$ses a$ay, ta3 is
c&osed etc.
)lement (b$ect
By e&e'ent $e 'ean any D/V! e&e'ent retrieved using
the document.!et)lementLy6 'ethods.
attributes E a&& e&e'ent attri3utes in an array
className E the C55 sty&e assigned to the e&e'ent
id E the id assigned to the e&e'ent
innerD78 E D/V! content of the e&e'ent
innere*t E content of the e&e'ent shorn of a&& D/V!
tags. Coes not $ork in 2irefo%
offset6 E e&e'ent di'ensions )6 # Dei!4t1Midt4* or
&ocation)6 # 8eft1Ri!4t* in pi%e&s
o=ner5ocument E take a guess
style E C55 sty&e dec&aration
ta!Name E e&e'ent tag type. Curious&y, a&$ays in
uppercase
te*tContent E the 2irefo% eWuiva&ent of innere*t
location (b$ect
4ost E RR! of the site serving up the docu'ent
4ref E the entire RR! to the docu'ent
pat4name E the path to the docu'ent on the host
protocol E the protoco& used, e.g. http
reload(p) : re&oad the docu'ent. 2ro' the cache if p is
true.
replace(url) E rep&ace the current docu'ent $ith the
one at urlA Ciscard docu'ent entry in 3ro$ser history.
screen (b$ect
4ei!4t E screen height in pi%e&s
=idt4 E screen $idth in pi%e&s
=indo= (b$ect
alert(ms!) E disp&ays a dia&og $ith ms!
clear'nterval(id) E c&ears interva& id set 3y set6nterva&
clearimeout(id) E c&ears ti'eout id set 3y set/i'eout
confirm(ms!) E sho$s a confir'ation dia&og
print() : prints the $indo$ contents
prompt(ms!& [ default ] ) E sho$s pro'pt dia&og,
optiona&&y $ith defau&t content. Returns content or null.
set'nterval(e*pr&interval& [ ar!s ] ) E sets repeat at
interval 's. /he function e*pr is eva&uated, optiona&&y
$ith ar!s passed as para'eters.
setimeout(e*pr&time& [ ar!s ] ) !ike set'nterval 3ut
non:repeating.
?78DttpRe>uest (b$ect
var reW , ne= ?78DttpRe>uest() in 2irefo%, Opera
and 6.7.
var reW , ne= Active?(b$ect)=Microsoft.XML!!">* in
6.A,J.
readyState E 0"Rninitiated, 1"!oading, 2"!oaded,
?"6nteractive, @"Co'p&ete
responsee*t E response as te%t string
response?78 E response as QV! o3Gect
status E D// status response, e.g. 200
statuse*t E status as te%t, e.g. O^
abort() : a3andon the D// reWuest
!etResponseDeader(4dr) gets response header
na'ed 4drA
open(met4od&url&async& [ username&pass=ord ] ) E
'ost&y o3vious. 5et async to fa&se to $ait for reWuest to
co'p&ete.
setRe>uestDeader(lbl&value) E sets D// reWuest
header
send(r>st) E sends D// reWuest
onreadystatec4an!e E hand&er to pickup response
$hen reWuest is asynchronous.
Notes
/ested under 6.JH, 2irefo% 1.AH ; Opera B.1H.
,
.va&uates after use
-
.va&uates before use
?
_ero:fi&& right shift
@
9ote the se'ico&on0
Color Codin!
italics user code blue 0 Javascript ^ey$ords
red E Option ob$ect E Javascript COV o3Gect
!reen 0 on&y nu'eric va&ues blue + o3Gect properties
!reen E o3Gect 'ethods ma!enta E o3Gect events
`e have refrained fro' discussing features that are not
avai&a3&e across a&& three 3ro$sers.
4 'ore e%tensive reference to the Javascript COV $i&&
3e provided on a separate Wuick reference card.