You are on page 1of 5

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

HTM
1 of 5 3/28/2008 10:44 AM

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">

9 http://www.dayhoctructuyen.com/file.php/158/PART9.HTM
2 of 5 3/28/2008 10:44 AM

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 phe p; 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);
9 http://www.dayhoctructuyen.com/file.php/158/PART9.HTM
3 of 5 3/28/2008 10:44 AM
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 ba n:
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;
}
9 http://www.dayhoctructuyen.com/file.php/158/PART9.HTM
4 of 5 3/28/2008 10:44 AM
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()
9 http://www.dayhoctructuyen.com/file.php/158/PART9.HTM
5 of 5 3/28/2008 10:44 AM
+ 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

Xemktqua

You might also like