Download as pdf or txt
Download as pdf or txt
You are on page 1of 49

V

http://www.dayhoctructuyen.com/file.php/158/PART1.HTM

V.

Cac Thanh phn c ban cua javaScript

1. Cach s dung bin va ham trong JavaScript a. Cach khai bao bin lu tr thng tin: khai bao bin nao o ban s dung t khoa var u va khng cn khai bao kiu d liu. Cu phap: <var varname;> hoc <var varname=value;> Vi du: var str; hoc var num=60; Tn bin c tao thanh t cac ch cai, ch s va bt u la mt ch cai, JavaScript phn bit ch hoa va ch thng. Ta co th dung cac toan t +,-,*,/ va phep gan thao tac trn cac bin cua no: Vi du: var a=55; var b=a*2+10; b. hin thi kt qua ra trang html, ta co th dung ham write() Cach vit: window.document.write(<ni dung cn vit>); <ni dung cn vit> co th la mt bin, mt biu thc hay mt chui. c. Chui Phep gan chui: var str=welcom to javascript; Phep ni chui, ta s dung toan t cng + d. Ham alert(); Ham nay dung a ra mt thng bao vi ni dung c chi inh trong thn cua ham: Vi du: alert(t ni dung cua ban tai y); e. Ham prompt(); Ham nay yu cu ngi nhp nhp vao mt s thng tin nao o sau o se gan nhng gi ngi dung nhp vao cho mt bin. Cu phap: prompt(<mess>,<tempinput>); <mess> xut ra dong thng bao <tempinput> d liu nhp mu Vi du: var name = prompt(what is your name? ,input your name); Sau khi ban nhp vao t ban phim, gia tri se c lu vao bin name. f. Cac ham va phng thc v ngay thang; Trong JavaScript trang bi cho ta mt i tng ly lai ngay gi h thng cua may tinh o la i tng Date(); Cach khai bao nh sau: var date = new Date(); Sau o ta co th s dung nhng phng thc cua i tng nay ly lai ngay, thang, nm hay gi, phut, giy cua thi gian trong h thng may tinh. Mt s phng thc cua i tng nay: Phng thc
1 of 2

M ta
3/28/2008 10:41 AM

http://www.dayhoctructuyen.com/file.php/158/PART1.HTM

getDate() getDay() getHours() getMinutes() getSeconds() getTime() getYear() getMonth() Vi du:

Tra v ngay cua thang 1..31 Tra v ngay trong tun 0:chu nht; 1:th hai Tra v gi trong ngay 0..23 Tra v phut 0..59 Tra v giy 0..59 Tra v thi gian tinh tinh theo mili giy, tinh t 0:0:1-1-1970 Tra v nm Tra v thang 0..11

var now = new Date(); var d = now.getDate(); var m = now.getMonth()+1; var y = now.getYear(); var day=d+/+m+/+y; window.document.write(day);

Bai tp cung c
1. Trong javascript cac bin phai c khai bao kiu d liu cua no ung Sai 2. Mt hng xu ki t phai c t gia cp du ngoc sau: ngoc kep "" ngoc n '' ca hai 3. Ham prompt co bao nhiu i s 2 i s 3 i s khng gii han i s

4. Phng thc getDay() cua i tng kiu ngay Date tra v gia tri thuc pham vi T 0 n 7 T 0 n 30 Ca hai u sai 5. i tng kiu Date tra v ngay gi h thng cua: May khach May chu
Xem kt qua

2 of 2

3/28/2008 10:41 AM

http://www.dayhoctructuyen.com/file.php/158/PART2-3.HTM

2. Cac cu truc iu khin trong javascript a. Chuyn hng ngi dung n trang web khac chuyn hng ngi s dung n mt trang web khac ban co th s dung nh sau: Cu phap window.location =URL; URL: la mt tai nguyn, mt trang web trn mang hay may cua ban. Vi du: window.location =http://www.vnn.vn; b. Cu truc cu lnh if-then y la mt cu lnh iu kin trong ngn ng javaScript, ging nh trong cac ngn ng khac, cu phap cua cu lnh nay nh sau: if(<iu kin>) { <nhom lnh 1;> } [else{ <nhom lnh 2;> }] Nu iu kin nhn gia tri true thi se thc hin <nhom lnh 1> con khng se thc hin <nhom lnh 2 >. + Nu s dung phep so sanh bng trong biu thc iu kin ban phai Chu y: dun g hai du bng(==) nh trong ngn ng java. + Co th co hay khng co else i vi cu lnh if. + Co th co cac if else lng nhau. c. Gia tri boolean va cac phep so sanh cac phep toan so sanh tra v gia tri kiu boolean: Phep toan > < == >= <= != Y nghia Ln hn Nho hn So sanh bng Ln hn hay bng Nho hn hay bng Khac nhau

d. Toan t or, and va mod Toan t or c ky hiu bi 2 du || th hin phep hoc thng s dung trong biu thc iu kin. Toan t and c ky hiu bi 2 du && th hin phep va thng s dung trong biu thc iu kin. Toan t mod c ky hiu bi du % ly kt qua phn d cua phep chia. Chu y: Ging nh java, gp nhiu cu lnh n lai thanh mt cu lnh ghep ta co th s dung cp du ngoc moc {}. 3. Cac s kin x ly trn trang html cua ban Phn nay noi mt s thu thut x ly kich hoat cac s kin v mouse nh: di chuyn mouse trn i tng, click mouse vao i tng a. S kin onClick(): S kin nay c xay ra khi ban kich chut phai vao i

1 of 3

3/28/2008 10:41 AM

http://www.dayhoctructuyen.com/file.php/158/PART2-3.HTM

tng. Vi du: <a href=http://www.vnn.vn onClick=alert(hello); > hay <input type="submit" onClick="javascript:closeWindow()"> vi ham closeWindow() c vit nh sau: function closeWindow() { top.window.close(); } b. S kin onMouseOver(): s kin nay xay ra khi ta di chuyn chut ln mt i tng lin kt nao o. c. S kin onMouseOut(): s kin nay xay ra khi ta di chuyn chut ra ngoai mt i tng lin kt nao o. d. S kin ondblclick() S kin nay c xay ra khi ban kich i chut phai vao i tng. e. S kin onmousedown() S kin nay c xay ra khi chut phai c nhn xung trn i tng. f. S kin onmouseup() S kin nay c xay ra khi chut phai c nha ra trn i tng. Chu y: Cac S kin onkeypress(), onkeydown(), onkeyup() c xay ra khi ta nhn mt phim, nhn xung hay nha phim khi ta chon i tng o. Vi du: <a href="# onMouseOver=alert(inside object); onMouseOver=alert(outside object); onClick="alert(click);return false; > try </a> h. Thay i mau nn thay i mau nn cua trang html ban co th s dung: window.document.bgColor=red; Vi du: <a href="# onClick=" var bgcolor=prompt(input color,); window.document.bgColor=bgcolor; return false;> change background </a> i. Nap anh nn va cach thay i anh nn nap mt anh nn cho trang html, ban cn khai bao trong the <img> nh sau: <img scr=beyeu.gif name=mypic> co th thay i anh nn khi ban click vao i tng, ban phai t tn cho anh nn trong the <img> hin tai ging nh: name=mypic sau o ban co th lam cho anh nn thay i khac khi click chut vao lin kt: Vi du: <a href="# onClick=" window.document.mypic.scr=emyeu.bmp; return false;> change picture </a>

