Professional Documents
Culture Documents
Javascript PDF
Javascript PDF
J 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.
oi tng Mo ta
Window 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.
V du 2:
<HTML> document.write('<IMG
<HEAD> SRC="welcome.gif">');
<TITLE>Example 2.4 </TITLE> document.write("<BR><H1>WELCOME TO
</HEAD> NETSCAPE 2.1</H1>");
<BODY> - ->
<SCRIPT LANGUAGE="Javascript"> </SCRIPT>
<!- - </BODY>
</HTML>
V du 2:
<HTML> document.write("<H1>Greeting ,");
<HEAD> document.writeln(prompt("enter your
<TITLE>Listing 2.6</TITLE> name:","name"));
</HEAD> document.write("Welcome to netscape
<BODY> navigator 2.01 </H1>");
<SCRIPT LANGUAGE="Javascript"> - ->
<!- - </SCRIPT>
document.write('<IMG </BODY>
SRC="welcome.gif">'); </HTML>
if ieu kien {
Ma JavaScript
}
V du: If (day!=Saturday) {
if (day==Saturday) { document.writeln(Its not Saturday);
document.writeln(Its the weekend); }
alert( Its the weekend); S dung cau truc else if cho v du tren
} If (day==Saturday) {
V du: document.writeln(Its the weekend);
If (day==Saturday) { }
document.writeln(Its the weekend); else {
} document.writeln(Its not Saturday);
}
Trong ky thuat lap trnh cac lap trnh vien thng s dung ham e thc hien mot oan chng
trnh the hien cho mot module nao o e thc hien mot cong viec nao o.
Trong Javascript co cac ham c xay dng san e giup ban thc hien mot chc nang nao o v
du nh ham alert(), document.write(), parseInt() va ban cung co the nh ngha ra cac ham khac
cua mnh e thc hien mot cong viec nao o cua ban, e nh ngha ham ban theo cu phap sau:
function function_name(parameters, arguments)
{
command block
}
function cube(number) {
var cube = number * number * number;
return cube;
}
student1.name
student1.age
student1.grade
4
function student(name, age, grade, mother) {
this.name = name;
this.age = age;
this.grade = grade;
this.mother = mother;
}
student1.displayProfile();
V du:
4
<HTML> function displayInfo() {
<HEAD> document.write(<H1>Employee Profile:
<TITLE>Example 4.3</TITLE> + this.name + </H1><HR><PRE>);
<SCRIPT LANGUAGE=JavaScript> document.writeln(Employee Number:
<!-- HIDE FROM OTHER BROWSERS + this.number);
//DEFINE METHOD
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)
<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>
T khoa this: quy cho oi tng hien hanh.Trong Javascript Form la mo oi tng.Cac thanh
phan cua Form bao gom text fields, checkboxes, radio buttons, buttons, va selection lists.
V du:
<INPUT TYPE=text onChange=checkField(this)>
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>
V du:
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>
MTWRFSS
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++)
{
<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,