Hiển thị các bài đăng có nhãn Code Javascript Hay. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn Code Javascript Hay. Hiển thị tất cả bài đăng

Thứ Ba, 9 tháng 6, 2009

Cách làm hiện thời gian load của trang web

Cách làm hiện thời gian load của trang web
Hiện thời gian load của trang web cũng là 1 yếu tố giúp cho trang của bạn trở nên pro hơn. Đôi khi nó trở nên không cần thiết nhưng dù sao bạn vẫn được cái quyền thể hiện tính pro của site mình chứ hả.

Để hiện thời gian load của trang, bạn cài đặt như sau:

1. Đặt đoạn code này vào ngay sau thẻ mở của trang web:

<script type="text/javascript"> var timing = new Date(); _startTime = timing.getTime(); </script>

2. Đặt tiếp đoạn code dưới đây vào vị trí mà bạn muốn hiện thời gian load. Thường thì code này phải đặt dưới chân trang, càng gần thẻ đóng thì thời gian load được tính càng chính xác.

<script type="text/javascript"><br />//<![CDATA[ var timing=new Date(); _endTime=timing.getTime(); document.write("Page load <"+"b>"+(_endTime-_startTime)/1000+"s<"+"/b>");<br />//[[><br /></script>

Thứ Ba, 2 tháng 6, 2009

Code JavaScript: Gửi địa chỉ mới URL

Bất cứ khi nào bạn muốn sinh ra 1 trang động bạn thường phải dùng đến ngôn ngữ viết như PHP hoặc ASP bên cạnh 1 vài máy chủ.

Nhưng hãy giả thiết rằng chỉ có 3 hoặc 4 trang mà chỉ có thể chọn từ biểu mẫu từ 1 thực đơn thả xuống (1 combo-box). Thủ tục sau sẽ hỗ trợ bạn tạo 1 thực đơn thả xuống được hỗ trợ JavaScript.

Mẫu đầu tiên:

<form name=="pages"> 
<select name=="pg_choice" onChange=="javascript: take_there();">
<option value=="no_page">Select a page to go to...</option>
<option value=="pg1.html">Page 1</option>
<option value=="pg2.html">Page 2</option>
<option value=="pg3.html">Page 3</option>
</select>
</form>

Biểu mẫu này giới thiệu tới bạn 3 sự lựa chọn của trang. Trong thuộc tính onChange của nó chúng ta quyết định hàm nào để gọi tới gửi lại địa chỉ để tới trang tương ứng. Và bây giờ JavaScript(ghi chú rằng tôi đã chèn 1 dấu . giữa dấu < và script vì rằng đọc email không là đối tượng để gửi của 1 đoạn JavaScript nhúng trong message của bạn):

<.script language=="javascript"> 
function take_there()
{
var destination==document.pages.pg_choice.value;
var version == navigator.appVersion;
// sets variable == browser version
if(destination!=="no_page")
{
if (version.indexOf("MSIE") >== -1)
// checks to see if using IE
{
window.location.href=?stination;
}
else
{
window.open(destination, target=="_self");
} } }
</script>

Những bộ duyệt khác nhau hỗ chợ những thứ khác nhau sao cho bạn thường phải kiểm tra bộ duyệt nào mà người dùng đang sử dụng.

Lệnh địa chỉ mới trang được sử dụng tương ứng.

Tạo đồng hồ bằng javascript

Dưới đây là đoạn code chương trình hiển thị đồng hồ bằng javascript, Bạn có thể thay đổi màu của đồng hồ

1. Chèn đoạn code sau vào tệp tài liệu HTML
Code:
<script type="text/javascript">
function showtime(){
if (!document.all& &!document.getElementById)
return
thelement=document.getElementById? document.getElementById("tick2"): document.all.tick2
var Digital=new Date()
var hours=Digital.getHours()
var minutes=Digital.getMinutes()
var seconds=Digital.getSeconds()
var dn="PM"
if (hours<12)
dn="AM"
if (hours>12)
hours=hours-12
if (hours==0)
hours=12
if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
var ctime=hours+":"+minutes+":"+seconds+" "+dn
thelement.innerHTML="<b style='font-size:14;color:blue;'>"+ctime+"</b>"
setTimeout("showtime()",1000)
}
window.onload=showtime
</script>

2. Chèn vào trong thẻ này <span id=tick2></span>

3. Để thay đồi màu của đồng hồ
thelement.innerHTML="<b style='font-size:14;color:blue;'>"+ctime+"</b>"

Bạn có thể đổi màu "color:blue;" (xanh) thành "color:red;" (đỏ) hoặc "color:black;" (đen)…..

Code 2:

<script language="JavaScript">