2 of 3

3/28/2008 10:41 AM

http://www.dayhoctructuyen.com/file.php/158/PART2-3.HTM

Bai tp cung c
1. Trong javascript m mt ca s mi ta s dung window.location window.open ca hai

2. Biu thc iu kin so sanh bng trong javascript: cp du bng du bng = ca hai == 3. Ham close() ong ca s ang m co bao nhiu i s khng gii han i 2 i s 3 i s s 4. S kin nao se c xay ra khi di chuyn chut trn i tng onClick onMouseOver onMouseOut 5. S kin nao xay ra khi chut phai c nhn trn i tng onClick onMouseup onMouseDown
Xem kt qua

Khng co

Khng co

3 of 3

3/28/2008 10:41 AM

http://www.dayhoctructuyen.com/file.php/158/PART2-3.HTM

2. Cac cu truc iu khin trong javascript a. Chuyn hng ngi dung n trang web khac chuyn hng ngi s dung n mt trang web khac ban co th s dung nh sau: Cu phap window.location =URL; URL: la mt tai nguyn, mt trang web trn mang hay may cua ban. Vi du: window.location =http://www.vnn.vn; b. Cu truc cu lnh if-then y la mt cu lnh iu kin trong ngn ng javaScript, ging nh trong cac ngn ng khac, cu phap cua cu lnh nay nh sau: if(<iu kin>) { <nhom lnh 1;> } [else{ <nhom lnh 2;> }] Nu iu kin nhn gia tri true thi se thc hin <nhom lnh 1> con khng se thc hin <nhom lnh 2 >. + Nu s dung phep so sanh bng trong biu thc iu kin ban phai Chu y: dun g hai du bng(==) nh trong ngn ng java. + Co th co hay khng co else i vi cu lnh if. + Co th co cac if else lng nhau. c. Gia tri boolean va cac phep so sanh cac phep toan so sanh tra v gia tri kiu boolean: Phep toan > < == >= <= != Y nghia Ln hn Nho hn So sanh bng Ln hn hay bng Nho hn hay bng Khac nhau

d. Toan t or, and va mod Toan t or c ky hiu bi 2 du || th hin phep hoc thng s dung trong biu thc iu kin. Toan t and c ky hiu bi 2 du && th hin phep va thng s dung trong biu thc iu kin. Toan t mod c ky hiu bi du % ly kt qua phn d cua phep chia. Chu y: Ging nh java, gp nhiu cu lnh n lai thanh mt cu lnh ghep ta co th s dung cp du ngoc moc {}. 3. Cac s kin x ly trn trang html cua ban Phn nay noi mt s thu thut x ly kich hoat cac s kin v mouse nh: di chuyn mouse trn i tng, click mouse vao i tng a. S kin onClick(): S kin nay c xay ra khi ban kich chut phai vao i

1 of 3

3/28/2008 10:42 AM

http://www.dayhoctructuyen.com/file.php/158/PART2-3.HTM

tng. Vi du: <a href=http://www.vnn.vn onClick=alert(hello); > hay <input type="submit" onClick="javascript:closeWindow()"> vi ham closeWindow() c vit nh sau: function closeWindow() { top.window.close(); } b. S kin onMouseOver(): s kin nay xay ra khi ta di chuyn chut ln mt i tng lin kt nao o. c. S kin onMouseOut(): s kin nay xay ra khi ta di chuyn chut ra ngoai mt i tng lin kt nao o. d. S kin ondblclick() S kin nay c xay ra khi ban kich i chut phai vao i tng. e. S kin onmousedown() S kin nay c xay ra khi chut phai c nhn xung trn i tng. f. S kin onmouseup() S kin nay c xay ra khi chut phai c nha ra trn i tng. Chu y: Cac S kin onkeypress(), onkeydown(), onkeyup() c xay ra khi ta nhn mt phim, nhn xung hay nha phim khi ta chon i tng o. Vi du: <a href="# onMouseOver=alert(inside object); onMouseOver=alert(outside object); onClick="alert(click);return false; > try </a> h. Thay i mau nn thay i mau nn cua trang html ban co th s dung: window.document.bgColor=red; Vi du: <a href="# onClick=" var bgcolor=prompt(input color,); window.document.bgColor=bgcolor; return false;> change background </a> i. Nap anh nn va cach thay i anh nn nap mt anh nn cho trang html, ban cn khai bao trong the <img> nh sau: <img scr=beyeu.gif name=mypic> co th thay i anh nn khi ban click vao i tng, ban phai t tn cho anh nn trong the <img> hin tai ging nh: name=mypic sau o ban co th lam cho anh nn thay i khac khi click chut vao lin kt: Vi du: <a href="# onClick=" window.document.mypic.scr=emyeu.bmp; return false;> change picture </a>

2 of 3

3/28/2008 10:42 AM

http://www.dayhoctructuyen.com/file.php/158/PART2-3.HTM

Bai tp cung c
1. Trong javascript m mt ca s mi ta s dung window.location window.open ca hai

2. Biu thc iu kin so sanh bng trong javascript: cp du bng du bng = ca hai == 3. Ham close() ong ca s ang m co bao nhiu i s khng gii han i 2 i s 3 i s s 4. S kin nao se c xay ra khi di chuyn chut trn i tng onClick onMouseOver onMouseOut 5. S kin nao xay ra khi chut phai c nhn trn i tng onClick onMouseup onMouseDown
Xem kt qua

Khng co

Khng co

3 of 3

3/28/2008 10:42 AM

http://www.dayhoctructuyen.com/file.php/158/PART6.HTM

6. Nhp va nhn lai thng tin t cac biu mu Biu mu la s thit kt, ni kt hp nhiu nhng i tng ln trn mt trang html cua ban dung nhp/xut gia tri vao/ra. Cac i tng o bao gm: Textfield, checkboxes, radio button, select, textarea a. Cach t tn cho cac i tng trong biu mu try cp n cac i tng trong biu mu, ban cn phai t tn cho cac i tng trong biu mu o, Qui tc t tn cho cac i tng ging nh t tn cho bin, t tn ham, tn khng co khoang trng. Tn cua cac i tng c xac inh trong t khoa name cua cac the cu th. Vi du: <input type=submit name=OK> <input type=text name=T1> OK: la tn cua nut submit T1: la tn cua TextField

b. oc va thit lp gia tri cho cac phn t + oc va thit lp gia tri cho cac trng vn ban: thit lp gia tri hay nhn lai gia tri t cac trng vn ban ban phai truy cp i cac i tng o theo cu phap sau: window.document.formname.objectname.value Trong o formname la tn cua form c xac inh trong the <form> thng t dong u tin cua phn <body> Vi du: <form name=myform> objectname: la tn cua i tng vn ban cn truy cp. gan gia tri cho trng vn ban o ban vit nh sau: window.document.formname.objectname.value=gia tri; nhn lai gia tri t cac trng vn ban ban vit: var st=window.document.formname.objectname.value; Vi du: <script language="JavaScript"> function docong() { var a=document.form.T1.value; var b=document.form.T2.value; document.form.T3.value=a+' + '+b+' = '; document.form.T4.value=a*1+b*1; } function clearS() { document.form.T3.value=; document.form.T4.value=; window.status="Hay nhap lai cho a va b "; return true; } </script>

