6. d. DƶƵ lieƤu kieƄu null:
TraĆ» veĆ giaĆ¹ trĆ² roĆ£ng.
e. DƶƵ lieƤu kieĆ„u vaĆŖn baĆ»n (gioĆ”ng nhƶ kieĆ„u chuoĆ„i)
10. TaĆÆo bieĆ”n trong JavaScript:
Var example;
Var example=āHelloā;
Ta coĆ¹ theĆ„ duĆøng document.write(example); ƱeĆ„ xuaĆ”t noƤi dung cuĆ»a bieĆ”n.
VĆ duĆÆ 1: duĆøng tƶĆø khoĆ¹a var ƱeĆ„ khai baĆ¹o bieĆ”n
<HTML> <BODY>
<HEAD> <SCRIPT LANGUAGE="Javascript">
<TITLE>Example 3.1</TITLE> <!--
document.write('<IMG
<SCRIPT LANGUAGE="Javascript"> SRC=/slideshow/tai-lieu-javascript/5089421/"Welcome.gif">&
<!- - document.write("<H1>Greeting ," +
var name=prompt("enter your name + " Welcome to netscape
name:","name"); navigator 2.01 </H1>");
- -> -->
</SCRIPT> </SCRIPT>
</BODY>
</HEAD> </HTML>
VĆ du 2: taĆÆo laĆÆi moƤt giaĆ¹ trĆ² mĆ“Ć¹i cho bieĆ”n
<HTML> </HEAD>
<HEAD> <BODY>
<TITLE>Example 3.2</TITLE> <SCRIPT LANGUAGE="Javascript">
<!--
<SCRIPT LANGUAGE="Javascript"> document.write('<IMG
var name=prompt("enter your SRC=/slideshow/tai-lieu-javascript/5089421/"Welcome.gif">&
name:","name"); document.write("<H1>Greeting ," +
alert ("greeting " + name + " , "); name + " Welcome to netscape
name=prompt("enter your friend's navigator 2.01 </H1>");
name:","friend's name"); -->
</SCRIPT>
</SCRIPT> </BODY>
</HTML>
11. LaĆøm vieƤc vĆ“Ć¹i bieĆ”n vaĆø bieĆ„u thĆ¶Ć¹c:
ā¢ ThieĆ”t laƤp bieĆ„u thĆ¶Ć¹c:
CuĆ¹ phaĆ¹p: <bieĆ”n> <toaĆ¹n tƶƻ > <bieĆ„u thĆ¶Ć¹c>
* ToaĆ¹n tƶƻ:
= ThieĆ”t laƤp giaĆ¹ trĆ² beĆ¢n phaĆ»i cho beĆ¢n traĆ¹i
VĆ duĆÆ :x=5
+= CoƤng traĆ¹i vaĆø phaĆ»i ,sau ƱoĆ¹ gaĆ¹n keĆ”t quaĆ» cho beĆ¢n traĆ¹i pheĆ¹p toaĆ¹n
VĆ duĆÆ: cho x=10,y=5
x+=y => x=15
-= TrƶĆø beĆ¢n traĆ¹i cho beĆ¢n phaĆ»i ,gaĆ¹n keĆ”t quaĆ» laĆÆi cho beĆ¢n traĆ¹i
x-=y => x=5
JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 6
7. *= NhaĆ¢n beĆ¢n traĆ¹i cho beĆ¢n phaĆ»i,gaĆ¹n keĆ”t quaĆ» cho beĆ¢n traĆ¹i
x*=y => x=50
/= Chia beĆ¢n traĆ¹i cho phaĆ»i ,gaĆ¹n keĆ”t quaĆ» laĆÆi cho beĆ¢n traĆ¹i
x/=y => x=2
%= Chia beĆ¢n traĆ¹i cho beĆ¢n phaĆ»i vaĆø laĆ”y soĆ” dƶ gaĆ¹n laĆÆi cho beĆ¢n traĆ¹i
x%=y => x=0
* CaĆ¹c toaĆ¹n tƶƻ khaĆ¹c:
VĆ duĆÆ:
x+=15+3 y=++x; (=> y=6 vƬ x taĆŖng leĆ¢n 6)
=> x=18 z=x++; (=> z=6 vƬ sau ƱoĆ¹ x gaĆ¹n cho z)
8+5 sau ƱoĆ¹ x taĆŖng 1 => x=7
32.5 * 72.3 Do ƱoĆ¹ ta coĆ¹ keĆ”t quaĆ» cuoĆ”i cuĆøng laĆø:
12 % 5 x=7;y=6;z=6;
DaĆ”u ++ vaĆø daĆ”u - - vaĆø daĆ”u - VĆ duĆÆ: x=5;
VĆ duĆÆ: x=-x => x=-5
x=5;
ā¢ PheĆ¹p toaĆ¹n Logic
&& : vaĆø
||: hoaƫc
! not
VĆ duĆÆ:
x=5 ,y=2 ,c=3
(x>y) && (x>c)
false && anything is always false
(x>y) || (c<x) => true
!x
ā¢ ToaĆ¹n tƶƻ so saĆ¹nh trong JavaScript:
== 1==1 => true
!= 3<1 =>false
> 5 >=4 =>true
< ātheā != āheā => true
>= 4==ā4ā =>true
<=
VĆ duĆÆ:
ā¢ ToaĆ¹n tƶƻ ƱieĆ u kieƤn:
CuĆ¹ phaĆ¹p:
(ƱieĆ u kieƤn ) ? giaĆ¹ trĆ² 1 : giaĆ¹ trĆ² 2
NeĆ”u ƱieĆ u kieƤn ƱuĆ¹ng thƬ traĆ» veĆ giaĆ¹ trĆ² 1
NeĆ”u ƱieĆ u kieƤn sai thƬ traĆ» veĆ giaĆ¹ trĆ² 2
VĆ duĆÆ:
JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 7
8. (day=āSaturdayā) ? āWeekendā : āNot Saturdayā
ā¢ ToaĆ¹n tƶƻ chuoĆ„i:
ā Welcome to ā + ā Netscape Navigatorā
VĆ duĆÆ:
Var welcome=āWelcome toā
Welcome += ā Netscape Navigatorā
! welcome= āWelcome to Netsacpe Navigatorā
VĆ duĆÆ : Sƶƻ duĆÆng toaĆ¹n tƶƻ ƱieĆ u kieƤn ƱeĆ„ kieĆ„m tra ngoĆ» vaĆøo
<HTML> var output = (response==answer) ?
<HEAD> correct:incorrect;
<TITLE>Example 3.3</TITLE> </SCRIPT>
</HEAD>
<SCRIPT LANGUAGE="Javascript">
var question="What is 10+10 ?"; <BODY>
var answer=20; <SCRIPT LANGUAGE="Javascript">
var correct='<IMG <!--
SRC=/slideshow/tai-lieu-javascript/5089421/"correct.gif">& document.write(output);
var incorrect='<IMG -->
SRC="incorect.gif">'; </SCRIPT>
var response=prompt(question,"0"); </BODY>
</HTML>
12. CaĆ”u truĆ¹c ƱieĆ u kieƤn if ā else
if ƱieĆ u kieƤn
leƤnh ;
if ƱieĆ u kieƤn {
MaƵ JavaScript
}
VĆ duĆÆ: If (day!=āSaturdayā) {
if (day==āSaturdayā) { document.writeln(āItās not Saturdayā);
document.writeln(āItās the weekendā); }
alert(ā Itās the weekendā); Sƶƻ duĆÆng caĆ”u truĆ¹c else ā if cho vĆ duĆÆ Ć“Ć» treĆ¢n
} If (day==āSaturdayā) {
VĆ duĆÆ: document.writeln(āItās the weekendā);
If (day==āSaturdayā) { }
document.writeln(āItās the weekendā); else {
} document.writeln(āItās not Saturdayā);
}
CaĆ”u truĆ¹c keĆ”t hĆ“ĆÆp :
if ƱieĆ u kieƤn 1 {
CaĆ¹c leƤnh JavaScript
JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 8
9. if ƱieĆ u kieƤn 2 {
CaĆ¹c leƤnh JavaScript
} else {
caĆ¹c leƤnh khaĆ¹c
}
CaĆ¹c leƤnh JavaScript
} else {
CaĆ¹c leƤnh khaĆ¹c
}
VĆ duĆÆ 1 : Sƶƻ duĆÆng phƶƓng phaĆ¹p confirm() vĆ“Ć¹i phaĆ¹t bieĆ„u if
<HTML> response=prompt(question,"0");
<HEAD> }
<TITLE>Example 3.3</TITLE>
var output = (response ==answer ) ?
<SCRIPT LANGUAGE="Javascript"> correct:incorrect ;
var question="What is 10+10 ?";
var answer=20; </SCRIPT>
var correct='<IMG
SRC=/slideshow/tai-lieu-javascript/5089421/"correct.gif">& </HEAD>
var incorrect='<IMG <BODY>
SRC="incorect.gif">'; <SCRIPT LANGUAGE="Javascript">
<!--
var response=prompt(question,"0"); document.write(output);
-->
if (response != answer) { </SCRIPT>
if (confirm("Wrong ! press OK for </BODY>
a second change")) </HTML>
VĆ duĆÆ 2 : Sƶƻ duĆÆng phƶƓng phaĆ¹p confirm() vĆ“Ć¹i phaĆ¹t bieĆ„u if - else
<HTML> question="What is 10*10";
<HEAD> answer=100;
<TITLE>Example 3.3</TITLE>
response=prompt(question,"0");
<SCRIPT LANGUAGE="Javascript"> }
var question="What is 10+10 ?"; }
var answer=20;
var correct='<IMG var output = (response ==answer ) ?
SRC=/slideshow/tai-lieu-javascript/5089421/"correct.gif">& correct:incorrect ;
var incorrect='<IMG
SRC="incorect.gif">'; </SCRIPT>
var response=prompt(question,"0"); </HEAD>
<BODY>
if (response != answer) { <SCRIPT LANGUAGE="Javascript">
if (confirm("Wrong ! press OK for <!--
a second change")) document.write(output);
response=prompt(question,"0"); -->
}else { </SCRIPT>
if (confirm("Correct ! press OK </BODY>
for a second question")) </HTML>
MTWRFSS
{
JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 9
10. BAĆI 3: HAĆM VAĆ ĆOĆI TĆĆĆNG
Trong kyƵ thuaƤt laƤp trƬnh caĆ¹c laƤp trƬnh vieĆ¢n thƶƓĆøng sƶƻ duĆÆng haĆøm ƱeĆ„ thƶĆÆc hieƤn moƤt ƱoaĆÆn chƶƓng
trƬnh theĆ„ hieƤn cho moƤt module naĆøo ƱoĆ¹ ƱeĆ„ thƶĆÆc hieƤn moƤt coĆ¢ng vieƤc naĆøo ƱoĆ¹.
Trong Javascript coĆ¹ caĆ¹c haĆøm ƱƶƓĆÆc xaĆ¢y dƶĆÆng saĆ¼n ƱeĆ„ giuĆ¹p baĆÆn thƶĆÆc hieƤn moƤt chĆ¶Ć¹c naĆŖng naĆøo ƱoĆ¹ vĆ
duĆÆ nhƶ haĆøm alert(), document.write(), parseInt() vaĆø baĆÆn cuƵng coĆ¹ theĆ„ Ć±Ć²nh nghĆ³a ra caĆ¹c haĆøm khaĆ¹c
cuĆ»a mƬnh ƱeĆ„ thƶĆÆc hieƤn moƤt coĆ¢ng vieƤc naĆøo ƱoĆ¹ cuĆ»a baĆÆn, ƱeĆ„ Ć±Ć²nh nghĆ³a haĆøm baĆÆn theo cuĆ¹ phaĆ¹p sau:
function function_name(parameters, arguments)
{
command block
}
TruyeĆ n tham soĆ”:
function printName(name) {
document.write(ā<HR>Your Name is <B><I>ā);
document.write(name);
document.write(ā</B></I><HR>ā);
}
VĆ duĆÆ:
GoĆÆi haĆøm printName()vĆ“Ć¹i leƤnh sau printName(āBobā);
Khi haĆøm printName()ƱƶƓĆÆc thi haĆønh giaĆ¹ trĆ² cuĆ»a name laĆø "Bob" neĆ”u goĆÆi haĆøm
printName()vĆ“Ć¹i ƱoĆ”i soĆ” laĆø moƤt bieĆ”n
var user = āJohnā;
printName(user);
Khi ƱoĆ¹ name laĆø āJohnā. NeĆ”u baĆÆn muoĆ”n thay ƱoĆ„i giaĆ¹ trĆ² cuĆ»a name baĆÆn coĆ¹ theĆ„ laĆøm nhƶ sau : name
= āMr. ā + name;
PhaĆÆm vi cuĆ»a bieĆ”n:
BieĆ”n toaĆøn cuĆÆc (Global variable)
BieĆ”n cuĆÆc boƤ (Local variable)
TraĆ» veĆ caĆ¹c giaĆ¹ trĆ²:
VĆ duĆÆ:
DuĆøng return ƱeĆ„ traĆ» veĆ giaĆ¹ trĆ² cuĆ»a bieĆ”n cube.
MTWRFSS
function cube(number) {
var cube = number * number * number;
return cube;
}
JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 10
11. VĆ duĆÆ:
MTWRFSS
<HTML> //CHECK THE RESULT
<HEAD> return (response == answer) ? correct
<TITLE>Example 4.1</TITLE> : incorrect;
<SCRIPT LANGUAGE=āJavaScriptā> }
<!-- HIDE FROM OTHER BROWSERS // STOP HIDING FROM OTHER BROWSERS --
//DEFINE FUNCTION testQuestion() >
function testQuestion(question) { </SCRIPT>
//DEFINE LOCAL VARIABLES FOR THE </HEAD<
FUNCTION <BODY>
var answer=eval(question); <SCRIPT LANGUAGE=āJavaScriptā>
var output=āWhat is ā + question + <!-- HIDE FROM OTHER BROWSERS
ā?ā; //ASK QUESTION AND OUTPUT RESULTS
var correct=ā<IMG var result=testQuestion(ā10 + 10ā);
SRC=/slideshow/tai-lieu-javascript/5089421/ācorrect.gifā>ā; document.write(result);
var incorrect=ā<IMG //STOP HIDING FROM OTHER BROWSERS -->
SRC=/slideshow/tai-lieu-javascript/5089421/āincorrect.gifā>ā; </SCRIPT>
//ASK THE QUESTION </BODY>
var response=prompt(output,ā0"); </HTML>
HaĆøm eval duĆøng chuyeĆ„n ƱoĆ„i giaĆ¹ trĆ² chuoĆ„i soĆ” thaĆønh giaĆ¹ trĆ² soĆ”
eval(ā10*10ā)traĆ» veĆ giaĆ¹ trĆ² laĆø 100
HaĆøm goĆÆi laĆÆi haĆøm:
VĆ duĆÆ:
<HTML> //CHECK THE RESULT
<HEAD> return (response == answer) ? correct
<TITLE>Example 4.2</TITLE> : testQuestion(question);
<SCRIPT LANGUAGE=āJavaScriptā> }
<!-- HIDE FROM OTHER BROWSERS // STOP HIDING FROM OTHER BROWSERS --
//DEFINE FUNCTION testQuestion() >
function testQuestion(question) { </SCRIPT>
//DEFINE LOCAL VARIABLES FOR THE </HEAD<
FUNCTION <BODY>
var answer=eval(question); <SCRIPT LANGUAGE=āJavaScriptā>
var output=āWhat is ā + question + <!-- HIDE FROM OTHER BROWSERS
ā?ā; //ASK QUESTION AND OUTPUT RESULTS
var correct=ā<IMG var result=testQuestion(ā10 + 10ā);
SRC=/slideshow/tai-lieu-javascript/5089421/ācorrect.gifā>ā; document.write(result);
var incorrect=ā<IMG //STOP HIDING FROM OTHER BROWSERS -->
SRC=/slideshow/tai-lieu-javascript/5089421/āincorrect.gifā>ā; </SCRIPT>
//ASK THE QUESTION </BODY>
var response=prompt(output,ā0"); </HTML>
VĆ duĆÆ 2:
<HTML> //DEFINE LOCAL VARIABLES FOR THE
<HEAD> FUNCTION
<TITLE>Example 4.2</TITLE> var answer=eval(question);
<SCRIPT LANGUAGE=āJavaScriptā> var output=āWhat is ā + question +
<!-- HIDE FROM OTHER BROWSERS ā?ā;
//DEFINE FUNCTION testQuestion() var correct=ā<IMG
function SRC=/slideshow/tai-lieu-javascript/5089421/ācorrect.gifā>ā;
testQuestion(question,chances) {
JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 11
12. var incorrect=ā<IMG // STOP HIDING FROM OTHER BROWSERS --
SRC=/slideshow/tai-lieu-javascript/5089421/āincorrect.gifā>ā; >
4 </SCRIPT>
//ASK THE QUESTION </HEAD>
var response=prompt(output,ā0"); <BODY>
//CHECK THE RESULT <SCRIPT LANGUAGE=āJavaScriptā>
if (chances > 1) { <!-- HIDE FROM OTHER BROWSERS
return (response == answer) ? correct //ASK QUESTION AND OUTPUT RESULTS
: testQuestion(question,chances-1); var result=testQuestion(ā10 + 10ā,3);
} else { document.write(result);
return (response == answer) ? correct //STOP HIDING FROM OTHER BROWSERS -->
: incorrect; </SCRIPT>
} </BODY>
} </HTML>
BaĆøi 4: TAĆO ĆOĆI TĆĆĆNG TRONG JAVASCRIPT
1. ĆĆ²nh nghĆ³a thuoƤc tĆnh cuĆ»a ƱoĆ”i tƶƓĆÆng:
function student(name,age, grade) {
this.name = name;
this.age = age;
this.grade = grade;
}
ĆeĆ„ taĆÆo moƤt Object ta sƶƻ duĆÆng phaĆ¹t bieĆ„u new.VĆ duĆÆ Ć±eĆ„ taĆÆo ƱoĆ”i tƶƓĆÆng student1
student1 = new student(āBobā,10,75);
3 thuoƤc tĆnh cuĆ»a ƱoĆ”i tƶƓĆÆng student1 laĆø :
student1.name
student1.age
student1.grade
VĆ duĆÆ Ć±eĆ„ taĆÆo ƱoĆ”i tƶƓĆÆng student2
student2 = new student(āJaneā,9,82);
ĆeĆ„ theĆ¢m thuoƤc tĆnh cho student1 baĆÆn coĆ¹ theĆ„ laĆøm nhƶ sau:
student1.mother = āSusanā; hoaĆ«c baĆÆn coĆ¹ theĆ„ Ć±Ć²nh nghĆ³a laĆÆi haĆøm student
MTWRFSS
4
function student(name, age, grade, mother) {
this.name = name;
this.age = age;
this.grade = grade;
this.mother = mother;
}
JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 12
14. document.writeln(āSocial Security // STOP HIDING FROM OTHER BROWSERS --
Number: ā + this.socsec); >
document.writeln(āAnnual Salary: ā + </SCRIPT>
this.salary); </HEAD>
document.write(ā</PRE>ā); <BODY>
} <SCRIPT LANGUAGE=āJavaScriptā>
//DEFINE OBJECT <!-- HIDE FROM OTHER BROWSERS
function employee() { newEmployee.displayInfo();
this.name=prompt(āEnter Employeeās // STOP HIDING FROM OTHER BROWSERS --
Nameā,āNameā); >
this.number=prompt(āEnter Employee </SCRIPT>
Number for ā + this.name,ā000-000"); </BODY>
this.socsec=prompt(āEnter Social </HTML>
Security Number for ā +
this.name,ā000-00-0000");
this.salary=prompt(āEnter Annual
Salary for ā + this.name,ā$00,000");
this.displayInfo=displayInfo;
}
newEmployee=new employee();
Vi du:
<script LANGUAGE="JavaScript"> myhours = mydate.getHours();
<!-- Begin ampmhour = (myhours > 12) ? myhours -
var day=""; 12 : myhours;
var month=""; ampm = (myhours >= 12) ? 'BuoĆ„ i ChieĆ u '
var ampm=""; : ' BuoĆ„ i SaĆ¹ ng ';
var ampmhour=""; mytime = mydate.getMinutes();
var myweekday=""; myminutes = ((mytime < 10) ? ':0' :
var year=""; ':') + mytime;
mydate = new Date(); if(myday == 0)
myday = mydate.getDay(); day = " Chuƻ NhaƤ t , ";
mymonth = mydate.getMonth(); else if(myday == 1)
myweekday= mydate.getDate(); day = " ThĆ¶Ć¹ hai, ";
weekday= myweekday; else if(myday == 2)
myyear= mydate.getYear(); day = " ThĆ¶Ć¹ ba, ";
year = myyear; else if(myday == 3)
JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 14
15. day = " ThĆ¶Ć¹ tƶ, "; else if(mymonth ==5)
else if(myday == 4) month = "thaĆ¹ ng saĆ¹ u ";
day = " ThĆ¶Ć¹ naĆŖ m, "; else if(mymonth ==6)
else if(myday == 5) month = "thaĆ¹ ng baĆ» y ";
day = " ThĆ¶Ć¹ saĆ¹ u , "; else if(mymonth ==7)
else if(myday == 6) month = "thaĆ¹ ng taĆ¹ m ";
day = " ThĆ¶Ć¹ baĆ» y , "; else if(mymonth ==8)
if(mymonth == 0) { month = "thaĆ¹ ng chĆn ";
month = "thaĆ¹ ng moƤ t ";} else if(mymonth ==9)
else if(mymonth ==1) month = "thaĆ¹ ng mƶƓĆø i ";
month = "thaĆ¹ ng hai "; else if(mymonth ==10)
else if(mymonth ==2) month = "thaĆ¹ ng mƶƓĆø i moƤ t ";
month = "thaĆ¹ ng ba "; else if(mymonth ==11)
else if(mymonth ==3) month = "thaĆ¹ ng mƶƓĆø i hai ";
month = "thaĆ¹ ng tƶ "; // End -->
else if(mymonth ==4) </script>
month = "thaĆ¹ ng naĆŖ m, ";
Trong phaĆ n body baĆÆn coĆ¹ theĆ„ xuaĆ”t ra daĆÆng nhƶ sau:
<body>
<script>
document.write("<b><font color=#0000ff face='VNI-
Times,helvetica,arial'>" + ampmhour + "" + myminutes + ampm)
document.write(" - " + day + " ngaĆø y " + myweekday +" ");
document.write( month + " , naĆŖ m " + year + "</font>");
</script>
</body>
BaĆøi 5: SĆĆ KIEĆN TRONG JAVASCRIPT
CaĆ¹c sƶĆÆ kieƤn cung caĆ”p caĆ¹c tƶƓng taĆ¹c vĆ“Ć¹i cƶƻa soĆ„ trƬnh duyeƤt vaĆø taĆøi lieƤu hieƤn haĆønh Ʊang ƱƶƓĆÆc load
trong trang web, caĆ¹c haĆønh ƱoƤng cuĆ»a user khi nhaƤp dƶƵ lieƤu vaĆøo form vaĆø khi click vaĆøo caĆ¹c button
trong form.
Khi sƶƻ duĆÆng boƤ quaĆ»n lyĆ¹ sƶĆÆ kieƤn baĆÆn coĆ¹ theĆ„ vieĆ”t caĆ¹c haĆøm ƱeĆ„ bieĆ„u dieĆ£n cho caĆ¹c haĆønh ƱoƤng dƶĆÆa vaĆøo
caĆ¹c sƶĆÆ kieƤn ƱƶĆÆoc choĆÆn
BaĆ»ng sƶĆÆ kieƤn trong Javascript
JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 15
16. TeĆ¢n sƶĆÆ kieƤn MoĆ¢ taĆ»
blur XaƵy ra khi ƱieĆ„m taƤp trungcuĆ»a ngoƵ vaĆøo ƱƶƓĆÆc di chuyeĆ„n ra
khoĆ»i moƤt thaĆønh phaĆ n cuĆ»a Form (Khi user click ra ngoaĆøi moƤt
trƶƓĆøng)
click Khi user Click vaĆøo 1 link hoaĆ«c thaĆønh phaĆ n cuĆ»a Form.
change XaƵy ra khi giaĆ¹ trĆ² cuĆ»a Form Field bĆ² thay ƱoĆ„i bĆ“Ć»i user.
focus XaƵy ra khi ngoƵ vaĆøo taƤp trung vaĆøo thaĆønh phaĆ n cuĆ»a Form
load XaƵy ra khi moƤt trang ƱƶƓĆÆc Load vaĆøo trong boƤ duyeƤt.
mouseover XaƵy ra khi User di chuyeƄn mouse qua moƤt Hyperlink.
select XaƵy ra khi User choĆÆn 1 trƶƓĆøng cuĆ»a thaĆønh phaĆ n Form.
submit XaƵy ra khi User xaĆ¹c nhaƤn ƱaƵ nhaƤp xong dƶƵ lieƤu.
unload XaƵy ra khi User rĆ“Ćøi khoĆ»i trang Web.
BoƤ quaĆ»n lyĆ¹ sƶĆÆ kieƤn (Event Handler)
ĆeĆ„ quaĆ»n lyĆ¹ caĆ¹c sƶĆÆ kieƤn trong javascript ta duĆøng caĆ¹c boƤ quaĆ»n lyĆ¹ sƶĆÆ kieƤn.
CuĆ¹ phaĆ¹p cuĆ»a moƤt boƤ quaĆ»n lyĆ¹ sƶĆÆ kieƤn:
<HTML_TAG OTHER_ATTRIBUTES eventHandler=āJavaScript Programā>
VĆ duĆÆ:
<INPUT TYPE=ātextā onChange=ācheckField(this)ā>
VĆ duĆÆ:
<INPUT TYPE=ātextā onChange=ā
if (parseInt(this.value) <= 5) {
alert(āPlease enter a number greater than 5.ā);
}
ā>
VĆ duĆÆ:
<INPUT TYPE=ātextā onChange=ā
alert(āThanks for the entry.ā);
confirm(āDo you want to continue?ā);
ā>
TƶĆø khoĆ¹a this: quy cho ƱoĆ”i tƶƓĆÆng hieƤn haĆønh.Trong Javascript Form laĆø moƤ ƱoĆ”i tƶƓĆÆng.CaĆ¹c thaĆønh
phaĆ n cuĆ»a Form bao goĆ m text fields, checkboxes, radio buttons, buttons, vaĆø selection lists.
VĆ duĆÆ:
<INPUT TYPE=ātextā onChange=ācheckField(this)ā>
CaĆ¹c boƤ quaĆ»n lyĆ¹ sƶĆÆ kieƤn trong Javascript
ĆoĆ”i tƶƓĆÆng BoƤ quaĆ»n lyĆ¹ sƶĆÆ kieƤn tƶƓng Ć¶Ć¹ng.
JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 16
17. Selection list onBlur, onChange, onFocus
Text element onBlur, onChange, onFocus, onSelect
Textarea element onBlur, onChange, onFocus, onSelect
Button element OnClick
Checkbox onClick
Radio button OnClick
Hypertext link onClick, onMouseOver
Reset button OnClick
Submit button OnClick
Document onLoad, onUnload
Window onLoad, onUnload
Form onSubmit
5
CaĆ¹ch duĆøng boƤ quaĆ»n lyĆ¹ sƶĆÆ kieƤn onLoad & onUnload
<HTML>
<HEAD>
<TITLE>Example 5.1</TITLE>
</HEAD>
<BODY onLoad=āalert(āWelcome to my page!ā);ā
onUnload=āalert(āGoodbye! Sorry to see you go!ā);ā>
<IMG SRC=/slideshow/tai-lieu-javascript/5089421/ātitle.gifā>
</BODY>
</HTML>
MTWRFSS
Vi du:
<HTML>
<HEAD>
<TITLE>Example 5.1</TITLE>
<SCRIPT LANGUAGE=āJavaScriptā>
<!-- HIDE FROM OTHER BROWSERS
var name = āā;
// STOP HIDING FROM OTHER BROWSERS -->
</SCRIPT>
</HEAD>
<BODY onLoad=ā
name = prompt(āEnter Your Name:ā,āNameā);
alert(āGreetings ā + name + ā, welcome to my page!ā);ā
onUnload=ā alert(Goodbye ā + name + ā, sorry to see you go!ā);ā>
<IMG SRC=/slideshow/tai-lieu-javascript/5089421/ātitle.gifā>
</BODY>
</HTML>
Vi du
MTWRFSS
<HTML>
<HEAD>
JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 17
18. <TITLE>Example 5.1</TITLE>
<SCRIPT LANGUAGE=āJavaScriptā>
<!-- HIDE FROM OTHER BROWSERS
// DEFINE GLOBAL VARIABLE
var name = āā;
function hello() {
name = prompt(āEnter Your Name:ā,āNameā);
alert(āGreetings ā + name + ā, welcome to my page!ā);
}
function goodbye() {
alert(Goodbye ā + name + ā, sorry to see you go!ā);
}
// STOP HIDING FROM OTHER BROWSERS -->
</SCRIPT>
</HEAD>
<BODY onLoad=āhello();ā onUnload=āgoodbye();ā>
<IMG SRC=/slideshow/tai-lieu-javascript/5089421/ātitle.gifā>
</BODY>
</HTML>
CaĆ¹c sƶĆÆ kieƤn vaĆø Form
Cac sƶĆÆ kieƤn ƱƶƓĆÆc sƶƻ duĆÆng ƱeĆ„ truy xuaĆ”t Form nhƶ:
OnClick, onSubmit, onFocus, onBlur, vaĆø onChange.
VĆ duĆÆ:
<INPUT TYPE=text NAME=ātestā VALUE=ātestā
onBlur=āalert(āThank You!ā);ā
onChange=ācheck(this);ā>
Khi giaĆ¹ trĆ² thay ƱoĆ„i function check() seƵ ƱƶƓĆÆc goĆÆi. Ta duĆøng tƶĆø khoĆ¹a this ƱeĆ„ chuyeĆ„n ƱoĆ”i tƶƓĆÆng cuĆ»a
trƶƓĆøng hieƤn haĆønh ƱeĆ”n haĆøm check()
BaĆÆn cuƵng coĆ¹ theĆ„ dƶĆÆa vaĆøo caĆ¹c phƶƓng phaĆ¹p vaĆø caĆ¹c thuoƤc tĆnh cuĆ»a ƱoĆ”i tƶƓĆÆng baĆØng phaĆ¹t bieĆ„u sau:
this.methodName() & this.propertyName.
VĆ duĆÆ:
<HTML> expressionā,āā);
<HEAD> calculate(form);
<TITLE>Example 5.3</TITLE> }
//STOP HIDING FROM OTHER BROWSERS -->
<SCRIPT LANGUAGE=āJavaScriptā> </SCRIPT>
<!-- HIDE FROM OTHER BROWSERS </HEAD>
function calculate(form) { <BODY>
form.results.value = <FORM METHOD=POST>
eval(form.entry.value); Enter a JavaScript mathematical
} expression:
function getExpression(form) { <INPUT TYPE=text NAME=āentryā
form.entry.blur(); VALUE=āā
form.entry.value = prompt(āPlease onFocus=āgetExpression(this.form);ā>
enter a JavaScript mathematical <BR>
JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 18
19. The result of this expression is: </FORM>
<INPUT TYPE=text NAME=āresultsā </BODY>
VALUE=āā </HTML>
onFocus=āthis.blur();ā>
MTWRFSS
formObjectName.fieldname:DuĆøng ƱeĆ„ chƦ teĆ¢n trƶƓĆøng cuĆ»a hieƤn haĆønh trong Form.
formObjectName.fieldname.value: duĆøng laĆ”y giaĆ¹ trĆ² cuĆ»a trƶƓĆøng form hieƤn haĆønh.
Sƶƻ duĆÆng voĆøng laĆ«p trong JavaScript
1 . VoĆøng laĆ«p for :
CuĆ¹ phaĆ¹p :
for ( init value ; condition ; update expression )
VĆ duĆÆ :
for (i = 0 ; i < 5 ; i++)
{
leƤnh ;
}
VĆ duĆÆ:
<HTML>
<HEAD>
<TITLE> for loop Examle </TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!- -
var name=prompt("What is your name?" ,"name");
var query= " " ;
document.write("<H1>" + name + " 's 10 favorite foods </H1> ");
for (var i=1 ;i<=10;i++)
{
JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 19
24. h . fontsize(size)
i . indexOf(character,[fromIndex])
j . italics()
k . lastIndexOf(character,[fromIndex])
l . link(URL)
m . small()
n . strike()
o . sub()
p . substring(startIndex,endIndex)
q . sup()
r . toLowerCase()
s . toUpperCase()
----------------------------
BieĆ¢n soaĆÆn: PhaĆÆm phuĆ¹ TaĆøi
Huu Thanh Design
JAVASCRIPT MEDIASPACE CLUB (HTD) PAGE: 24