You are on page 1of 12

TRUNG TM O TO LP TRNH WEB & HA

VIETPRO EDUATION

BI GING JAVASCRIPT (BUI 1)


NHP MN LP TRNH JAVASCRIPT
Ging vin: Hong Minh Tun

TNG QUAN V NGN NG JAVASCRIPT


1. Javascript l g
- Javascript l mt ngn ng lp trnh nh, c thit
k to ra cc tng tc vo cc trang HTML, to
nn cc hiu ng v ng dng mt m cho web.
2. Javascript s dng vi mc ch g
- To cc hiu ng cho trang web
- To cc tng tc gia ngi dng v website
- Thay i ni dung vn bn HTML

S DNG JAVASCRIPT TRONG VN BN HTML


1. Nhng m Script vo vn bn HTML
- To vng vit m (Lm vic) vi Javascrip theo c
php sau:
- <script language=javascript> ... </script>
2. Xut ni dung bng Javascript
- Hin th ni dung trong Javascript cn thng qua cu
lnh vi c php sau:
- document.write(Ni dung mun hin th);
3. Comment trong Javascript
- /* Ni dung mun Comment */
4. a mt file Javascript t bn ngoi vo
- Ly ni dung ca File Jacascript bn ngoi a vo
vn bn HTML tng t nh CSS
- <script type=text/javascript language=javascript
src=URL></script>

JAVASCRIPT GIAO TIP VI NGI DNG


1. Hp thoi Alert
- Hp thoi Alert c s dng cho mc ch Thng
bo hoc Cnh bo,...
- alert(Ni dung mun thng bo);
2. Hp thoi Prompt
- Hp thoi Prompt c s dng cho mc ch tng
tc vi Ngi dng
- Prompt(Ni dung tng tc);

BIN TRONG JAVASCRIPT


1. Khi nim Bin trong Javascript
- Bin l mt on m ngn c s dng lu tr
thng tin trong qu trnh x l
2. Khai bo mt Bin trong Javascript
- var Bin
3. Quy tc t t cho Bin
- Tn bin phi bt u bng ch ci hoc k t gch
di (_), tip theo c th s dng ch ci, ch s v
k t (_) v khng c cha khong trng, du.
- Tn Bin phn bit hoa thng nh x s khc vi X
- Tn Bin phi c ngha & Lin quan n ni dung m
n lu tr.
- Tn Bin Di th nhng ch ci u tin ca mi t
tip theo nn vit hoa, cn li l vit thng
4. In ra gi tr ca Bin
- document.write(tenBien);

CC KIU D LIU S DNG TRONG JAVASCRIPT


1. D liu kiu Nguyn (Interger)
- D liu kiu Nguyn l cc ch s thuc hng S
Nguyn trong S hc
2. D liu kiu thc (Float)
- D liu kiu Thc l cc ch s thuc hng S Thc
trong ton hc i S
3. D liu kiu chui (String)
- Kiu chui c biu din bi khng hay nhiu k t
c t trong cp du "... " hay '... '
4. D liu kiu Logic (Boolean)
- Kiu Logic c s dng kim tra 2 trng hp
ng hoc Sai tng ng vi cc gi tr TRUE hoc
FALSE
5. D liu kiu Mng (Array)
6. D liu kiu i tng (Object)

TON T TRONG JAVASCRIPT


1. Ton t Gn
- Ton t Gn bao gm mt k t n =. Ton t ny s
ly gi tr ca Ton hng bn phi n gn cho Ton
hng bn tri n.
- tenBien = Gi tr c gn;
2. Ton t Ni
- Ton t Ni bao gm mt k t n (.). Ton t ny s
dng ni cc kiu d liu vi nhau.
- Bien = Chui . Bien2;

TON T TRONG JAVASCRIPT


3. Ton t S hc
- L dng php tnh gin n cng, tr, nhn, chia trong
s hc. Ngoi ra cn c php chia ly d (%). c s
dng ly ra n v d ca 1 php ton.

Ton t

M t

V d

Php ton cng

A+B

Php ton tr

A-B

Php ton nhn

A*B

Php ton chia

A/B

Php ton chia ly phn d

A%B

TON T TRONG JAVASCRIPT


4. Ton t So snh
- L ton t c s dng thc hin cc php ton
so snh gia hai s hng. Thng thng hay c s
dng trong cc biu thc iu kin v vng lp

Ton t

M t

V d

==

So snh bng

(A == 10)

!=

Khng bng

(A != 10)

>

So snh ln hn

(A > 10)

>=

So snh ln hn hoc bng

(A >= 10)

<

So snh b hn

(A < 10)

<=

So snh b hn hoc bng

(A <= 10)

TON T TRONG JAVASCRIPT


5. Ton t Logic
- Ton t logic l cc t hp cc gi tr boolean. Thng
thng hay c s dng trong cc biu thc iu
kin v vng lp

Ton t

M t

V d

&&

V (And)

(A == 10 && B == 15)

||

Hoc (Or)

(A == 5 || A == 8)

Ph nh (Not)

!(A == 10)

TON T TRONG JAVASCRIPT


6. Ton t Kt hp
- Khi to m Javascript, chng ta s thng nhn thy
cn phi tng hoc gim lng bin mt s nguyn
no . Bn s thng thc hin iu ny khi chng ta
m 1 gi tr no trong vng lp.
Ton t

V d

M t

++

A++

Tng ng vi: A = A + 1

--

A--

Tng ng vi: A = A - 1

+=

A += B

Tng ng vi: A = A + B

-=

A -= B

Tng ng vi: A = A - B

*=

A *= B

Tng ng vi: A = A * B

/=

A /= B

Tng ng vi: A = A / B

THC HNH
I BI TP THC HNH
1. Bi 1
2. Bi 2
II BI TP V NH
1. Bi 1
2. Bi 2