1 of 4

3/28/2008 10:43 AM

http://www.dayhoctructuyen.com/file.php/158/PART6.HTM

Trong phn body vit nh sau: <form name="form" > <input type="text" name="T1" size="10">&nbsp;&nbsp;&nbsp; <input type="text" name="T2" size="10">&nbsp;&nbsp;&nbsp; <input type="text" name="T3" size="5" disabled> <input type="text" name="T4" size="11"> <b><a href="" onMouseOver="docong();return true;" onMouseout=" clearS();return true;">+</b> </form> Chu y: Cach nhn va nhn ly gia tri cho cac vung vn ban textarea cung thc hin tng t cac trng vn ban textField. + oc va thit lp gia tri cho cac hp kim tra: Thuc tinh checked cua i tng hp kim tra co th xac inh hp o co c chon hay khng va ta cung co th thit t thuc tinh cho cac hp chon nay: Cach thao tac trn trn cac i tng nay nh sau: window.document.formname.objectname.checked Trong o formname va objectname la nh trn. Vi du: xem mt hp kim tra co c chon hay khng ta vit nh sau: if(window.document.formname.objectname.checked==true) Va ta co th gan cho hp kim tra o ang ch c chon ta co th vit: window.document.formname.objectname.checked=true; Chu y: Nu ta khng xac inh tn cho tp hp cac hp kim tra checkbox, ta co th xac inh hp kim tra nao c chon co th truy cp thng qua phng thc elements truy cp n chi s cua phn t nao o trong hp kim tra. Ban co th tham khao vi du v phn nay trong phn mang muc tip theo. + oc va thit lp gia tri cho cac hp chon la: Vi tt ca cac hp chon la trong cung mt nhom co cung mt tn, do o nhn bit hp nao c chon javaScript se cho phep xac inh cac phn t cua hp chon la thng qua chi s phn t cua no. Vi du mt nhom co 4 hp chon la, nhom o co tn la radioname thi luc o ta co th truy cp n mi hp chon la thng qua chi s cua no, chi s phn t c xac inh bt u t 0. Vic kim tra ly lai trang thai hp chon la co th c thc hin theo cu phap sau: if(window.document.formname.objectname[i].checked) hay ta co th gan gia tri cho hp la chon nh sau: window.document.formname.radioname[0].checked=true; + oc va thit lp gia tri cho cac menu tha xung va danh sach cun: Ban co th oc va thit lp gia tri chon cho menu tha xung va danh sach cun ging nh hp kim tra hay hp chon thng qua chi s phn t cua no: Vi du: <form name=myform> <select name=selectlist >

2 of 4

3/28/2008 10:43 AM

http://www.dayhoctructuyen.com/file.php/158/PART6.HTM

<option value=male > <option value=female > </select> </form> Luc o ta co th bit menu tha xung nao c chon thng qua chi s cua no nh sau: if(window.document. myform.selectlist.options[0].checked==true) {alert(this is boy); } Ngoai ra ta co th xac inh c phn t nao c chon thng qua phng th selectedIndex, phng thc nay se tra v chi s phn t c chon trong menu tha xung, cach vit nh sau: var num = window.document.myform.selectlist.selectedIndex; Ta cung co th ly lai gia tri t phn t nao o c chon trong menu tha xung thng qua phng thc value nh sau: var num = window.document.myform.selectlist.selectedIndex; var val = window.document.myform.selectlist.options[num].value; +X ly cac s kin bng cach dung cac phn t cua biu mu Phn t Nut nhn Hp kim tra Nut chon la Trng vn ban Vung vn ban Chon la Biu mu S kin onClick onClick onClick onChange onChange onChange OnSubmit Hanh ng kich hoat

Thay i ni dung trng vn ban va sau o nhay chut bn ngoai trng vn ban o. Thay i ni dung vung vn ban va sau o nhay chut bn ngoai vung vn ban o. Thay i muc chon la trong menu tha xung Nhn Enter trong mt trng vn ban hay nhay chut vao nut submit.

Vi du: <form name= my onSubmit= window.location= window.document.my.T1.value; return false; > <input type =text name=T1 value=http://> <input type=submit value=go there> </form> Sau y la mt vi du s dung menu tha xung nh mt cng cu m mt trang mi theo ia chi cho sn: <script language="JavaScript">
3 of 4 3/28/2008 10:43 AM

http://www.dayhoctructuyen.com/file.php/158/PART6.HTM

function doit(site) { window.location=site; } </script> Sau o vit trong phn <body> nh sau: <form name=lam> <select name=sel onChange= doit(this.options[this.selectedIndex].value); > <option value=http://www.vnn.vn> Trang thng tin vit nam <option value=http://www.w3c.org> Trang Cng ngh IBM <option value=http://www.tegosoft.com> Microsoft learning <option value=http://www.sun.ibm.com> Java </select> Chu y: vi ta goi trc tip trong i tng select, th khoa this co th thay th cho i tng hin hanh nay: Ta co th thay th t this nay vi cum t thng dung: window.document.lam.sel

Bai tp cung c
1. Trong javascript gan gia tri cho cac i tng nh Textbox, radio, checkbox, option... ta gan thng qua phng thc value cua oi tng o ung Sai 2. vit mt ham tra v mt kt qua nao o ta phai khai bao return trong thn ham ung Khng ung 3. gan thuc tinh c chon(checked) cho i tng radio hay checkbox ta co th gan tri true cho phong thc checked cua i tng o Khng ung ung 4. T khoa this co th thay th cho window.document.<formname>.<objectname> khi nao Khng thay th Goi va x li trc tip trn i tng o Tt ca moi khi c 5. Khi nhn Enter trong trng vn ban hay click vao nut Submit thi s kin nao c goi: Onclick() OnSubmit() OnChange()
Xem kt qua

4 of 4

3/28/2008 10:43 AM

http://www.dayhoctructuyen.com/file.php/158/PART9.HTM

9. X ly chui a. Cac phng thc kim tra x ly chui + indexOf(s): Tra v vi tri cua chui s trong chui me tinh t u chui + lastIndexOf(s): Tra v vi tri cua chui s trong chui me tinh t cui chui tr v u chui. + charAt(i): Tra v ki t tai vi tri th i cua chui. + substring(m,n): Tra v mt chui con ly t chui me tai vi tri m, ly n ki t. + split(s): Ct chui me thanh nhiu chui con vi ky t ngn cach cac chui la s, tri tra v cua ham nay la mt mang cac chui con. Vi du: <html><head> <script language="JavaScript"> <!-function validdate(s){ if (s.split("/").length==3) { var d= s.split("/")[0]; var m= s.split("/")[1]; var y= s.split("/")[2]; alert("Hom nay la ngay "+d+" thang "+m+" nam "+y); } else alert("invalid date"); } --> </script></head><body> <form name="form"> <input type="text" name="t1" value ="0" size="20"> <input type="button" value="Kiem tra" size="20" onClick="validdate(window.document.form.t1.value);"> </form></body></html> Ban cn chu y thuc tinh maxlength, disabled, checked cua i cac tng text, button, radio, checkbox trong form. Vi du: <input type="text" name="t1" maxlength ="10" size="20"> <input type="button" value="See Hidden disabled> <input type="radio" name="Hidden" checked> <input type="text" name="t2" disabled size="20">