//Analog clock script- By VnTim™ (admin@vntim.co.cc)
//Script featured on Dynamic Drive
//Visit http://vntim.blogspot.com/ for this script and more

fCol='444444'; //face colour.
sCol='FF0000'; //seconds colour.
mCol='444444'; //minutes colour.
hCol='444444'; //hours colour.

Ybase=30; //Clock height.
Xbase=30; //Clock width.

H='...';
H=H.split('');
M='....';
M=M.split('');
S='.....';
S=S.split('');
NS4=(document.layers);
NS6=(document.getElementById&&!document.all);
IE4=(document.all);
Ypos=0;
Xpos=0;
dots=12;
Split=360/dots;
if (NS6){
for (i=1; i < dots+1; i++){
document.write('<div id="n6Digits'+i+'" style="position:absolute;top:0px;left:0px;width:30px;height:30px;font-family:Arial;font-size:10px;color:#'+fCol+';text-align:center;padding-top:10px">'+i+'</div>');
}
for (i=0; i < M.length; i++){
document.write('<div id="Ny'+i+'" style="position:absolute;top:0px;left:0px;width:2px;height:2px;font-size:2px;background:#'+mCol+'"></div>');
}
for (i=0; i < H.length; i++){
document.write('<div id="Nz'+i+'" style="position:absolute;top:0px;left:0px;width:2px;height:2px;font-size:2px;background:#'+hCol+'"></div>');
}
for (i=0; i < S.length; i++){
document.write('<div id="Nx'+i+'" style="position:absolute;top:0px;left:0px;width:2px;height:2px;font-size:2px;background:#'+sCol+'"></div>');
}
}
if (NS4){
dgts='1 2 3 4 5 6 7 8 9 10 11 12';
dgts=dgts.split(' ')
for (i=0; i < dots; i++){
document.write('<layer name=nsDigits'+i+' top=0 left=0 height=30 width=30><center><font face=Arial size=1 color='+fCol+'>'+dgts[i]+'</font></center></layer>');
}
for (i=0; i < M.length; i++){
document.write('<layer name=ny'+i+' top=0 left=0 bgcolor='+mCol+' clip="0,0,2,2"></layer>');
}
for (i=0; i < H.length; i++){
document.write('<layer name=nz'+i+' top=0 left=0 bgcolor='+hCol+' clip="0,0,2,2"></layer>');
}
for (i=0; i < S.length; i++){
document.write('<layer name=nx'+i+' top=0 left=0 bgcolor='+sCol+' clip="0,0,2,2"></layer>');
}
}
if (IE4){
document.write('<div style="position:absolute;top:0px;left:0px"><div style="position:relative">');
for (i=1; i < dots+1; i++){
document.write('<div id="ieDigits" style="position:absolute;top:0px;left:0px;width:30px;height:30px;font-family:Arial;font-size:10px;color:'+fCol+';text-align:center;padding-top:10px">'+i+'</div>');
}
document.write('</div></div>')
document.write('<div style="position:absolute;top:0px;left:0px"><div style="position:relative">');
for (i=0; i < M.length; i++){
document.write('<div id=y style="position:absolute;width:2px;height:2px;font-size:2px;background:'+mCol+'"></div>');
}
document.write('</div></div>')
document.write('<div style="position:absolute;top:0px;left:0px"><div style="position:relative">');
for (i=0; i < H.length; i++){
document.write('<div id=z style="position:absolute;width:2px;height:2px;font-size:2px;background:'+hCol+'"></div>');
}
document.write('</div></div>')
document.write('<div style="position:absolute;top:0px;left:0px"><div style="position:relative">');
for (i=0; i < S.length; i++){
document.write('<div id=x style="position:absolute;width:2px;height:2px;font-size:2px;background:'+sCol+'"></div>');
}
document.write('</div></div>')
}

