You are on page 1of 40

Khoa Cng Ngh Thng Tin

Trng i Hc Cn Th

Lp Trnh JavaScript

Thanh Ngh
dtnghi@cit.ctu.edu.vn

Cn Th
04-11-2005

Ni dung
Gii thiu v JavaScript
 Bin, kiu d liu, php ton
 Lnh iu khin
 Popup
 S dng cc i tng


Printed with FinePrint trial version - purchase at www.fineprint.com

Gii thiu v JavaScript


 Bin, kiu d liu, php ton
 Lnh iu khin
 Popup
 S dng cc i tng


Gii thiu v JavaScript




JavaScript l g ?







JavaScript c thit k cng vi HTML to trang Web


sinh ng
JavaScript l ngn ng script, hng i tng, cha cc
dng lnh thc thi c
JavaScript c vit trc tip vo trang HTML
Javascript l ngn ng thng dch
Javascript khc vi Java

Printed with FinePrint trial version - purchase at www.fineprint.com

Gii thiu v JavaScript




JavaScript dng lm g ?






Ngi thit k Web c th hc k nng lp trnh n gin


ca JavaScript vit cc trang HTML sinh ng
JavaScript xut nhng text mt cch ng cho cc trang
HTML
JavaScript bt v x l cc s kin t giao tip ca ngi s
dng Webbrowser
JavaScript c th c v vit cc phn t c bn hay ni
dung ca trang HTML
JavaScript c th c s dng kim tra d liu trc
khi submit
JavaScript c th cung cp thng tin v browser
To cookies
5

V d 1 v JavaScript

Printed with FinePrint trial version - purchase at www.fineprint.com

V d 2 v JavaScript

V d 3 v JavaScript

Printed with FinePrint trial version - purchase at www.fineprint.com

V d 4 v JavaScript

V d 5 v JavaScript

Printed with FinePrint trial version - purchase at www.fineprint.com

10

Gii thiu v JavaScript


 Bin, kiu d liu, php ton
 Lnh iu khin
 Popup
 S dng cc i tng


11

Bin


Bin
Cha d liu
 Phn bit gia k t thng v hoa
 Khai bo :
var strname = some value
strname = some value
 Gn gi tr :
var strname = "Hege"
strname = "Hege"
 Phm vi s dng bin : cc b v ton cc


Printed with FinePrint trial version - purchase at www.fineprint.com

12

V d

13

Kiu d liu


Kiu d liu





S nguyn : 10, -301, 1974, etc.


S thc v s chm ng : 13.5, 1.35E1
Lun l : true, false
Chui : do thanh nghi, sinh nam 1974, \b, \n, \r, \t, \\

var quote = "He read \"The Cremation of Sam McGee\" by R.W. Service."
document.write(quote)
Kt qu s l :
He read "The Cremation of Sam McGee" by R.W. Service.

Printed with FinePrint trial version - purchase at www.fineprint.com

14

Kiu d liu


Kiu d liu
Mng :
myArray = new Array(10); foo = new Array(5);
myArray[0] = 56; myArray[9] = 44;
colors = new Array();
colors[99] = "midnightblue";
numberOfElements = myArray.length;


15

Kiu d liu


Kiu d liu


i tng :

Printed with FinePrint trial version - purchase at www.fineprint.com

16

Chuyn kiu d liu




Kiu d liu
c chuyn t ng :
var answer = 42
answer = "Thanks for all the fish..."
x = "The answer is " + 42


Chuyn chui sang s : ParseInt(), ParseFloat()

17

Php ton


Php ton








Gn : =
Php tng hoc gim 1 : ++, -Gn rt gn : +=, -=, *=, /=, %=
So snh : ==, !=, <, <=, >, >=
Php tnh s hc : +, -, *, /, %
Php ton lun l : &&, ||, !,
Php : (cond) ? Expr1:Expr2

Printed with FinePrint trial version - purchase at www.fineprint.com

