Professional Documents
Culture Documents
Javascript
Javascript
avascript la mot ngon ng thong dch (interpreter), chng trnh nguon cua no c nhung
(embedded) hoac tch hp (integated) vao tap tin HTML chuan. Khi file c load trong
Browser (co support cho JavaScript), Browser se thong dch cac Script va thc hien cac cong viec
xac nh. Chng trnh nguon JavaScript c thong dch trong trang HTML sau khi toan bo trang
c load nhng trc khi trang c hien th.
Javascript la mot ngon ng co ac tnh:
n gian.
ong (Dynamic).
Hng oi tng (Object Oriented).
2. Ngon ng JavaScript:
Mot trong nhng ac tnh quan trong cua ngon ng JavaScript la kha nang tao va s dung cac oi
tng (Object). Cac Object nay cho phep ngi lap trnh s dung e phat trien ng dung.
Trong JavaScript ,cac Object c nhn theo 2 kha canh:
a. Cac Object a ton tai.
b. Cac Object do ngi lap trnh xay dng.
Trong cac Object a ton tai c chia thanh 2 kieu:
a. Cac Object cua JavaScript (JavaScript Built-in Object).
b. Cac oi tng c cung cap bi moi trng Netscape.
JAVASCRIPT
Mo ta
Cung cap cac phng phap va cac tnh chat cho ca so hien hanh
cua trnh duyet,bao gom cac oi tng cho moi frame.
PAGE: 1
Location
History
Document
Cung cap cac tnh chat va phng phap lam viec vi cac a ch
URL hien hanh c m.
Cac oi tng history cung cap thong tin ve cac danh sach cu va
co the gii han s tng tac vi danh sach.
ay la mot oi tng c s dung nhieu nhat .No cha ng cac
oi tng,tnh chat va cac phng phap lam viec vi cac thanh
phan cua tai lieu nh cac :form,link,anchor,applet.
JAVASCRIPT
PAGE: 2
PAGE: 3
V du 1:
<HTML>
<HEAD>
<TITLE>Outputting Text</TITLE>
</HEAD>
<BODY>
This is text plain <BR>
<B>
<SCRIPT LANGUAGE="Javascript">
<!- document.write("This is text bold </B>");
- ->
</SCRIPT>
</BODY>
</HTML>
V du 2:
document.write('<IMG
SRC="welcome.gif">');
document.write("<BR><H1>WELCOME TO
NETSCAPE 2.1</H1>");
- ->
</SCRIPT>
</BODY>
</HTML>
<HTML>
<HEAD>
<TITLE>Example 2.4 </TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="Javascript">
<!- -
document.writeln("Two,");
document.write("Three");
document.write("...");
- ->
</SCRIPT>
</PRE>
</BODY>
</HTML>
JAVASCRIPT
PAGE: 4
V du 2:
document.write("<H1>Greeting ,");
document.writeln(prompt("enter your
name:","name"));
document.write("Welcome to netscape
navigator 2.01 </H1>");
- ->
</SCRIPT>
</BODY>
</HTML>
<HTML>
<HEAD>
<TITLE>Listing 2.6</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="Javascript">
<!- document.write('<IMG
SRC="welcome.gif">');
<HTML>
<HEAD>
<TITLE>Listing 2.6</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="Javascript">
<!- document.write('<IMG
SRC="welcome.gif">');
JAVASCRIPT
PAGE: 5
<HTML>
<HEAD>
<TITLE>Example 3.1</TITLE>
<SCRIPT LANGUAGE="Javascript">
<!- var name=prompt("enter your
name:","name");
- ->
</SCRIPT>
</HEAD>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="Javascript">
<!-document.write('<IMG
SRC="Welcome.gif">');
document.write("<H1>Greeting ," +
name + " Welcome to netscape
navigator 2.01 </H1>");
-->
</SCRIPT>
</BODY>
</HTML>
JAVASCRIPT
PAGE: 6
*= Nhan ben trai cho ben phai,gan ket qua cho ben trai
x*=y => x=50
/= Chia ben trai cho phai ,gan ket qua lai cho ben trai
x/=y => x=2
%= Chia ben trai cho ben phai va lay so d gan lai cho ben trai
x%=y => x=0
* Cac toan t khac:
V du:
x+=15+3
y=++x; (=> y=6 v x tang len 6)
=> x=18
z=x++; (=> z=6 v sau o x gan cho z)
8+5
sau o x tang 1 => x=7
32.5 * 72.3
Do o ta co ket qua cuoi cung la:
12 % 5
x=7;y=6;z=6;
Dau ++ va dau - - va dau V du: x=5;
V du:
x=-x => x=-5
x=5;
Phep toan Logic
&& : va
||: hoac
! not
V du:
x=5 ,y=2 ,c=3
(x>y) && (x>c)
false && anything is always false
(x>y) || (c<x) => true
!x
Toan t so sanh trong JavaScript:
==
1==1 => true
!=
3<1 =>false
>
5 >=4 =>true
<
the != he => true
>=
4==4 =>true
<=
V du:
Toan t ieu kien:
Cu phap:
(ieu kien ) ? gia tr 1 : gia tr 2
Neu ieu kien ung th tra ve gia tr 1
Neu ieu kien sai th tra ve gia tr 2
V du:
JAVASCRIPT
PAGE: 7
<HTML>
<HEAD>
<TITLE>Example 3.3</TITLE>
<SCRIPT LANGUAGE="Javascript">
var question="What is 10+10 ?";
var answer=20;
var correct='<IMG
SRC="correct.gif">';
var incorrect='<IMG
SRC="incorect.gif">';
var response=prompt(question,"0");
<BODY>
<SCRIPT LANGUAGE="Javascript">
<!-document.write(output);
-->
</SCRIPT>
</BODY>
</HTML>
If (day!=Saturday) {
document.writeln(Its not Saturday);
}
S dung cau truc else if cho v du tren
If (day==Saturday) {
document.writeln(Its the weekend);
}
else {
document.writeln(Its not Saturday);
}
JAVASCRIPT
PAGE: 8
if ieu kien 2 {
Cac lenh JavaScript
} else {
cac lenh khac
}
Cac lenh JavaScript
} else {
Cac lenh khac
}
V du 1 : S dung phng phap confirm() vi phat bieu if
response=prompt(question,"0");
<HTML>
<HEAD>
<TITLE>Example 3.3</TITLE>
<SCRIPT LANGUAGE="Javascript">
var question="What is 10+10 ?";
var answer=20;
var correct='<IMG
SRC="correct.gif">';
var incorrect='<IMG
SRC="incorect.gif">';
var response=prompt(question,"0");
if (response != answer) {
if (confirm("Wrong ! press OK for
a second change"))
<HTML>
<HEAD>
<TITLE>Example 3.3</TITLE>
<SCRIPT LANGUAGE="Javascript">
var question="What is 10+10 ?";
var answer=20;
var correct='<IMG
SRC="correct.gif">';
var incorrect='<IMG
SRC="incorect.gif">';
var response=prompt(question,"0");
if (response != answer) {
if (confirm("Wrong ! press OK for
a second change"))
response=prompt(question,"0");
}else {
if (confirm("Correct ! press OK
for a second question"))
{
response=prompt(question,"0");
}
}
var output = (response ==answer ) ?
correct:incorrect ;
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="Javascript">
<!-document.write(output);
-->
</SCRIPT>
</BODY>
</HTML>
MTWRFSS
JAVASCRIPT
PAGE: 9
V du:
Goi ham printName()vi lenh sau printName(Bob);
Khi ham printName()c thi hanh gia tr cua name la "Bob" neu goi ham
printName()vi oi so la mot bien
= Mr. + name;
JAVASCRIPT
PAGE: 10
V du:
MTWRFSS
<HTML>
<HEAD>
<TITLE>Example 4.1</TITLE>
<SCRIPT LANGUAGE=JavaScript>
<!-- HIDE FROM OTHER BROWSERS
//DEFINE FUNCTION testQuestion()
function testQuestion(question) {
//DEFINE LOCAL VARIABLES FOR THE
FUNCTION
var answer=eval(question);
var output=What is + question +
?;
var correct=<IMG
SRC=correct.gif>;
var incorrect=<IMG
SRC=incorrect.gif>;
//ASK THE QUESTION
var response=prompt(output,0");
V du 2:
<HTML>
<HEAD>
<TITLE>Example 4.2</TITLE>
<SCRIPT LANGUAGE=JavaScript>
<!-- HIDE FROM OTHER BROWSERS
//DEFINE FUNCTION testQuestion()
function
testQuestion(question,chances) {
JAVASCRIPT
PAGE: 11
var incorrect=<IMG
SRC=incorrect.gif>;
4
//ASK THE QUESTION
var response=prompt(output,0");
//CHECK THE RESULT
if (chances > 1) {
return (response == answer) ? correct
: testQuestion(question,chances-1);
} else {
return (response == answer) ? correct
: incorrect;
}
}
student1.name
student1.age
student1.grade
V du e tao oi tng student2
student2 = new student(Jane,9,82);
JAVASCRIPT
PAGE: 12
V du:
4
<HTML>
<HEAD>
<TITLE>Example 4.3</TITLE>
<SCRIPT LANGUAGE=JavaScript>
<!-- HIDE FROM OTHER BROWSERS
//DEFINE METHOD
JAVASCRIPT
function displayInfo() {
document.write(<H1>Employee Profile:
+ this.name + </H1><HR><PRE>);
document.writeln(Employee Number:
+ this.number);
PAGE: 13
document.writeln(Social Security
Number: + this.socsec);
document.writeln(Annual Salary: +
this.salary);
document.write(</PRE>);
}
//DEFINE OBJECT
function employee() {
this.name=prompt(Enter Employees
Name,Name);
this.number=prompt(Enter Employee
Number for + this.name,000-000");
this.socsec=prompt(Enter Social
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">
<!-- Begin
var day="";
var month="";
var ampm="";
var ampmhour="";
var myweekday="";
var year="";
mydate = new Date();
myday = mydate.getDay();
mymonth = mydate.getMonth();
myweekday= mydate.getDate();
weekday= myweekday;
myyear= mydate.getYear();
year = myyear;
JAVASCRIPT
myhours = mydate.getHours();
ampmhour = (myhours > 12) ? myhours 12 : myhours;
ampm = (myhours >= 12) ? 'Buo i Chie u '
: ' Buo i Sa ng ';
mytime = mydate.getMinutes();
myminutes = ((mytime < 10) ? ':0' :
':') + mytime;
if(myday == 0)
day = " Chu Nha t , ";
else if(myday == 1)
day = " Th hai, ";
else if(myday == 2)
day = " Th ba, ";
else if(myday == 3)
PAGE: 14
<body>
<script>
document.write("<b><font color=#0000ff face='VNITimes,helvetica,arial'>" + ampmhour + "" + myminutes + ampm)
document.write(" - " + day + " nga y " + myweekday +" ");
document.write( month + " , na m " + year + "</font>");
</script>
</body>
PAGE: 15
Ten s kien
blur
click
change
focus
load
mouseover
select
submit
unload
Mo ta
Xay ra khi iem tap trungcua ngo vao c di chuyen ra
khoi mot thanh phan cua Form (Khi user click ra ngoai mot
trng)
Khi user Click vao 1 link hoac thanh phan cua Form.
Xay ra khi gia tr cua Form Field b thay oi bi user.
Xay ra khi ngo vao tap trung vao thanh phan cua Form
Xay ra khi mot trang c Load vao trong bo duyet.
Xay ra khi User di chuyen mouse qua mot Hyperlink.
Xay ra khi User chon 1 trng cua thanh phan Form.
Xay ra khi User xac nhan a nhap xong d lieu.
Xay ra khi User ri khoi trang Web.
JAVASCRIPT
PAGE: 16
Selection list
Text element
Textarea element
Button element
Checkbox
Radio button
Hypertext link
Reset button
Submit button
Document
Window
Form
onLoad, onUnload
onLoad, onUnload
onSubmit
5
Cach dung bo quan ly s kien 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=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=title.gif>
</BODY>
</HTML>
Vi du
MTWRFSS
<HTML>
<HEAD>
JAVASCRIPT
PAGE: 17
<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=title.gif>
</BODY>
</HTML>
JAVASCRIPT
expression,);
calculate(form);
}
//STOP HIDING FROM OTHER BROWSERS -->
</SCRIPT>
</HEAD>
<BODY>
<FORM METHOD=POST>
Enter a JavaScript mathematical
expression:
<INPUT TYPE=text NAME=entry
VALUE=
onFocus=getExpression(this.form);>
<BR>
PAGE: 18
</FORM>
</BODY>
</HTML>
MTWRFSS
JAVASCRIPT
PAGE: 19
JAVASCRIPT
PAGE: 20
PAGE: 21
g.
4)
a.
b.
c.
window
Cac hanh vi (Methods) cua oi tng window
alert() : Hien 1 thong bao trong hop thoai vi OK button.
close() : ong ca so hien hanh.
open() : M mot ca so mi vi 1 tai lieu c ch ra hoac m mot tai lieu trong mot ten ca
so c ch nh.
d . prompt() : Hien mot hop thong bao
e . setTimeout() :
f . clearTimeout() :
Hanh vi nay cung cap cach goi phat bieu JavaScript sau mot khoang thi gian troi qua .Ngoai
ra oi tng window co the thc hien event handler : onLoad=statement
Lam viec vi status bar
Khi user di chuyen qua mot hyperlink ta co the hien ra mot thong bao tai thanh status bar cua
bowser da vao event handler onMouseOver va bang cach at self.status la mot chuoi (hoac
window.status).
V du:
<HTML>
<HEAD>
<TITLE>Status Example</TITLE>
<BODY>
<A HREF=plc.htm onMouseOver=self.status=Chuyen de PLC ;return true ; >Lop chuyen de PLC </A>
<A HREF=tkweb.htm onMouseOver=self.status=Thiet Ke Trang Web ;return true ; >Thiet Ke Web</A>
</BODY>
</HTML>
M va ong cac ca so
S dung phng phap open() va close() ta co the ieu khien viec m va ong ca so cha tai
lieu.
open (URL , WindowName , featureList) ;
Cac ac iem trong phng phap open() gom co:
toolbar : tao mot toolbar chuan
location: tao mot vung location
directories: tao cac button th muc chuan
status: tao thanh trang thai.
menubar : tao thanh menu tai nh cua ca so
scrollbars: tao thanh scroll bar
resizable: cho phep user thay oi kch thc ca so
width : ch nh chieu rong ca so theo n v pixel
height : ch nh chieu cao ca so theo n v pixel
V du:
window.open( plc.htm,newWindow,toolbar=yes,location=1,directories=yes,status=yes,
JAVASCRIPT
PAGE: 22
menubar=1,scroolbar=yes,resizable=0,copyhistory=1,width=200,height=200);
V du:
<HTML>
<HEAD>
<TITLE>WINDOWS</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!-function openWindow(url,name) {
popupWin = window.open(url, name, "scrollbars=yes,width=800, heigth=200 ");
}
-->
</SCRIPT>
</HEAD>
<BODY>
<a href="javascript:openWindow('../chuyende/plc.htm','Win')">PLC</a>,
<a href="javascript:openWindow('../chuyende/suachuaw.htm','stoogeWin')">Sua chua</a>,
<a href="javascript:openWindow('../chuyende/tkweb.htm','stoogeWin')">Thiet ke web</a>
</BODY>
</HTML>
PAGE: 23
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()
JAVASCRIPT
----------------------------
PAGE: 24