function clock(){
time = new Date ();
secs = time.getSeconds();
sec = -1.57 + Math.PI * secs/30;
mins = time.getMinutes();
min = -1.57 + Math.PI * mins/30;
hr = time.getHours();
hrs = -1.57 + Math.PI * hr/6 + Math.PI*parseInt(time.getMinutes())/360;

if (NS6){
Ypos=window.pageYOffset+window.innerHeight-Ybase-25;
Xpos=window.pageXOffset+window.innerWidth-Xbase-30;
for (i=1; i < dots+1; i++){
 document.getElementById("n6Digits"+i).style.top=Ypos-15+Ybase*Math.sin(-1.56 +i *Split*Math.PI/180)
 document.getElementById("n6Digits"+i).style.left=Xpos-15+Xbase*Math.cos(-1.56 +i*Split*Math.PI/180)
 }
for (i=0; i < S.length; i++){
 document.getElementById("Nx"+i).style.top=Ypos+i*Ybase/4.1*Math.sin(sec);
 document.getElementById("Nx"+i).style.left=Xpos+i*Xbase/4.1*Math.cos(sec);
 }
for (i=0; i < M.length; i++){
 document.getElementById("Ny"+i).style.top=Ypos+i*Ybase/4.1*Math.sin(min);
 document.getElementById("Ny"+i).style.left=Xpos+i*Xbase/4.1*Math.cos(min);
 }
for (i=0; i < H.length; i++){
 document.getElementById("Nz"+i).style.top=Ypos+i*Ybase/4.1*Math.sin(hrs);
 document.getElementById("Nz"+i).style.left=Xpos+i*Xbase/4.1*Math.cos(hrs);
 }
}
if (NS4){
Ypos=window.pageYOffset+window.innerHeight-Ybase-20;
Xpos=window.pageXOffset+window.innerWidth-Xbase-30;
for (i=0; i < dots; ++i){
 document.layers["nsDigits"+i].top=Ypos-5+Ybase*Math.sin(-1.045 +i*Split*Math.PI/180)
 document.layers["nsDigits"+i].left=Xpos-15+Xbase*Math.cos(-1.045 +i*Split*Math.PI/180)
 }
for (i=0; i < S.length; i++){
 document.layers["nx"+i].top=Ypos+i*Ybase/4.1*Math.sin(sec);
 document.layers["nx"+i].left=Xpos+i*Xbase/4.1*Math.cos(sec);
 }
for (i=0; i < M.length; i++){
 document.layers["ny"+i].top=Ypos+i*Ybase/4.1*Math.sin(min);
 document.layers["ny"+i].left=Xpos+i*Xbase/4.1*Math.cos(min);
 }
for (i=0; i < H.length; i++){
 document.layers["nz"+i].top=Ypos+i*Ybase/4.1*Math.sin(hrs);
 document.layers["nz"+i].left=Xpos+i*Xbase/4.1*Math.cos(hrs);
 }
}

if (IE4){
Ypos=document.body.scrollTop+window.document.body.clientHeight-Ybase-20;
Xpos=document.body.scrollLeft+window.document.body.clientWidth-Xbase-20;
for (i=0; i < dots; ++i){
 ieDigits[i].style.pixelTop=Ypos-15+Ybase*Math.sin(-1.045 +i *Split*Math.PI/180)
 ieDigits[i].style.pixelLeft=Xpos-15+Xbase*Math.cos(-1.045 +i *Split*Math.PI/180)
 }
for (i=0; i < S.length; i++){
 x[i].style.pixelTop =Ypos+i*Ybase/4.1*Math.sin(sec);
 x[i].style.pixelLeft=Xpos+i*Xbase/4.1*Math.cos(sec);
 }
for (i=0; i < M.length; i++){
 y[i].style.pixelTop =Ypos+i*Ybase/4.1*Math.sin(min);
 y[i].style.pixelLeft=Xpos+i*Xbase/4.1*Math.cos(min);
 }
for (i=0; i < H.length; i++){
 z[i].style.pixelTop =Ypos+i*Ybase/4.1*Math.sin(hrs);
 z[i].style.pixelLeft=Xpos+i*Xbase/4.1*Math.cos(hrs);
 }
}
setTimeout('clock()',100);
}
clock();
//-->
</script>

Code một trang đồng hồ mẫu hoàn chỉnh.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
 <title>Dong ho bang JavaScript By VnTim</title>
 </head>

<body>
 <form name="BM1">
 <input type="text" name="MucVB">
 </form>

<script language="javascript">
 function HamThoiGian(){
 // Khoi tao doi tuong thoi gian.
 var ThoiGian = new Date(); // Khoi tao doi tuong Date.
 var Gio = ThoiGian.getHours(); // Lay gia tri gio tu doi tuong Date.
 var Phut = ThoiGian.getMinutes(); // Lay phút tu doi tuong Date.
 var Giay = ThoiGian.getSeconds(); // Lay giây tu doi tuong Date
 // Thêm so 0 vào truoc so chi thoi gian neu nhu nó nho hon 10.
 if(Gio<10){
 Gio="0"+Gio;}
 if(Phut<10){
 Phut="0"+Phut;}
 if(Giay<10){
 Giay="0"+Giay;}
 // Xuat ket qua ra muc nhap van ban có tên MucVB
 document.BM1.MucVB.value="Bay gio la: "+Gio+":"+Phut+":"+Giay;
 // Goi lai hàm HamThoiGian sau moi mot giây trôi qua.
 }
 setInterval("HamThoiGian()",1000); // Su dung setInterval de goi lap di lap lai hàm HamThoiGian sau 1 giây.
 </script>

</body>
 </html>