Skip to main content

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

 

Comments

Popular posts from this blog

PHP#33MySQL#29 - Đếm lượt truy cập (KẾT THÚC KHÓA HỌC)

//hitcounter.php <?php session_start();  ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php  include("../connect.php"); //cập nhật số luowhtj truy cập: $sl="update counter set cnt=cnt+1"; mysqli_query($link,$sl); //Lấy dữ liệu số lượt truy cập để hiển thị ra web: $sl2="select * from counter"; $kq=mysqli_query($link,$sl2); $d=mysqli_fetch_array($kq); //Định dạng: $chuoi=str_pad($d['cnt'],6,"0",STR_PAD_LEFT); $luottrycap=""; for($i=0;$i<strlen($chuoi);$i++) { $tam=substr($chuoi,$i,1); $luottrycap.="<img src='images/$tam.png'/>"; } /*-Khi user truy cập vào website: +lần đầu tuy cập: Lưu thông tin của user lên csdl +Sau lần đầu tiên: cập nhtaaj lastvisit - Xóa các dòng dữ liệu quá 1 phút (Quy ước thời gian để khẳng định online hay offline: 1 phút) - Thống kê số liệu người dùng o...

2 (giao diện Visual C#) Solution Explorer

Bài 2 - SOME OTHER TIME - Vào dịp khác