Professional Documents
Culture Documents
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
JavaScript l g ?
JavaScript dng lm g ?
V d 1 v JavaScript
V d 2 v JavaScript
V d 3 v JavaScript
V d 4 v JavaScript
V d 5 v JavaScript
10
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
12
V d
13
Kiu d liu
Kiu d liu
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.
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 :
16
Kiu d liu
c chuyn t ng :
var answer = 42
answer = "Thanks for all the fish..."
x = "The answer is " + 42
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
18
Php ton
19
Php ton
20
Php ton
21
Php ton
22
Php ton
23
nh ngha hm
nh ngha
function functionname(var1,var2,...,varX)
{
some code
}
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>
26
nh ngha hm
27
nh ngha hm
28
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
32
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
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>
34
35
Cu trc lp for
C php
36
Cu trc lp for
37
Cu trc lp for
38
C php
while (var<=endvalue)
{
code to be executed
}
do
{
code to be executed
}
while (var<=endvalue)
39
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
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
42
43
44
45
46
Popup
Cc lai
Alert box
Confirm box
Prompt box
47
alert("sometext")
48
confirm("sometext")
49
prompt("sometext","defaultvalue")
50
Kt hp vi form
51
Kt hp vi form
52
53
i tng
Cc i tng
String
Date
Array
Boolean
Math
HTML DOM
54
i tng string
55
i tng string
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
58
i tng Date
59
i tng Date
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
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))
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
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
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))
70
71
72
73