Tra Từ Điển

Thời gianTiếtThứ 2Thứ 3Thứ 4Thứ 5Thứ 6
8g40 - 9g101Tiếng Việt
Tiếng Anh
Tiếng Việt
Tiếng Việt
9g10 - 9g302Nghỉ giải lao
Tiếng Anh
Nghỉ giải lao
Nghỉ giải lao
9g30 - 10g3Toán
Toán
Toán

Saturday, November 6, 2021

LAB JAVASCRIPT

LAB JAVASCRIPT

1.     Kiểm tra form flogin

-        Tạo form  như sau: .

// File login.html

<form id="flogin" name="flogin" method="post" action="xulylogin.html">

Username:<br/> <input name="u" id="u" type="text" /> <br/>

Password:<br/> <input name="p" id="p" type="password"/> <br/>

<input type="submit" name="sub" id="sub" value="Login" />

</form>

 

-        Tạo file javaScript checklogin.js

function checklogin(){

     if(document.flogin.u.value==''){

           alert("Bạn chưa nhập username");

           document.flogin.u.focus(); //Chọn đối tượng u

          return false;   // Chặn form không cho submit

     }

     if(document.flogin.p.value=='') {

           alert("Bạn chưa nhập password");

           document.flogin.p.focus(); //Chọn đối tượng p

           return false;  

     }   

     return true; // Cho submit form khi đã kiểm tra xong

}

 

-         Chèn file checklogin.js vào file login.html và gọi hàm kiểm tra form như sau:

<head>

<script type="text/javascript" src="checklogin.js"></script>

</head>

<body>

<form onsubmit="return checklogin();" id="flogin" name="flogin" method="post" action="xulylogin.php">

2.      Kiểm tra form search

-        Tạo form sau:

<form id=formtim name=formtim action="kqtim.html" method="get">

<input  name="tukhoa" id="tukhoa" type="text"  value="Tìm kiếm"/>   

<input  name="btntim" id="btntim" type="submit" value=" TÌM "/>   

</form>

-        Chọn text field tukhoa è vào code và thêm code như sau.

<input onclick="if(this.value=='Tìm kiếm') this.value=''" onblur="if(this.value=='') this.value='Tìm kiếm'" name="tukhoa" id="tukhoa" type="text" value="Tìm kiếm" />   

 

-        Viết hàm kiểm tra form search

// file checksearch.js

function checksearch (){

     if(document.formtim.tukhoa.value=='Tìm kiếm')    {

           alert("Bạn chưa nhập từ khóa tìm kiếm…");  

           document.formtim.tukhoa.focus();     

           return false;   // Chặn form không cho submit

     }         

     return true; // Cho submit form khi đã kiểm tra xong

}

 

-        Chèn file checksearch.js và gọi hàm kiểm tra form như sau:

<head>

<script type="text/javascript" src="checksearch.js"></script>

</head>

<body>

<form onsubmit="return checksearch();" id="formtim" name="formtim" action="kqtim.html" method="get">

3.     Dò độ phân giải 

Dò độ phân giải để chuyển cho user trang web thích hợp với độ phân giải của họ

<script type="text/javascript">
if (
screen.width==800) document.location='index1.html';
else if (
screen.width==1024) document.location='index2.html';
else
document.location='index3.html';
</script>

4.     Dò browser của user

Dò browser để chuyển cho user trang web thích hợp

<script type="text/javascript">

if (navigator.userAgent.indexOf('MSIE')>0) document.location='index1.html';

else if (navigator.userAgent.indexOf('Firefox')>0) document.location='index2.html';

else document.location='index3.html';

</script>

5.     Hiện giờ hiện hành

<script type="text/javascript">

function tg(){

var currentTime = new Date();

var month = currentTime.getMonth() + 1;

var day = currentTime.getDate();

var year = currentTime.getFullYear();

var hour = currentTime.getHours();

var minutes = currentTime.getMinutes();

var seconds = currentTime.getSeconds();

document.getElementById("dongho").innerHTML = day + "/"  + month  +

 "/" + year + " " + hour + ":" + minutes+":"+seconds;

}

setInterval("tg()",1000);

</script>

<span id="dongho"></span>

6.     Đếm giảm dần để chuyển trang

<script>

var sogiay=5;

setTimeout("document.location='index.html", sogiay *1000);

setInterval("document.getElementById('sogiay').innerHTML=sogiay--",1000);

</script>

 

<center>

<p> Đã thực thi xong </p>

<p><a href='index.html>Quay lại trang chủ</a></p>

<P>Sẽ quay lại trang chủ sau <span id="sogiay"></span> &nbsp;giây nữa

</center>

7.     Mở đóng window

a.      Tạo trang a.html, gõ trong body:

<style>

.do{ background-color:#993333}

.xanh{ background-color:#0099FF}

</style>

<script>

function openWin(){

w = window.open('b.html','CSMoi','width=300,height=150,menubar=no,

toolbar=no,left=250,top=150');

w.focus();

}

</script>

 

<input type="button" onclick="openWin()" value="Mở window" />

b.     Tạo trang b.html, gõ trong body:

<p align="center">Đổi màu window cha</p>

<p align="center">

<input type="button" value="Màu đỏ" onclick="chuyenmau('do')"/> &nbsp;

<input type="button" value="Màu xanh" onclick="chuyenmau('xanh')" />

</p>

 

<p align="center">

<a href="#" onclick="window.close()">Đóng</a>

</p>

 

<script>

function chuyenmau(mau){

window.opener.document.body.className=mau;

}

</script>

c.      Test

 

0 comments:

Post a Comment