首页 > web前端 > js教程 > JavaScript在浏览器标题栏上显示当前日期和时间的方法_javascript技巧

JavaScript在浏览器标题栏上显示当前日期和时间的方法_javascript技巧

WBOY
发布: 2016-05-16 16:08:21
原创
1463 人浏览过

本文实例讲述了JavaScript在浏览器标题栏上显示当前日期和时间的方法,分享给大家供大家参考。具体如下:

将这段脚本放到head区即可:

<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) &#63; hours -12 :hours)
timeValue = ((timeValue <10)&#63; "0":"") + timeValue
timeValue += ((minutes < 10) &#63; ":0" : ":") + minutes
timeValue += ((seconds < 10) &#63; ":0" : ":") + seconds
timeValue += (hours >= 12) &#63; " 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) &#63; "0" : "")+ dateNow.getDate();
function y2k(number){return (number < 1000) &#63; 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程序设计有所帮助。

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板