Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

i

f(scr
een.
avai
l
Widt
h<650){

/
/def
inet
hest
yleVar
iabl
east
hel
ow-
resol
uti
onst
yle

cssName='
l
owr
es.
css'
;

r
esol
uti
onI
nfo='
l
owr
esol
uti
on'
;

/
/Ori
fthewi
dthoft
hescr
eeni
slesst
han1000pi
xel
s

}el
se{

i
f(scr
een.
avai
l
Widt
h>1000){

/
/def
inet
hest
yleVar
iabl
east
hehi
gh-
resol
uti
onst
yle

cssName='
hi
ghr
es.
css'
;

r
esol
uti
onI
nfo='
hi
ghr
esol
uti
on'
;

/
/Ot
her
wise

}el
se{

/
/def
inet
hest
yleVar
iabl
east
hemi
d-r
esol
uti
onst
yle
cssName='
l
owr
es.
css'
;

r
esol
uti
onI
nfo='
medi
um r
esol
uti
on'
;

document
.wr
it
e('
<l
inkr
el="
Sty
leSheet
"hr
ef="
'+cssName+'
"ty
pe="
text
/css"/
>')
;

</
scr
ipt
>

</
head>

<body
>

<scr
iptt
ype="
text
/j
avascr
ipt
">

document
.wr
it
e('
<p>Appl
i
edSt
yle:
'+r
esol
uti
onI
nfo+'
</p>'
);

</
scr
ipt
>

</
body
>
</
html
>

Al
thoughwe’l
lbelooki
ngatt
hedet
ail
sofi
fst
atement
sandl
oopsi
nthenextchapt
er,
youcan
pr
obablyseehow

t
hisi
swor ki
ngal
ready
.Thei
fst
atementont
hef
ir
stl
i
neaskswhet
hert
hescr
een.
avai
l
Widt
his
l
essthan650:

i
f(scr
een.
avai
l
Widt
h<650)

Ift
heuser
’sscreeni
s640×480,
thewi
dthi
slesst
han650,
sot
hecodewi
thi
nthecur
lybr
acesi
s
execut
edandthe

l
ow-
resol
uti
onst
yleandmessagegetdef
ined.

i
f(scr
een.
avai
l
Widt
h<650){

/
/def
inet
hest
yleVar
iabl
east
hel
ow-
resol
uti
onst
yle

cssName='
l
owr
es.
css'
;

r
esol
uti
onI
nfo='
l
owr
esol
uti
on'
;

Thecodecarri
esonchecki
ngt
hescr
eensi
zeusi
ngt
heel
sest
atement
.Thef
inal
elseoccur
s
onl
yifnei
therof
theotherev
aluat
ionshaver
esul
tedi
ncodebei
ngexecut
ed,
soy
ouassumet
hatt
hescr
eeni
s
800×600anddefinethe

medi
um st
yleandmessageaccor
dingl
y:

el
se{

/
/def
inet
hest
yleVar
iabl
east
hemi
d-r
esol
uti
onst
yle

cssName='
l
owr
es.
css'
;

r
esol
uti
onI
nfo='
medi
um r
esol
uti
on'
;

ht
tp:
//www.
fr
eepdf
-books.
com/

Not
ethatyou’
remeasur
ingt
hescr
eensi
zeher
e,andt
heusermi
ghthav
ea800×600scr
een,
but
t
hatdoesn’
t

meanhi
sbr
owserwi
ndowi
smaxi
miz
ed.Youmi
ghtbeappl
yi
ngast
ylet
hati
snotappr
opr
iat
e.

You’
reusi
nganot
herobj
ect
,thedocumentobj
ect
,towr
it
etot
hepage(
anHTMLdocument
).
Thedocument

obj
ect’
swri
te(
)met
hodal
l
owsy
out
oinser
tHTMLi
ntot
hepage.Not
ethatdocument
.wr
it
e()
doesn’
tact
ual
ly
changet
hesour
ceHTMLpage,
justt
hepaget
heuserseesonhi
scomput
er.

■Note I nf
act
,you’
ll
finddocument
.wr
it
e()v
eryusef
ulasy
ouwor
kthr
ought
hef
ir
stf
ew
chapt
ersoft
hebook.It
’s

goodf
orsmallexampl
est
hatshowhowascr
ipti
swor
king,
forcommuni
cat
ingwi
tht
heuser
,
andev
enfordebuggi
ngan

ar
eaofapr
ogram t
hatyou’
renotsur
eisdoi
ngwhaty
out
hinki
tshoul
dbedoi
ng.I
tal
sowor
ks
onal
lbr
owser
sthatsuppor
t

Jav
aScri
pt.Moremodernbrowser
shav
ebet
tert
ool
sandmet
hodsf
ordebuggi
ng,
butI

llsay
moreonthatl
ateri
nthebook.

Youusedocument
.wr
it
e()t
owr
it
eoutt
heappr
opr
iat
eli
nkel
ementwi
thy
ourdef
inedst
ylei
nthe
head:

document
.wr
it
e('
<l
inkr
el="
Sty
leSheet
"hr
ef="
'+

cssName+'
"ty
pe="
text
/css"/
>')
;

Andint
hedocument
’sbody
,youwr
it
eoutt
hemessageexpl
aini
ngwhi
chr
esol
uti
onst
ylewas
appl
i
ed:

<scr
iptt
ype="
text
/j
avascr
ipt
">

document
.wr
it
e('
<p>Appl
i
edSt
yle:
'+r
esol
uti
onI
nfo+'
</p>'
);
</
scr
ipt
>

Lateron,
we’l
lworkwi
thmor
ecompl
exexampl
est
hatuseJav
aScr
iptt
otestcapabi
l
iti
esoft
he
user’
sagentand

i
nter
face.Fornow,
though,
Ihopet
hissi
mpl
eexampl
egi
vesy
ouani
nkl
i
ngoft
heki
ndof
f
lexi
bil
i
tyyoucanaddto

y
ourwebpagesusi
ngJav
aScr
ipt
.

Summar
y

I
nthischapter
,wetookal
ookatwhatJav
aScr
ipti
s,howi
twor
ks,
andwhati
tsadv
ant
agesand
di
sadvantagesar
e.

Inot
edthatthebi
ggestdi
sadv
ant
agei
sthaty
oucannotr
elyoni
tasagi
ven.Howev
er,
Ial
so
menti
onedthatusi
ng

Jav
aScr
iptcanmakewebsi
tesani
cerandsl
i
ckerexper
iencef
ory
ouruser
s.

YouransomeJavaScr
iptcode,
sawhowt
oaddcomment
stot
hecode,
andsawhowt
o
separ
ateJav
aScr
ipt

st
atementsusi
ngsemicol
ons.Youal
sosawt
haty
oucant
ell
Jav
aScr
iptt
otr
eatagr
oupofl
i
nes
ofcodeasasingl
ebl
ock

usi
ngcur
lybr
aces,
fol
l
owi
ngani
fst
atement
,forexampl
e.Youl
ear
nedt
hatJav
aScr
ipt
execut
iongener
all
yrunsf
rom

toptobot
tom,andf
rom t
hef
ir
stscr
iptbl
ockt
othel
ast
,wi
tht
heexcept
ionoff
unct
ionst
hat
execut
eonlywheny
ou

t
ell
them t
o.

Youal
sol
ookedatobj
ect
s,whi
char
ecent
ral
towr
it
ingJav
aScr
ipt
.Notonl
yisJav
aScr
ipti
tsel
f
ver
ymuch

dependentonobject
s,butt
hebr
owseral
sousesobj
ect
sandmet
hodst
omakei
tsel
fandt
he
documentavai
lablefor

scr
ipti
ng.Fi
nal
ly
,youl
ookedatasi
mpl
eexampl
ethatr
eadsoutt
heuser
’sscr
eenr
esol
uti
onand
appli
esasuit
abl
e

st
ylesheet
.

I
nthenextchapter
,I’
l
lcov
ert
hel
anguagef
undament
alsofJav
aScr
ipt
.You’
l
lseehow
Jav
aScri
ptstor
esand

mani
pul
atesdat
a,andusesi
tincal
cul
ati
ons.We’
l
lal
sol
ookatcr
eat
ing“
int
ell
i
gent
”Jav
aScr
ipt
pr
ogr
amsusing

deci
si
on-
maki
ngstat
ement
sthatal
l
owy
out
oev
aluat
edat
a,docal
cul
ati
onswi
thi
t,anddeci
de
onanappr
opr
iat
e

cour
seofact
ion.Wi
tht
hatchapt
erundery
ourbel
t,y
ou’
l
lhav
emostoft
hef
undament
al
knowl
edgeneededt
ogoon
t
omor
eexci
ti
ngandusef
ulwebpr
ogr
ammi
ng.

You might also like