1 of 5

3/28/2008 10:44 AM

http://www.dayhoctructuyen.com/file.php/158/PART9.HTM

b. Chng trinh vi du: Sau y la mt s ham mu x ly d liu khi nhp vao t mt s i tng: + Ham kim tra d liu nhp vao t mt TextField co ung theo qui inh hay khng.
/********************************************************** // ham: validString // form -- Tn form // object -- Tn i tng // lenMax -- dai ln nht // lenMin -- dai nho nht // space -- Cho phep chui cha khoang trng hay khng (0- cho phep; 1- khng ) // require -- Cho phep hay khng cho phep bo qua (0- cho phep; 1- khng) //**************************************************************

function validString(form, object, lenMax, lenMin, space, require ){ var tmp, tmp2, msg; tmp2 = " "; msg = ""; tmp = document.forms(form).all(object).value; len1 = tmp.length; if (require == 1){ if (len1 < 1){ msg = 'Invalid '; alert(msg); document.forms(form).all(object).focus(); return false; } } if (lenMax != lenMin){ if (len1 > lenMax){ msg = 'Invalid ' + lenMax; alert(msg); document.forms(form).all(object).focus(); return false; } if (len1 < lenMin){ msg = 'Invalid ' + lenMin; alert(msg); document.forms(form).all(object).focus(); return false; } } if (space == 1){ for(i=0; i<len1; i++){ if (tmp.charAt(i) == tmp2){ msg = 'Invalid '; alert(msg);

2 of 5

3/28/2008 10:44 AM

http://www.dayhoctructuyen.com/file.php/158/PART9.HTM

document.forms(form).all(object).focus(); return false; } } } return true; } 10. Cookie va HTML ng a. Cookie La mt mu thng tin ma khi ban truy cp vao mt website no se tao ra va c lu lai di ia cng cua ngi s dung. + Thit lp mt cookie Ta thit lp cookie cho document bng cach: document.cookie= <chui cookie> Sau o la s dung ham escape() ma hoa cookie cho ban: Vi du: function setcookie() { var name= prompt(Input your name ,); var cookiename=username=+escape(name); document.cookie=cookiename; } + oc mt cookie Ta s dung ham unescape() giai ma cookie cua ban : Vi du: function readcookie() { var thecookie= document.cookie; var cookiename= thecookie.split(=); var name= cookiename[1]; name=unescape(name); alert(your name: +name); }

b. HTML ng va khai nim c ban v CSS. + The DIV The nay cho phep ngi dung co th inh vi mt the HTML bt ky vi tri nao trn trang cua ban. y la cach lam cho trang web cua ban ng, co th thy hinh anh di chuyn c trn man hinh. Vi du: function movediv(which) { var thediv; if (window.document.all) { thediv=window.document.all.mydiv.style; }
3 of 5 3/28/2008 10:44 AM

http://www.dayhoctructuyen.com/file.php/158/PART9.HTM

else if(document.layers) { thediv=window.document.mydiv; }else { alert("xin chao");return; } thediv.left=parseInt(thediv.left)+10*(Math.random()*10-5); thediv.top=parseInt(thediv.top)+10*(Math.random()*10-5); } Sau o trong phn <body> ban vit nh sau: <body onClick="movediv()"> 11. Mt s phng thc thng dung: a. applet Tt ca moi applet trn trang c lu vao mang thng qua cach truy cp nh sau: window.document.applets[]; kich hoat no ta s dung phng thc start() va tt no ta s dung phng thc stop(). Vi du: window.document.applets[0].start(); b. Area Cac the area c lu trong mang sau: window.document.links[]; Mi Area c lu nh mt i tng va ta co th truy cp no nh nhng i tng khac. Mt s phng thc thng dung: + confirm(): Se a ra mt cu thng bao va Tra v tri true hay false nu ngi dung chon YES hay NO. + toUpperCase(): Chuyn mt chui thanh chui ch hoa. + toLowerCase(): Chuyn mt chui thanh chui ch thng. + focus(): Se di chuyn con tro n i tng ma ta thit t focus(), thng s dung trong vic kim tra li va yu cu ngi dung nhp lai. + history.back(): Tr lai trang trc o. + eval(s): inh gia tri cua mt biu thc. + bgColor(): Thit t mau nn. + concat(): ghep ni chui. Vi du: function setcolorbg () { window.document.bgColor="#008800"; return false; }

c. Cac ham toan hoc: + abs()

4 of 5

3/28/2008 10:44 AM

http://www.dayhoctructuyen.com/file.php/158/PART9.HTM

+ acos() + asin() + atan() + cos() + exp() + log() + max(a,b) + min(a,b) + pow(e,x) + random() + round() + sin() + sqrt() + tan()

Bai tp cung c
1. ct mt xu ly t thanh cac chui con thng qua ky t ngn cach nao o ta s dung phng thc SubString() Split() Ca hai 2. tra v vi tri cua chui con trong chui me tinh t u chui ta s dung IndexOf() LastIndexOf() SubString() 3. gii han dai ti a cua mt i tng Textbox khi nhp d liu t ban phim ta s dung maxlength Disabled Hidden 4. thit lp ch bao mt cua mt trang web nao o ta s dung Session Cookie Ca hai co th 5. ma hoa mt i tng cookie ta dung Unescape() escape()

Ca hai

Xem kt qua

5 of 5

3/28/2008 10:44 AM

Ngn ng JavaScript

http://www.dayhoctructuyen.com/file.php/158/Overviewjavascript1.htm

Ngn ng Javascript
Ngi soan: L Thanh Lm

I.

Gii Thiu

JavaScript la mt ngn ng lp trinh c xy dng sn trn cac trinh duyt web, la mt trong nhng cach tt nht cho phep ban co th b sung tinh nng tng tac cho webSite cua minh, no la ngn ng cho php lam vic trc tip vi cac trinh duyt. JavaScript rt n gian, d hoc, khng cn phai co bt ky mt phn mm hay phn cng nao yu cu khi thc thi chng trinh JavaScript ma ban chi cn co trinh duyt trn may la u. Ban khng cn phai co mt kin thc v may tinh, v ngn ng lp trinh mi co th vit c ngn ng nay. JavaScript la mt ngn ng hoan chinh, no cho phep ban kha nhiu cng cu lp trinh. Ngay nay, JavaScript hu nh c s dung rt ph dung trn hu ht cac webSite trn th gii.

II.

Muc ich

Giup ban co kha nng t vit ring nhng oan JavaScript ring cua minh, co kha nng oc hiu nhng chng trinh JavaScript do ngi khac vit. Giup ban co th tim kim thng tin, hoc v JavaScript thng qua mt s webSite:
http://www.w3schools.com http://www.javascript.com http://www.developer.earthweb.com/directories/pages/dir.javascript.html

Thng qua mt s vi du mu ban co th hoc hoi cach xy dng mt chng trinh JavaScript. Tao cac hiu ng trn cac trang web cua ban rt xinh ng.

III. u im
Tng tac trc tip vi cac trang web cua ban. s dung cac chng trinh vit bng JavaScript ban chi cn thm cac ma ngun cua ban vao gia the <script> trong phn <head> cua trang html cua ban: <script language = "JavaScript"> code script.. </script> Sau o ban co th s dung cac ham cua minh trong trang html. Hoc bn c th chn 1 file ngun javascript nh sau: <script language="JavaScript" src="........."></script>

IV. Nhc im
3/28/2008 10:37 AM

1 of 2

Ngn ng JavaScript

http://www.dayhoctructuyen.com/file.php/158/Overviewjavascript1.htm

JavaScript khng th tng tac, giao tip vi may chu. JavaScript khng th tao ra cac hinh anh hoa. JavaScript lam vic co th khac nhau trn cac trinh duyt khac nhau.

V.

Cac Thanh phn c ban cua javaScript

1. Cach s dung bin va ham trong JavaScript 2. Cac cu truc iu khin trong javascript 3. 4. 5. 6. 7. 8. 9. Cac s kin x ly trn trang html cua ban M va thao tac cac ca s Xy dng ham javaScript cho ban Nhp va nhn lai thng tin t cac biu mu Mang va vong lp inh thi gian cho cac s kin X ly chui

Bai tp cung c
.1. Theo ban bit Javascript co th nhung vao c nhng loai trang web nao: Trang HTML Trang Servlet Trang JSP 2. Trong Javascript ban co th x ly cac hinh anh hoa c khng? c Khng 3. Vic vit nhng oan chng trinh JavaScript trong trang web vi muc ich Tng hiu qua xu ly trn Kim tra d Lam cho tc cac trang liu nhanh hn 4. Javascript co phai la mt ngn ng lp trinh khng? Co Khng 5. Ban co th vit nhng oan ma javascript vao trang web cua minh tai ni nao Title Head Body
Xem kt qua

Tt ca

2 of 2

3/28/2008 10:37 AM

BI 1

http://www.dayhoctructuyen.com/file.php/158/b1_files/b01.htm

BI 1: GII THIU V ASP


1. GII THIU V ASP.
ASP (Active Server Pages) l mt mi trng lp trnh cung cp cho vic kt hp HTML, ngn ng kch bn (Scripting) nh VBScript, Javacript, v cc thnh phn c vit trong cc ngn ng nhm to ra mt ng dng Internet mnh m v hon chnh.

2. ASP file l g?
File c to vi phn m rng .ASP. Trong file ny cha cc th HTML, Cc kch bn Scripting nh VBSCript, JavaScript hay cc li gi n cc components(Nh DLL v ActiveX control) Cc script ca ASP c nm trong cp th <% .. %> Khi cn sa i cc file ASP ta ch cn ghi li trn server thi. Vo nhng ln sau khi trang A c gi, cc Script trong file ASP t ng bin dch li. Cng ngh ASP c xy dng trc tip bn trong ; IIS(WinNT, 2000, XP), Personal Webserver.

3. ASP lm vic nh th no ?
Trang HTML tnh:

Trang web ng (Dynamic):

1 of 3

3/28/2008 10:51 AM

BI 1

http://www.dayhoctructuyen.com/file.php/158/b1_files/b01.htm

4. Gii thiu v IIS - Internet Information Server.


a. IIS l g? Microsoft Internet Information Server l mt ng dng server chuyn giao thng tin bng vic s dng giao thc chuyn i siu vn bn HTTP b. IIS c th lm c g? Xut bn mt Home page ln Internet. To cc giao dch thng mi in t trn Internet( Qu trnh giao dch, t hng) Cho php ngi dng t xa c th truy xut C s d liu (Data Base Remote Access)

5. IIS hot ng nh th no ?
Web, v c bn thc s l mt h thng cc yu cu (Request) v cc p ng (Response). IIS phn hi li cc yu cu i thng tin ca Web Browser. IIS lng nghe cc yu cu t pha Users trn mt mng s dng WWW.

6. Ci t IIS. Thc hnh trn my

2 of 3

3/28/2008 10:51 AM

BI 1

http://www.dayhoctructuyen.com/file.php/158/b1_files/b01.htm

7. V d v file ASP.
<html> <body> <% response.write("Hello World!") %> </body> </html> <html> <body> <%="Hello World!"%> </body> </html>

3 of 3

3/28/2008 10:51 AM

BI 2: LP TRNH VI ASP
1. Khai bo bin.
Khng bt buc nhng nn khai bo kim sot v bt li. Nn s dng <% Option Explicit %> ngay u mi tp ASP. C php: Dim bin 1, bin 2 khai bo mng: 1. Dim a(10) : ch s chy t 0->10 do c 11 phn t. 2. Khi khai bo nn s dng cc tin t: Dng d liu Tin t (prefix) Boolean Bln Byte Byt Double Dbl Integer Int Long Lng Object Obj String Str ADO command Cmd ADO connection Cnn ..

V d:
<html> <body> <% dim name name="Donald Duck" response.write("My name is: " & name) %> </body> </html>

2. Khai bo hng s.
CONST tn hng= gi tr

3. Cc i tng xy dng sn trong ASP.


Trong ASP c 5 i tng phc v cho vic s dng ton b. + Request: Ly thng tin t User + Response: Gi thng tin t Server ti User. + Server: Dng iu khin IIS + Session: Dng lu tr cc thng tin nh cc ci t, thay i cho mt phin lm vic hin thi ca User. + Application: Dng chia s cc thng tin cp ng dng v iu khin cc thit lp cho ton b qu trnh chy ng dng

BI 3: CU TRC IU KIN-LP.
1. Cc cu trc iu khin. A. CU TRC IU KIN IF..THEN..ELSE..IF
C php: <% If <iu kin 1> then <Cc cu lnh> Else If <iu kin 2> then <Cc cu lnh> End if End if %> Ngoi ra c th s dng cu trc IF.. THEN. . . . ELSEIF. . . END IF <% If <iu kin 1> then <Cc cu lnh 1> ElseIf <iu kin 2> then <Cc cu lnh 2> Else <Cc cu lnh 3> End if %>

B. CU TRC LA CHN SELECT CASEEND SELECT


C php: <% Select Case <tn bin> Case <gi tr 1> <Nhm lnh 1> Case < gi tr 2> <Nhm lnh 2> Case < gi tr n> <Nhm lnh n> Case Else <Nhm lnh n+1> %> V d: Hin ngy gi trn my ch v cho bit hm nay l th my?

C. CU TRC LP TUN T FORNEXT.


FOR. C php: <%

Dng lp vi s ln bit, tuy nhin ta c th ngt bng lnh EXIT

FOR <tn bin>=<gi tr u> TO <gi tr bin> STEP <bc nhy> NEXT %>
<html> <body> <% Dim famname(5),i famname(0) = "Jan Egil" famname(1) = "Tove" famname(2) = "Hege" famname(3) = "Stale" famname(4) = "Kai Jim" famname(5) = "Borge" For i = 0 to 5 response.write(famname(i) & "<br />") Next %> </body> </html>

V D:

D. CC CU TRC LP .
D.1 DO WHILE.LOOP. C php: <% Do while <iu kin> <Cc cu lnh> Loop %> D.2 WHILE.WEND. C php: <% While < iu kin > <Cc cu lnh> Wend %> D.3 DO.LOOP UNTIL C php: <% DO <Cc cu lnh> Loop until <iu kin> %> E. CC V D .

2. Mt s hm hu dng trong ASP.


A. Cc Hm x l vn bn i. TRIM(xu as string): B khong trng hai u k t ii. LEFT(Xu as string, n as interger): Ly bn tri xu n k t. iii. RIGHT(Xu as string, n as interger): Ly bn phi xu n k t. iv. LCASE(Xu as string) : Chuyn xu v ch thng v. UCASE(Xu as string) : Chuyn xu v ch hoa vi. MID(xu as string, n1, n2): Ly n2 k t trong xu bt u t v tr n1. vii. CSTR(Bin): Hm chuyn i bin thnh kiu string viii. Hm JOIN/SPLIT(Xu as string, k t ngn cch): S Ni/Ct xu thnh mt/nhiu on bng cch xc nh k t ngn cch trn v cho cc on ln lt vo mt mng. V du: <% x=H ni;Hi phng; TPHCM y=split(x,;) Response.write y(0) %> B. Cc hm x l s. i. SQR(n): Cn bc hai ca n ii. INT(n) : Ly phn nguyn n iii. MOD : Php ng d iv. Ton t \ : Chia nguyn v. Round(s, n) : Lm trn s vi n ch s thp phn vi. RND(): Tr v s ngu nhin bt k trong khong [0,1] C. Cc hm v ngy thng thi gian.

BI 4: XY DNG NG DNG ASP.


1. Cc th HTML.
Cn xem li cc tag HTML c bit l: FORM(name, method, action), INPUT(TEXT, BUTTON, SUBMIT), CHECKBOX, RADIO, OPTION. Mi th TAG HTML phi c xc nh tn ca n(name), gi tr ca n(value), v cc thuc tnh khc. V d: <INPUT TYPE=TEXT NAME=T1 VALUE=TH READONLY>

2. Th FORM.
Trong mt trang c th c nhiu FORM, mi form xc nh mt tp cc hot ng ca n, form phi c t 1 tn(name), trong mi form c th c nhiu i tng nh TEXTBOX, BUTTON, SUBMIT, OPTION.. Mi form c phng thc(Method) chuyn d liu(nhn hay gi d liu n bao gm 2 phng thc GET hay POST), v phi xc nh chuyn d liu n u thng qua ACTION, v vy khai bo Form thng thng chng ta phi khai bo nh sau:

<FORM NAME=formname METHOD=get/post ACTION=url>


V d:

Cho form sau gm Textbox c tn l T1 <form name=lam method="get" action="simpleform.asp"> First Name: <input type="text" name="T1" /> <br /> <input type="submit" value="Submit" /> </form> nhn li d liu ta c th s dng 1 trong 2 cch sau: Request.QueryString hoc Request.Form. * S dng Request.QueryString Lnh Request.QueryString thng i km vi form s dng phng thc GET ( method="get"). Thng tin gi t Form c phng thc GET s c hin th trn thanh address bar ca trnh duyt v n b gii hn bi thong tin c gi(nh s i s, gi tr d liu).

Nu bn nhp gi tr cho T1 l DONGA th trn thanh a ch s hin th nh sau:

http://maychu1/05TH1a/06th0010/simpleform.asp?T1=DONGA
Gi s ta c file ASP c tn "simpleform.asp" cha on m sau:

<body> Welcome: <% response.write(request.querystring("T1")) %> </body>


Kt qu hin th ra mn hnh:

Welcome DONGA

* S dng Request.Form
Cu lnh Request.Form dung nhn gi tr t form vi phng thc GET( method="post"). Thng tin gi t form vi phng thc POST s khng b gii hn bi i s v d liu. If a user typed "Bill" and "Gates" in the form example above, the URL sent to the server would look like this:

http://maychu1/05TH1a/06th0010/simpleform.asp
Gi s file asp "simpleform.asp" cha on m sau:

<body> Welcome <% response.write(request.form("T1")) %> </body>


Kt qu:

Welcome DONGA

3. Th INPUT. 4. To vng nhp liu. 5. To COMBO BOX. V d 1: S dng Request.QueryString


<html> <body> <form action="demo_reqquery.asp" method="get">

Your name: <input type="text" name="fname" size="20" /> <input type="submit" value="Submit" /> </form> <% dim fname fname=Request.QueryString("fname") If fname<>"" Then Response.Write("Hello " & fname & "!<br />") Response.Write("How are you today?") End If %> </body> </html>

V d 2:

<html> <% dim cars cars=Request.Form("cars") %> <body> <form action="demo_radiob.asp" method="post"> <p>Please select your favorite car:</p> <input type="radio" name="cars" <%if cars="Volvo" then Response.Write("checked")%> value="Volvo">Volvo</input> <br /> <input type="radio" name="cars" <%if cars="Saab" then Response.Write("checked")%> value="Saab">Saab</input> <br /> <input type="radio" name="cars" <%if cars="BMW" then Response.Write("checked")%> value="BMW">BMW</input> <br /><br /> <input type="submit" value="Submit" /> </form> <% if cars<>"" then Response.Write("<p>Your favorite car is: " & cars & "</p>") end if %> </body> </html>

S dng RADIO

BI 5: H QUN TR CSDL MS Access 2003


CN XEM LI CC KHI NIM

I. CC KHI NIM C BN 1/ H Qun Tr CSDL? 2/ C S D Liu l g? 3/ Table l g? 4/ Kha chnh/Primary key? 5/ Kha ngoi/Foreign key? 6/ Quan h/ relationship? 7/ Rng buc, tham chiu d liu/references? 8/ Duy nht d liu / Unique? 9/ Truy vn/ query? II. SQL - Structure Query Language 1/ Ngn ng nh ngha d liu 2/ Ngn ng thao tc d liu

Bi 7: KT NI DATABASE.
1. ADO l g?
ADO C th dung truy cp DL t trang web.
z z z z z

ADO l cng ngh ca Microsoft ADO c vit tt l ActiveX Data Objects ADO l mt thnh phn ca Active-X ADO t ng ci t khi ci IIS ADO l mi trng lp trnh giao tip vi CSDL- database

1. Cch truy xut database bng ADO trong ASP.


Cch thng thng truy cp CSDL t trang web ASP nh sau: 1. 2. 3. 4. 5. 6. 7. Create an ADO connection to a database Open the database connection Create an ADO recordset Open the recordset Extract the data you need from the recordset Close the recordset Close the connection

2. To kt ni ADO (ADO Connection).


Trc khi truy cp d liu, bn phi thit lp kt ni n CSDL . C 02 cch thit lp kt ni.

a/ Kt ni trc tip( DSN-less Database Connection) y l cch n gin nht thng kt ni vi Microsoft Access database. Gi s bn c mt Database c tn "northwind.mdb" t ti th mc "c:/webdata/", bn c th kt ni n CSDL nh sau: <% set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open "c:/webdata/northwind.mdb" %> b/ Kt ni thng qua ODBC(ODBC Database Connection)

Nu bn c ODBC database c tn l "northwind" bn c th kt ni vo CSDL ny nh sau: <% set conn=Server.CreateObject("ADODB.Connection") conn.Open "northwind" %> Vi kt ni ODBC connection, Bn c th kt ni n bt k CSDL no v bt k my no trong h thnga mng ca bn, min l ODBC connection tn ti. Cch to ODBC Connection vi MS Access Database 1. Open the ODBC icon in your Control Panel. 2. Choose the System DSN tab. 3. Click on Add in the System DSN tab. 4. Select the Microsoft Access Driver. Click Finish. 5. In the next screen, click Select to locate the database. 6. Give the database a Data Source Name (DSN). 7. Click OK.

3. S dng ADO Recordset


c th c d liu t database, u tin d liu phi c np vo recordset. Cch to ADO Recordset Sau khi ADO Database Connection c to, tip theo ta phi to ADO Recordset. Gi s ta c database c tn "Northwind", Chng ta c th truy cp d liu t bn "Customers" nh sau: <% set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open "c:/webdata/northwind.mdb" set rs=Server.CreateObject("ADODB.recordset") rs.Open "Customers", conn %> Cch to ADO SQL Recordset Chng ta c th truy cp d liu t bn "Customers" s dng cu lnh SQL:

<% set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open "c:/webdata/northwind.mdb" set rs=Server.CreateObject("ADODB.recordset") rs.Open "Select * from Customers", conn %>

Ly d liu t Recordset Sau khi recordset c m, chng ta c th ly d liu recordset. Chng ta c th truy xut d liu t bn Customers trong CSDL Northwind nh sau : <% set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open "c:/webdata/northwind.mdb" set rs=Server.CreateObject("ADODB.recordset") rs.Open "Select * from Customers", conn for each x in rs.fields response.write(x.name) response.write(" = ") response.write(x.value) next %>

4. Mt s v d.
Hin th tn Field v gi tr ca Field Chng ta c th truy xut d liu t bn Customers trong CSDL Northwind nh sau : <html> <body> <% set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open "c:/webdata/northwind.mdb" set rs = Server.CreateObject("ADODB.recordset") rs.Open "SELECT * FROM Customers", conn do until rs.EOF for each x in rs.Fields Response.Write(x.name)

Response.Write(" = ") Response.Write(x.value & "<br />") next Response.Write("<br />") rs.MoveNext loop rs.close conn.close %> </body> </html> Kt qu CustomerID = ALFKI CompanyName = Alfreds Futterkiste ContactName = Maria Anders ContactTitle = Sales Representative Address = Obere Str. 57 City = Berlin PostalCode = 12209 Country = Germany CustomerID = BERGS CompanyName = Berglunds snabbkp ContactName = Christina Berglund ContactTitle = Order Administrator Address = Berguvsvgen 8 City = Lule PostalCode = S-958 22 Country = Sweden CustomerID = CENTC CompanyName = Centro comercial Moctezuma ContactName = Francisco Chang ContactTitle = Marketing Manager Address = Sierras de Granada 9993 City = Mxico D.F. PostalCode = 05022 Country = Mexico .... .... ....

Hin th tn Field v gi tr ca Field trong HTML table <html> <body> <% set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open "c:/webdata/northwind.mdb" set rs = Server.CreateObject("ADODB.recordset") rs.Open "SELECT Companyname, Contactname FROM Customers", conn %> <table border="1" width="100%"> <%do until rs.EOF%> <tr> <%for each x in rs.Fields%> <td><%Response.Write(x.value)%></td> <%next rs.MoveNext%> </tr> <%loop rs.close conn.close %> </table> </body> </html> Kt qu: Alfreds Futterkiste Berglunds snabbkp Centro comercial Moctezuma Ernst Handel FISSA Fabrica Inter. Salchichas S.A. Galera del gastrnomo Island Trading Kniglich Essen Laughing Bacchus Wine Cellars Magazzini Alimentari Riuniti North/South Paris spcialits Rattlesnake Canyon Grocery Simons bistro Maria Anders Christina Berglund Francisco Chang Roland Mendel Diego Roel Eduardo Saavedra Helen Bennett Philip Cramer Yoshi Tannamuri Giovanni Rovelli Simon Crowther Marie Bertrand Paula Wilson Jytte Petersen

The Big Cheese Vaffeljernet Wolski Zajazd

Liz Nixon Palle Ibsen Zbyszek Piestrzeniewicz

Thm Headers vo Table <html> <body> <% set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open "c:/webdata/northwind.mdb" set rs = Server.CreateObject("ADODB.recordset") sql="SELECT Companyname, Contactname FROM Customers" rs.Open sql, conn %> <table border="1" width="100%"> <tr> <%for each x in rs.Fields response.write("<th>" & x.name & "</th>") next%> </tr> <%do until rs.EOF%> <tr> <%for each x in rs.Fields%> <td><%Response.Write(x.value)%></td> <%next rs.MoveNext%> </tr> <%loop rs.close conn.close %> </table> </body> </html> Kt qu: Companyname Alfreds Futterkiste Berglunds snabbkp Centro comercial Moctezuma Ernst Handel Contactname Maria Anders Christina Berglund Francisco Chang Roland Mendel

FISSA Fabrica Inter. Salchichas S.A. Galera del gastrnomo Island Trading Kniglich Essen Laughing Bacchus Wine Cellars Magazzini Alimentari Riuniti North/South Paris spcialits Rattlesnake Canyon Grocery Simons bistro The Big Cheese Vaffeljernet Wolski Zajazd

Diego Roel Eduardo Saavedra Helen Bennett Philip Cramer Yoshi Tannamuri Giovanni Rovelli Simon Crowther Marie Bertrand Paula Wilson Jytte Petersen Liz Nixon Palle Ibsen Zbyszek Piestrzeniewicz

5. QUERY
Display Selected Data Chng ta ch hin th nhng bn ghi t bn "Customers" m trng "Companyname" bt u bng ch A <html> <body> <% set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open "c:/webdata/northwind.mdb" set rs=Server.CreateObject("ADODB.recordset") sql="SELECT Companyname, Contactname FROM Customers WHERE CompanyName LIKE 'A%'" rs.Open sql, conn %> <table border="1" width="100%"> <tr> <%for each x in rs.Fields response.write("<th>" & x.name & "</th>") next%> </tr> <%do until rs.EOF%> <tr> <%for each x in rs.Fields%> <td><%Response.Write(x.value)%></td> <%next

rs.MoveNext%> </tr> <%loop rs.close conn.close%> </table> </body> </html> Sort the Data Hin th "Companyname" v "Contactname" t bn "Customers", sp xp bi "Companyname": <html> <body> <% set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open "c:/webdata/northwind.mdb" set rs = Server.CreateObject("ADODB.recordset") sql="SELECT Companyname, Contactname FROM Customers ORDER BY CompanyName" rs.Open sql, conn %> <table border="1" width="100%"> <tr> <%for each x in rs.Fields response.write("<th>" & x.name & "</th>") next%> </tr> <%do until rs.EOF%> <tr> <%for each x in rs.Fields%> <td><%Response.Write(x.value)%></td> <%next rs.MoveNext%> </tr> <%loop rs.close conn.close%> </table> </body> </html>

Add a Record to a Table in a Database Chng ta c th them 1 bn ghi mi vo CSDL Northwind. Trc tin bn phi to ra 01 form nhp liu: <html> <body> <form method="post" action="demo_add.asp"> <table> <tr> <td>CustomerID:</td> <td><input name="custid"></td> </tr><tr> <td>Company Name:</td> <td><input name="compname"></td> </tr><tr> <td>Contact Name:</td> <td><input name="contname"></td> </tr><tr> <td>Address:</td> <td><input name="address"></td> </tr><tr> <td>City:</td> <td><input name="city"></td> </tr><tr> <td>Postal Code:</td> <td><input name="postcode"></td> </tr><tr> <td>Country:</td> <td><input name="country"></td> </tr> </table> <br /><br /> <input type="submit" value="Add New"> <input type="reset" value="Cancel"> </form> </body> </html> Khi bn bm vo nt submit(add New) s chuyn sang file "demo_add.asp". "demo_add.asp" cha on m thm 01 record mi vo bn Customers nh sau: <html> <body> <%

set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open "c:/webdata/northwind.mdb" sql="INSERT INTO customers (customerID,companyname," sql=sql & "contactname,address,city,postalcode,country)" sql=sql & " VALUES " sql=sql & "('" & Request.Form("custid") & "'," sql=sql & "'" & Request.Form("compname") & "'," sql=sql & "'" & Request.Form("contname") & "'," sql=sql & "'" & Request.Form("address") & "'," sql=sql & "'" & Request.Form("city") & "'," sql=sql & "'" & Request.Form("postcode") & "'," sql=sql & "'" & Request.Form("country") & "')" on error resume next conn.Execute sql,recaffected if err<>0 then Response.Write("No update permissions!") else Response.Write("<h3>" & recaffected & " record added</h3>") end if conn.close %> </body> </html> Update a Record in a Table Bn mun cp nht li thng tin 1 record trong bn Customers trong CSDL: Northwind. u tin ta hin th tt c cc records trong bng Customers, mi record ly customerID lm tham s : <html> <body> <% set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open "c:/webdata/northwind.mdb" set rs=Server.CreateObject("ADODB.Recordset") rs.open "SELECT * FROM customers",conn %> <h2>List Database</h2> <table border="1" width="100%"> <tr> <% for each x in rs.Fields

response.write("<th>" & ucase(x.name) & "</th>") next %> </tr> <% do until rs.EOF %> <tr> <form method="post" action="demo_update.asp"> <% for each x in rs.Fields if lcase(x.name)="customerid" then%> <td> <input type="submit" name="customerID" value="<%=x.value%>"> </td> <%else%> <td><%Response.Write(x.value)%></td> <%end if next %> </form> <%rs.MoveNext%> </tr> <% loop conn.close %> </table> </body> </html> Khi bn click vo "customerID" s gi n file "demo_update.asp". tp tin "demo_update.asp" file ny cha on m to ra form cha cc field t customerID . Form ny c 01 nt lnh "Update record" s thc hin vic lu d liu: <html> <body> <h2>Update Record</h2> <% set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open "c:/webdata/northwind.mdb" cid=Request.Form("customerID") if Request.form("companyname")="" then set rs=Server.CreateObject("ADODB.Recordset") rs.open "SELECT * FROM customers WHERE customerID='" & cid & "' %>

<form method="post" action="demo_update.asp"> <table> <%for each x in rs.Fields%> <tr> <td><%=x.name%></td> <td><input name="<%=x.name%>" value="<%=x.value%>"></td> <%next%> </tr> </table> <br /><br /> <input type="submit" value="Update record"> </form> <% else sql="UPDATE customers SET " sql=sql & "companyname='" & Request.Form("companyname") & "'," sql=sql & "contactname='" & Request.Form("contactname") & "'," sql=sql & "address='" & Request.Form("address") & "'," sql=sql & "city='" & Request.Form("city") & "'," sql=sql & "postalcode='" & Request.Form("postalcode") & "'," sql=sql & "country='" & Request.Form("country") & "'" sql=sql & " WHERE customerID='" & cid & "'" on error resume next conn.Execute sql if err<>0 then response.write("No update permissions!") else response.write("Record " & cid & " was updated!") end if end if conn.close %> </body> </html> Delete a Record in a Table Bn mun xa 1 record trong bng Customers trong CSDL Northwind. Trc tin bn phi hin th ra tt c cc record trong bng chn xa: <html> <body> <% set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open "c:/webdata/northwind.mdb"

set rs=Server.CreateObject("ADODB.Recordset") rs.open "SELECT * FROM customers",conn %> <h2>List Database</h2> <table border="1" width="100%"> <tr> <% for each x in rs.Fields response.write("<th>" & ucase(x.name) & "</th>") next %> </tr> <% do until rs.EOF %> <tr> <form method="post" action="demo_delete.asp"> <% for each x in rs.Fields if x.name="customerID" then%> <td> <input type="submit" name="customerID" value="<%=x.value%>"> </td> <%else%> <td><%Response.Write(x.value)%></td> <%end if next %> </form> <%rs.MoveNext%> </tr> <% loop conn.close %> </table> </body> </html> Nu bn click vo ct "customerID" mt file mi c gi n "demo_delete.asp". File "demo_delete.asp" cha on m hin th d liu bn mun xa. Tip c nt lnh "Delete record" xa record m bn chn: <html> <body> <h2>Delete Record</h2> <%

set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open "c:/webdata/northwind.mdb" cid=Request.Form("customerID") if Request.form("companyname")="" then set rs=Server.CreateObject("ADODB.Recordset") rs.open "SELECT * FROM customers WHERE customerID='" & cid & "' %> <form method="post" action="demo_delete.asp"> <table> <%for each x in rs.Fields%> <tr> <td><%=x.name%></td> <td><input name="<%=x.name%>" value="<%=x.value%>"></td> <%next%> </tr> </table> <br /><br /> <input type="submit" value="Delete record"> </form> <% else sql="DELETE FROM customers" sql=sql & " WHERE customerID='" & cid & "'" on error resume next conn.Execute sql if err<>0 then response.write("No update permissions!") else response.write("Record " & cid & " was deleted!") end if end if conn.close %> </body> </html>

TNG QUAN V NGN NG LT WEB


CNG C LP TRNH WEB HIN NAY MS(ASP, ASP.NET) JAVA SUN (JSP, SERVLET) PHP CC THUT NG C BN HTTP: Hypertext Transfer Protocol FTP: File Transfer Protocol HTML: HyperText Markup Language(DHTML, XHTML) XML: eXtensible Markup Language CSS: Cascading Style Sheets JAVASCRIPT: (Multi Web Server Service) VBSCRIPT: (only MS Web Server Service) ASP: Active Server Pages JSP: JAVA Server Pages CGI: Common Gateway Interface PWS: Personal Web Server IIS: Internet Information Services DOMAIN NAME HOSTING POST Mt s i tng WEB FLASH APPLET CSS Trong iu kin v gii hn ca mn hc ny, chng ta s c nghin cu ngn ng lp trnh JAVASCRIPT v ASP.
Phn JAVASCRIPT c gii thiu trn Ni dung c th ca NNLT ASP: Bi 1: Tng Quan v NNLT ASP Bi 2: CC THNH PHN C BN Bi 3: CC CU TRC IU KHIN Bi 4: XY DNG NG DNG TRONG ASP Bi 5: H QUN TR CSDL - SQL

Bi 6: KT NI CSDL - ADO Bi 7: PHN TCH V THIT K HTTT QUN L.

You might also like