18

Php ton

19

Php ton

Printed with FinePrint trial version - purchase at www.fineprint.com

20

Php ton

21

Php ton

Printed with FinePrint trial version - purchase at www.fineprint.com

22

Php ton

23

nh ngha hm


nh ngha
function functionname(var1,var2,...,varX)
{
some code
}

Printed with FinePrint trial version - purchase at www.fineprint.com

24

nh ngha hm


V d
function total(a,b)
{
x=a*b
return x
}
product=total(2,3)

25

nh ngha hm


V d
<html> <head>
<script type="text/javascript">
function displaymessage()
{
alert("Hello World!")
}
</script>
</head><body>
<form>
<input type="button" value="Click me!"
onclick="displaymessage()" >
</form>
</body>
</html>

Printed with FinePrint trial version - purchase at www.fineprint.com

26

nh ngha hm

27

nh ngha hm

Printed with FinePrint trial version - purchase at www.fineprint.com

28

Gii thiu v JavaScript


 Bin, kiu d liu, php ton
 Lnh iu khin
 Popup
 S dng cc i tng


29

Cu trc IF-ELSE


C php

if (condition) {
statements1
}
Hay
if (condition) {
statements1
}
else {
statements2
}
Printed with FinePrint trial version - purchase at www.fineprint.com

30

Cu trc IF-ELSE

31

Cu trc IF-ELSE

Printed with FinePrint trial version - purchase at www.fineprint.com

32

Cu trc la chn switch-case




C php

switch(n) {
case 1:
execute code block 1
break
case 2:
execute code block 2
break
default:
code to be executed if n is
different from case 1 and 2
}

33

Cu trc la chn switch-case




V d

<script type="text/javascript">
theDay=d.getDay()
switch (theDay) {
case 5:
document.write("Finally Friday")
break
case 6:
document.write("Super Saturday")
break
case 0:
document.write("Sleepy Sunday")
break
default:
document.write("I'm looking forward to this weekend!")
}
</script>

Printed with FinePrint trial version - purchase at www.fineprint.com

34

Cu trc la chn switch-case

35

Cu trc lp for


C php

for (initial-expression; condition; increment-expression) {


statements
}
V d:
var n = 0;
for (var i = 0; i < 3; i++) {
n += i;
alert("The value of n is now " + n);
}
Printed with FinePrint trial version - purchase at www.fineprint.com

36

Cu trc lp for

37

Cu trc lp for

Printed with FinePrint trial version - purchase at www.fineprint.com

38

Cu trc lp while, do-while




C php

while (var<=endvalue)
{
code to be executed
}
do
{
code to be executed
}
while (var<=endvalue)
39

Cu trc lp while, do-while




V d

<html>
<body>
<script type="text/javascript">
var i=0
while (i<=10)
{
document.write("The number is " + i)
document.write("<br />")
i=i+1
}
</script>
</table>
</body>
</html>
Printed with FinePrint trial version - purchase at www.fineprint.com

40

Cu trc lp while, do-while




V d

<html>
<body>
<script type="text/javascript">
var i=0
do {
document.write("The number is " + i)
document.write("<br />")
i=i+1
}
while (i<0)
</script>
</table>
</body>
</html>

41

Cu trc lp while, do-while

Printed with FinePrint trial version - purchase at www.fineprint.com

42

Cu trc lp while, do-while

43

break trong cu trc lp

Printed with FinePrint trial version - purchase at www.fineprint.com

44

continue trong cu trc lp

45

Gii thiu v JavaScript


 Bin, kiu d liu, php ton
 Lnh iu khin
 Popup
 S dng cc i tng


Printed with FinePrint trial version - purchase at www.fineprint.com

46

Popup


Cc lai




Alert box
Confirm box
Prompt box

47

alert("sometext")

Printed with FinePrint trial version - purchase at www.fineprint.com

48

confirm("sometext")

49

prompt("sometext","defaultvalue")

