Tải bản đầy đủ

L6 javascript

8/2/2010

Bài 6.
JavaScript

Ĉӛ%i/kP
9LӋQ&17777- Ĉ+%.+1
1

0өFÿtFK
‡ 9tGөYӅ-DYDVFULSW
‡ &XQJFҩSFKRVLQKYLrQQKӳQJNLӃQWKӭFYӅ
QJ{QQJӳ-DYD6FULSWÿӇWҥRUDVӵWѭѫQJWiFFӫD
trang Web

2

1


8/2/2010


1ӝLGXQJ
‡
‡
‡
‡
‡
‡
‡
‡
‡

*LӟLWKLӋXYӅ-DYD6FULSW
9DLWUzFӫD-DYD6FULSW
&KqQPmOӋQK-DYD6FULSW
&iFWKjQKSKҫQFѫEҧQ
&ҩXWU~FÿLӅXNKLӇQ
;ӱOêVӵNLӋQYӟL-DYD6FULSW
0ӝWVӕÿӕLWѭӧQJWURQJ-DYD6FULSW
7UҳFQJKLӋP
7KӵFKjQK

3

*LӟLWKLӋXYӅ-DYD6FULSW
‡ /êGRUDÿӡL
± +70/;+70/OjFiFÿӏQKGҥQJWjLOLӋXFyWKӇ
ÿѭӧFVӱGөQJÿӇWUDRÿәLGӳOLӋXWUrQPҥQJ
Internet
± &iF:HESDJHQJj\FjQJ\rXFҫXWtQKViQJWҥR
ÿӝFÿiRÿӇKҩSGүQQJѭӡLVӱGөQJ
± => JavaScript và Visual Basic Script

4

2


8/2/2010


*LӟLWKLӋXYӅ-DYD6FULSW
‡ JavaScript = Java + Script?
‡ Script?
± 1J{QQJӳNӏFKEҧQ
± /jPӝWQJ{QQJӳOұSWUuQKFKRSKpSWҥRUDVӵÿLӅX
NKLӇQWURQJPӝWӭQJGөQJNKiF
± &iFQJ{QQJӳNӏFKEҧQWKѭӡQJÿѭӧFWK{QJGӏFK

5

*LӟLWKLӋXYӅ-DYD6FULSW
‡ 0өFWLrXFӫD-DYD6FULSW
± &XQJFҩSFKRQJѭӡLSKiWWULӇQ:HENKҧQăQJWҥR
UDVӵWѭѫQJWiFWURQJPӝWWUDQJ:HE
± 0m-DYD6FULSWFyWKӇQK~QJWURQJWjLOLӋX+70/
ÿӇÿLӅXNKLӇQQӝLGXQJWUDQJ:HEYjKӧSOӋGӳOLӋX
QJѭӡLVӱGөQJQKұSYjR

‡ .ӃWOXұQ
± -DYD6FULSWOjPӝWQJ{QQJӳNӏFKEҧQKѭӟQJÿӕL
WѭӧQJFKRYLӋFSKiWWULӇQFiFӭQJGөQJ,QWHUQHW
phía Client và Server.
6

3


8/2/2010

9DLWUzFӫD-DYD6FULSW
‡ &XQJFҩSVӵWѭѫQJWiFYӟLQJѭӡLGQJ
± 6ӵNLӋQGRQJѭӡLGQJWҥRUDGLFKXӝWFOLFN
FKXӝW«
± 6ӵNLӋQGRKӋWKӕQJWҥRUDWKD\ÿәLNtFKWKѭӟF
WUDQJWҧLWUDQJ«

‡ 7KD\ÿәLÿӝQJQӝLGXQJ
± 7KD\ÿәLQӝLGXQJYjYӏWUtFiFWKjQKSKҫQWUrQ
WUDQJ:HEWKHRVӵWѭѫQJWiFFӫDQJѭӡLGQJ

7

9DLWUzFӫD-DYD6FULSW
‡ +ӧSOӋKyDGӳOLӋX
± .LӇPWUD[HPGӳOLӋXFyKӧSOӋNK{QJWUѭӟFNKLQy
ÿѭӧFJӱL VXEPLW
ÿӃQ:HE6HUYHUÿӇ[ӱOê

8

4


8/2/2010

3. Chèn mã JavaScript vào Web Page
‡ 9LӃWWUӵFWLӃSWURQJWjLOLӋXVӱGөQJWKҿ6FULSW
‡ /LrQNӃWÿӃQPӝWILOH-DYD6FULSW

9

9LӃWWUӵFWLӃSWURQJWjLOLӋX
‡ Cú pháp
VFULSWODQJXDJH ³-DYD6FULSW´!


‡ &yWKӇ[iFÿӏQKFөWKӇSKLrQEҧQ-DYD6FULSW
‡ 0ӛLOӋQKNӃWWK~FEҵQJGҩX
10

5


8/2/2010

9LӃWWUӵFWLӃSWURQJWjLOLӋX







11

9LӃWWUӵFWLӃSWURQJWjLOLӋX

12

6


8/2/2010

6ӱGөQJILOHErQQJRjL
‡ Cú pháp
VFULSWODQJXDJH ³-DYD6FULSW´
VUF ³ILOHQDPHMV´!

‡ )LOH-DYD6FULSW MV
FKӍEDRJӗPPm
-DYD6FULSWNK{QJFKӭDWKҿ+70/

13

6ӱGөQJILOHErQQJRjL
HTML file: external.html


VFULSWODQJXDJH MDYDVFULSW³
VUF ³WHVWMV´!





JavaScript file: test.js
GRFXPHQWZULWH ³:HOFRPHWR-DYD6FULSW´
14

7


8/2/2010

6ӱGөQJILOHErQQJRjL

15

6ӱGөQJILOHErQQJRjL
‡ So sánh
± 0m-DYD6FULSWÿѭӧFFKqQWUӵFWLӃSFyWKӇÿһWӣEҩW
NuQѫLÿkX
‡ ĈһWӣ+HDG
‡ ĈһWӣ%RG\

± )LOH-DYD6FULSWErQQJRjLFKRSKpSVӱGөQJOҥL
QKLӅXOҫQYjFKӍQKVӱDGӉGjQJ

16

8


8/2/2010

&iFWKjQKSKҫQFѫEҧQFӫD-DYD6FULSW
‡
‡
‡
‡
‡
‡
‡
‡

%LӃQ
.LӇXGӳOLӋX
4.3. Phép toán
%LӇXWKӭF
+ӝSWK{QJEiR
4.6. Chú thích
&iFNtWӵÿһFELӋW
4.8. Hàm
17

%LӃQ
‡ /jÿҥLOѭӧQJFyWKӇWKD\ÿәLJLiWUӏWURQJ
FKѭѫQJWUuQK
‡ 4X\WҳFÿһWWrQ
± 7rQSKҧLEҳWÿҫXEҵQJNtWӵKRһFGҩXJҥFKGѭӟL
± 7rQFyWKӇFKӭDVӕ
± -DYD6FULSWSKkQELӋWFKӳKRDYjFKӳWKѭӡQJ

18

9


8/2/2010

%LӃQ
‡ .KDLEiRELӃQ
± Cú pháp: YDUWrQBELӃQ
± .KӣLWҥRJLiWUӏFKRELӃQFQJNKDLEiRKRһFNKӣL
WҥRJLiWUӏVDX
var A=10;
KRһF
var A;
A =10;
± .KDLEiRQKLӅXELӃQFQJGzQJ
19

%LӃQ
‡ 3KҥPYLFӫDELӃQ
± ĈѭӧF[iFÿӏQKEӣLQѫLELӃQÿѭӧFNKDLEiR

Script
Global variable
Function 1
Local variable

Function 2
Local varibale
20

10


8/2/2010

%LӃQ
‡ %LӃQWRjQFөF
± Khai báo bên ngoài hàm
± ĈѭӧFWUX\FұSWӯPӑLQѫLWURQJNӏFKEҧQ
± .KDLEiRNK{QJFҫQWӯNKyDYDU

‡ %LӃQFөFEӝ
± Khai báo bên trong hàm
± 3KҥPYLWURQJKjPNKDLEiR
± %ҳWEXӝFSKҧLFyWӯNKyDYDU
21

.LӇXGӳOLӋX
‡ Numbers
± 6ӕQJX\rQKRһFVӕWKӵFQKѭKD\

‡ Logical or Boolean
± WUXHKRһFIDOVH

‡ Strings
± ;kXNtWӵQKѭ³7KLHWNH:HE´

‡ nulll
± ĈӅFұSÿӃQJLiWUӏQXOO
22

11


8/2/2010

.LӇXGӳOLӋX
‡ 'ӳOLӋXOX{QOX{QWKXӝFYӅPӝWORҥLQjRÿy
‡ %LӃQVӁWKXӝFYӅPӝWNLӇXGӳOLӋXNKLJiQJLi
WUӏ
‡ .LӇXWKD\ÿәLNKLGӳOLӋXWKD\ÿәL
var x = 10;
x = true;
[ ³+HOOR:RUOG´
23

.LӇXGӳOLӋX
‡ .ӃWKӧSKDLELӃQNKiFNLӇX[kXYjVӕ«
YDU$ ³+D1RL´³SKRSKXRQJ´
‡ Hàm
± SDUVH,QW
FKX\ӇQ[kXWKjQKVӕQJX\rQ
± SDUVH)ORDW
FKX\ӇQ[kXWKjQKVӕWKӵF
± &KX\ӇQNK{QJWKjQKF{QJ³1D1´

24

12


8/2/2010

.LӇXGӳOLӋX


25

.LӇXGӳOLӋX

26

13


8/2/2010

4.3. Phép toán
‡
‡
‡
‡

3KpSWRiQVӕKӑF
4.3.2. Phép toán so sánh
4.3.3. Phép toán logic
3KpSWRiQÿӏQKOѭӧQJ

27

3KpSWRiQVӕKӑF
Phép toán
Mô Wҥ
+
&ӛQJ
7Uӭ
/
Chia
%
6ӓ GѬ
++
7ăQJ 1
-*LҥP 1
ĈҥR GҧX

Ví Gӧ
A = 5 + 8
A = 8 ± 5
A = 20 / 5
10 % 3 = 1
++ x và x ++
-- x và x -QӁX A = 5
thì ±A = -5
28

14


8/2/2010

4.3.2. Phép toán so sánh
Phép
toán
= =

Mô WҥVRViQK

Ví Gӧ [ 
.ӁW TXҥ

%ҳQJJLi WUӍ

x = = 5

= = = %ҳQJJLiWUӍYj
NLӅX

x =

true

 ³´ false

! =

.K{QJEҳQJ

x ! = 4

true

>

/ӝQ KѪQ

x > 5

false

>=

/ӝQ KѪQKRҹFEҳQJ

x >= 5

true

<

1Kӑ KѪQ

x < 4

false

<=

1Kӑ KѪQKRҹFEҳQJ

x <=5

true
29

4.3.3. Phép toán logic
Phép
toán
&&
||
!

Mô Wҥ
And
Or
Not

Ví Gӧ
(x=6, y=3)
(x<10)&& (y>1)
(x<1)|| (y>5)
!(x==y)

.ӁW TXҥ
true
false
true

30

15


8/2/2010

4.3.3. Phép toán logic

31

4.3.3. Phép toán logic

32

16


8/2/2010

3KpSWRiQÿӏQKOѭӧQJ
‡ 3KpSWRiQÿLӅXNLӋQ
Cú pháp
WrQELӁQ ÿLӃXNLӉQ"JLiWUӍJLiWUӍ

9tGө
VWDWXV  GLHP! 
"³ÿҥW´³WKLOҥL´

33

3KpSWRiQÿӏQKOѭӧQJ
‡ typeof
± 7UҧYӅ[kX[iFÿӏQKNLӇXGӳOLӋXFӫDELӃQ
9tGө

var x = 5;
document.write(typeof(x));
=> number
34

17


8/2/2010

%LӇXWKӭF
‡ /jVӵJKpSQӕLFiFSKpSWRiQYjWRiQKҥQJWKHRPӝW
TX\WҳF[iFÿӏQK
‡ &iFWRiQKҥQJELӃQKҵQJKjPKRһFELӇXWKӭFNKiF
‡ 7KѭӡQJWKӇKLӋQF{QJWKӭFWtQKWRiQPӝWJLiWUӏQjR
ÿy
‡ ORҥL
± %LӇXWKӭFVӕKӑF
± %LӇXWKӭFORJLF
± %LӇXWKӭF[kX
35

+ӝSWK{QJEiR
‡ 6ӱGөQJFiFSKѭѫQJWKӭFFRQILUPDOHUW
prompt
‡ 3KѭѫQJWKӭFDOHUW

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay

×