萬盛學電腦網

 萬盛學電腦網 >> 腳本專題 >> javascript >> JavaScript在浏覽器標題欄上顯示當前日期和時間的方法

JavaScript在浏覽器標題欄上顯示當前日期和時間的方法

 這篇文章主要介紹了JavaScript在浏覽器標題欄上顯示當前日期和時間的方法,實例分析了javascript操作時間及DOM節點實現定時觸發的技巧,非常具有實用價值,需要的朋友可以參考下

   

本文實例講述了JavaScript在浏覽器標題欄上顯示當前日期和時間的方法,分享給大家供大家參考。具體如下:

將這段腳本放到head區即可:

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 <script language="JavaScript"> <!-- function resetIt() { // Calculate Time var timerID = null; var timerRunning = false; if(timerRunning) clearTimeout(timerID); timerRunning = false; // getTime var timeNow = new Date(); var hours = timeNow.getHours(); var minutes = timeNow.getMinutes(); var seconds = timeNow.getSeconds(); var timeValue = "" + ((hours >12) ? hours -12 :hours) timeValue = ((timeValue <10)? "0":"") + timeValue timeValue += ((minutes < 10) ? ":0" : ":") + minutes timeValue += ((seconds < 10) ? ":0" : ":") + seconds timeValue += (hours >= 12) ? " PM" : " AM" timerID = setTimeout("resetIt()",100); timerRunning = true; // getDate var dateNow = new Date(); var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December'); var date = ((dateNow.getDate()<10) ? "0" : "")+ dateNow.getDate(); function y2k(number){return (number < 1000) ? number + 1900 : number;} // compileIt today = timeValue + " " + days[dateNow.getDay()] + " " + months[dateNow.getMonth()] + ", " + date + " " + (y2k(dateNow.getYear())); if(document.all || document.getElementById){ // Browser Check document.title = today.toString(); }else{ self.status = today.toString(); // Default to status. } } resetIt(); //--> </script>

希望本文所述對大家的javascript程序設計有所幫助。

copyright © 萬盛學電腦網 all rights reserved