Printed with FinePrint trial version - purchase at www.fineprint.com

50

Kt hp vi form

51

Kt hp vi form

Printed with FinePrint trial version - purchase at www.fineprint.com

52

Gii thiu v JavaScript


 Bin, kiu d liu, php ton
 Lnh iu khin
 Popup
 S dng cc i tng


53

i tng


Cc i tng







String
Date
Array
Boolean
Math
HTML DOM

Printed with FinePrint trial version - purchase at www.fineprint.com

54

i tng string

55

i tng string

Printed with FinePrint trial version - purchase at www.fineprint.com

56

i tng string


V d
var txt="Hello world!"
document.write(txt.length)
document.write(txt.toUpperCase())
document.write(txt.substring(4, 8))
document.write(txt.small())
document.write(txt.strike())
document.write(txt.charAt(8))

57

i tng Date

Printed with FinePrint trial version - purchase at www.fineprint.com

58

i tng Date

59

i tng Date

Printed with FinePrint trial version - purchase at www.fineprint.com

60

i tng Date


V d
var myDate=new Date()
myDate.setFullYear(2010,0,14)
myDate.setDate(myDate.getDate()+5)
myDate.setFullYear(2010,0,14)
var today = new Date()
if (myDate>today)
alert("Today is before 14th January 2010")
else
alert("Today is after 14th January 2010")
61

i tng Array

Printed with FinePrint trial version - purchase at www.fineprint.com

62

i tng Array


V d
var mycars=new Array()
mycars[0]="Saab"
mycars[1]="Volvo"
mycars[2]="BMW
var mycars=new Array("Saab","Volvo","BMW")
var mycars=new Array(3)
mycars[0]="Saab"
mycars[1]="Volvo"
mycars[2]="BMW"

63

i tng Array


V d
var arr = new Array(3)
arr[0] = "Jani"
arr[1] = "Tove"
arr[2] = "Hege"
var arr2 = new Array(3)
arr2[0] = "John"
arr2[1] = "Andy"
arr2[2] = "Wendy"
document.write(arr.concat(arr2))

Printed with FinePrint trial version - purchase at www.fineprint.com

64

i tng Array


V d
var arr = new Array(6)
arr[0] = "Jani"
arr[1] = "Hege"
arr[2] = "Stale"
arr[3] = "Kai Jim"
arr[4] = "Borge"
arr[5] = "Tove"
document.write(arr + "<br />")
document.write(arr.sort())
65

i tng Boolean

Printed with FinePrint trial version - purchase at www.fineprint.com

66

i tng Boolean


V d
var myBoolean=new Boolean()
var myBoolean=new Boolean(0)
var myBoolean=new Boolean(null)
var myBoolean=new Boolean("")
var myBoolean=new Boolean(false)
var myBoolean=new Boolean(NaN)
var myBoolean=new Boolean(true)
var myBoolean=new Boolean("true")
var myBoolean=new Boolean("false")
var myBoolean=new Boolean("Richard")
67

i tng Math

Printed with FinePrint trial version - purchase at www.fineprint.com

68

i tng Math

69

i tng Math


V d
Math.E
Math.PI
Math.SQRT2
Math.SQRT1_2
Math.LN2
Math.LN10
Math.LOG2E
Math.LOG10E
document.write(Math.round(4.7))
document.write(Math.random())
document.write(Math.floor(Math.random()*11))
document.write(Math.round(-4.60))

Printed with FinePrint trial version - purchase at www.fineprint.com

70

i tng HTML DOM

71

i tng HTML DOM

Printed with FinePrint trial version - purchase at www.fineprint.com

72

i tng HTML DOM

Printed with FinePrint trial version - purchase at www.fineprint.com

73

Printed with FinePrint trial version - purchase at www.fineprint.com

Printed with FinePrint trial version - purchase at www.fineprint.com

Printed with FinePrint trial version - purchase at www.fineprint.com

You might also like