JavaScript在浏览器标题栏上显示当前日期和时间的方法
更新时间:2015年03月19日 16:36:17 作者:不吃皮蛋
这篇文章主要介绍了JavaScript在浏览器标题栏上显示当前日期和时间的方法,实例分析了javascript操作时间及DOM节点实现定时触发的技巧,非常具有实用价值,需要的朋友可以参考下
本文实例讲述了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) ? 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程序设计有所帮助。
相关文章
js中AppendChild与insertBefore的用法详细解析
这篇文章主要是对js中AppendChild与insertBefore的用法进行了详细的分析介绍,需要的朋友可以过来参考下,希望对大家有所帮助2013-12-12
List Information About the Binary Files Used by an Applicati
List Information About the Binary Files Used by an Application...2007-06-06
每天一篇javascript学习小结(Function对象)
这篇文章主要介绍了javascript中的Function对象知识点,对Function对象的基本使用方法,以及各种方法进行整理,感兴趣的小伙伴们可以参考一下2015-11-11
ES6通过babel转码使用webpack使用import关键字
这篇文章主要介绍了es6通过babel转码还需要使用webpack才可以使用import关键字吗的相关资料,需要的朋友可以参考下2016-12-12


最新评论